feat: add server log preview
This commit is contained in:
@@ -9,6 +9,7 @@ import { ServerSettingsEditor } from '@/components/configuration/ServerSettingsE
|
|||||||
import { StatisticsDashboard } from '@/components/statistics/StatisticsDashboard';
|
import { StatisticsDashboard } from '@/components/statistics/StatisticsDashboard';
|
||||||
import { LeaderboardManager } from '@/components/leaderboard/LeaderboardManager';
|
import { LeaderboardManager } from '@/components/leaderboard/LeaderboardManager';
|
||||||
import { ActivityLogTable } from '@/components/activityLog/ActivityLogTable';
|
import { ActivityLogTable } from '@/components/activityLog/ActivityLogTable';
|
||||||
|
import { ServerLogViewer } from '@/components/server/ServerLogViewer';
|
||||||
import { useState } from 'react';
|
import { useState } from 'react';
|
||||||
import { StateHistoryStats } from '@/lib/schemas';
|
import { StateHistoryStats } from '@/lib/schemas';
|
||||||
import { Leaderboard } from '@/lib/schemas/leaderboard';
|
import { Leaderboard } from '@/lib/schemas/leaderboard';
|
||||||
@@ -28,7 +29,8 @@ const tabs = [
|
|||||||
{ id: ServerTab.eventRules, name: 'Event Rules', icon: '📋' },
|
{ id: ServerTab.eventRules, name: 'Event Rules', icon: '📋' },
|
||||||
{ id: ServerTab.settings, name: 'Server Settings', icon: '🔧' },
|
{ id: ServerTab.settings, name: 'Server Settings', icon: '🔧' },
|
||||||
{ id: ServerTab.leaderboard, name: 'Leaderboard', icon: '🏆' },
|
{ id: ServerTab.leaderboard, name: 'Leaderboard', icon: '🏆' },
|
||||||
{ id: ServerTab.activityLog, name: 'History', icon: '📋' }
|
{ id: ServerTab.activityLog, name: 'History', icon: '📋' },
|
||||||
|
{ id: ServerTab.logs, name: 'Logs', icon: '📄' }
|
||||||
];
|
];
|
||||||
|
|
||||||
const configTabs = new Set([
|
const configTabs = new Set([
|
||||||
@@ -143,6 +145,9 @@ export function ServerConfigurationTabs({
|
|||||||
case ServerTab.activityLog:
|
case ServerTab.activityLog:
|
||||||
return <ActivityLogTable serverId={serverId} />;
|
return <ActivityLogTable serverId={serverId} />;
|
||||||
|
|
||||||
|
case ServerTab.logs:
|
||||||
|
return <ServerLogViewer serverId={serverId} />;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return (
|
return (
|
||||||
<div className="py-12 text-center">
|
<div className="py-12 text-center">
|
||||||
|
|||||||
153
src/components/server/ServerLogViewer.tsx
Normal file
153
src/components/server/ServerLogViewer.tsx
Normal file
@@ -0,0 +1,153 @@
|
|||||||
|
'use client';
|
||||||
|
|
||||||
|
import { useEffect, useRef, useState, useCallback } from 'react';
|
||||||
|
import { useWebSocket } from '@/lib/websocket/context';
|
||||||
|
import type { WebSocketMessage } from '@/lib/schemas/websocket';
|
||||||
|
import { getServerLogAction } from '@/lib/actions/serverLog';
|
||||||
|
|
||||||
|
const MAX_BUFFER = 5000;
|
||||||
|
const LINE_OPTIONS = [100, 500, 1000, 5000] as const;
|
||||||
|
|
||||||
|
interface ServerLogViewerProps {
|
||||||
|
serverId: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function ServerLogViewer({ serverId }: ServerLogViewerProps) {
|
||||||
|
const [lines, setLines] = useState<string[]>([]);
|
||||||
|
const [initialLines, setInitialLines] = useState<number>(100);
|
||||||
|
const [isLoading, setIsLoading] = useState(false);
|
||||||
|
const [error, setError] = useState<string | null>(null);
|
||||||
|
const [isPaused, setIsPaused] = useState(false);
|
||||||
|
|
||||||
|
const { associateWithServer, addMessageHandler, removeMessageHandler } = useWebSocket();
|
||||||
|
const scrollRef = useRef<HTMLDivElement>(null);
|
||||||
|
const isPausedRef = useRef(false);
|
||||||
|
|
||||||
|
const appendLines = useCallback((newLines: string[]) => {
|
||||||
|
setLines((prev) => {
|
||||||
|
const combined = [...prev, ...newLines];
|
||||||
|
return combined.length > MAX_BUFFER ? combined.slice(combined.length - MAX_BUFFER) : combined;
|
||||||
|
});
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const fetchInitial = useCallback(
|
||||||
|
async (count: number) => {
|
||||||
|
setIsLoading(true);
|
||||||
|
setError(null);
|
||||||
|
const result = await getServerLogAction(serverId, count);
|
||||||
|
setIsLoading(false);
|
||||||
|
if (!result.success) {
|
||||||
|
setError(result.message);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
setLines(result.lines);
|
||||||
|
},
|
||||||
|
[serverId]
|
||||||
|
);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
associateWithServer(serverId);
|
||||||
|
fetchInitial(initialLines);
|
||||||
|
}, [serverId, associateWithServer, fetchInitial, initialLines]);
|
||||||
|
|
||||||
|
const handleMessage = useCallback(
|
||||||
|
(message: WebSocketMessage) => {
|
||||||
|
if (message.type !== 'log_line') return;
|
||||||
|
if (message.server_id !== serverId) return;
|
||||||
|
appendLines([message.data.line]);
|
||||||
|
},
|
||||||
|
[serverId, appendLines]
|
||||||
|
);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
addMessageHandler(handleMessage);
|
||||||
|
return () => {
|
||||||
|
removeMessageHandler(handleMessage);
|
||||||
|
};
|
||||||
|
}, [addMessageHandler, removeMessageHandler, handleMessage]);
|
||||||
|
|
||||||
|
// Auto-scroll to bottom when new lines arrive, unless paused
|
||||||
|
useEffect(() => {
|
||||||
|
if (!isPausedRef.current && scrollRef.current) {
|
||||||
|
scrollRef.current.scrollTop = scrollRef.current.scrollHeight;
|
||||||
|
}
|
||||||
|
}, [lines]);
|
||||||
|
|
||||||
|
const handleScroll = () => {
|
||||||
|
if (!scrollRef.current) return;
|
||||||
|
const { scrollTop, scrollHeight, clientHeight } = scrollRef.current;
|
||||||
|
const isAtBottom = scrollHeight - scrollTop - clientHeight < 40;
|
||||||
|
const shouldPause = !isAtBottom;
|
||||||
|
isPausedRef.current = shouldPause;
|
||||||
|
setIsPaused(shouldPause);
|
||||||
|
};
|
||||||
|
|
||||||
|
const jumpToBottom = () => {
|
||||||
|
if (scrollRef.current) {
|
||||||
|
scrollRef.current.scrollTop = scrollRef.current.scrollHeight;
|
||||||
|
}
|
||||||
|
isPausedRef.current = false;
|
||||||
|
setIsPaused(false);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleLineLimitChange = (e: React.ChangeEvent<HTMLSelectElement>) => {
|
||||||
|
const count = Number(e.target.value);
|
||||||
|
setInitialLines(count);
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="flex flex-col gap-3">
|
||||||
|
<div className="flex items-center gap-3">
|
||||||
|
<label className="text-xs text-subtle">Initial lines</label>
|
||||||
|
<select
|
||||||
|
value={initialLines}
|
||||||
|
onChange={handleLineLimitChange}
|
||||||
|
className="form-input w-28 text-sm"
|
||||||
|
>
|
||||||
|
{LINE_OPTIONS.map((n) => (
|
||||||
|
<option key={n} value={n}>
|
||||||
|
{n}
|
||||||
|
</option>
|
||||||
|
))}
|
||||||
|
</select>
|
||||||
|
<span className="ml-auto text-xs text-subtle">{lines.length} lines buffered</span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{error && <p className="text-sm text-red">{error}</p>}
|
||||||
|
|
||||||
|
<div className="relative">
|
||||||
|
<div
|
||||||
|
ref={scrollRef}
|
||||||
|
onScroll={handleScroll}
|
||||||
|
className="h-[calc(100vh-280px)] min-h-64 overflow-y-auto rounded-lg border border-border bg-base p-3 font-mono text-xs text-secondary"
|
||||||
|
>
|
||||||
|
{isLoading ? (
|
||||||
|
<span className="text-muted">Loading…</span>
|
||||||
|
) : lines.length === 0 ? (
|
||||||
|
<span className="text-muted">No log output yet.</span>
|
||||||
|
) : (
|
||||||
|
lines.map((line, i) => (
|
||||||
|
<div key={i} className="leading-5 whitespace-pre-wrap break-all">
|
||||||
|
{line}
|
||||||
|
</div>
|
||||||
|
))
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{isPaused && (
|
||||||
|
<div className="absolute bottom-3 left-1/2 flex -translate-x-1/2 items-center gap-2">
|
||||||
|
<div className="flex items-center gap-2 rounded-full border border-border bg-overlay px-3 py-1.5 shadow-lg">
|
||||||
|
<span className="text-xs text-muted">Paused</span>
|
||||||
|
<button
|
||||||
|
onClick={jumpToBottom}
|
||||||
|
className="text-xs font-medium text-blue hover:underline"
|
||||||
|
>
|
||||||
|
Jump to bottom
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
20
src/lib/actions/serverLog.ts
Normal file
20
src/lib/actions/serverLog.ts
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
'use server';
|
||||||
|
|
||||||
|
import { requireAuth } from '@/lib/auth/server';
|
||||||
|
import { getServerLog } from '@/lib/api/server/serverLog';
|
||||||
|
|
||||||
|
export async function getServerLogAction(
|
||||||
|
serverId: string,
|
||||||
|
lines: number = 100
|
||||||
|
): Promise<{ success: true; lines: string[] } | { success: false; message: string }> {
|
||||||
|
try {
|
||||||
|
const session = await requireAuth();
|
||||||
|
const data = await getServerLog(session.token!, serverId, lines);
|
||||||
|
return { success: true, lines: data };
|
||||||
|
} catch (error) {
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
message: error instanceof Error ? error.message : 'Failed to fetch server log'
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
13
src/lib/api/server/serverLog.ts
Normal file
13
src/lib/api/server/serverLog.ts
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
import { fetchServerAPI } from './base';
|
||||||
|
|
||||||
|
export async function getServerLog(
|
||||||
|
token: string,
|
||||||
|
serverId: string,
|
||||||
|
lines: number = 100
|
||||||
|
): Promise<string[]> {
|
||||||
|
const response = await fetchServerAPI<{ lines: string[] }>(
|
||||||
|
`/server/${serverId}/log?lines=${lines}`,
|
||||||
|
token
|
||||||
|
);
|
||||||
|
return response.data?.lines ?? [];
|
||||||
|
}
|
||||||
@@ -17,7 +17,8 @@ export enum ServerTab {
|
|||||||
eventRules = 'eventRules',
|
eventRules = 'eventRules',
|
||||||
settings = 'settings',
|
settings = 'settings',
|
||||||
leaderboard = 'leaderboard',
|
leaderboard = 'leaderboard',
|
||||||
activityLog = 'activityLog'
|
activityLog = 'activityLog',
|
||||||
|
logs = 'logs'
|
||||||
}
|
}
|
||||||
export const serverTabSchema = z.enum(ServerTab);
|
export const serverTabSchema = z.enum(ServerTab);
|
||||||
|
|
||||||
|
|||||||
@@ -44,6 +44,13 @@ export const completeDataSchema = z.object({
|
|||||||
|
|
||||||
export type CompleteData = z.infer<typeof completeDataSchema>;
|
export type CompleteData = z.infer<typeof completeDataSchema>;
|
||||||
|
|
||||||
|
// Log line data schema
|
||||||
|
export const logLineDataSchema = z.object({
|
||||||
|
line: z.string()
|
||||||
|
});
|
||||||
|
|
||||||
|
export type LogLineData = z.infer<typeof logLineDataSchema>;
|
||||||
|
|
||||||
// WebSocket message schema using discriminated union
|
// WebSocket message schema using discriminated union
|
||||||
export const webSocketMessageSchema = z.discriminatedUnion('type', [
|
export const webSocketMessageSchema = z.discriminatedUnion('type', [
|
||||||
z.object({
|
z.object({
|
||||||
@@ -69,6 +76,12 @@ export const webSocketMessageSchema = z.discriminatedUnion('type', [
|
|||||||
server_id: z.string(),
|
server_id: z.string(),
|
||||||
timestamp: z.number(),
|
timestamp: z.number(),
|
||||||
data: completeDataSchema
|
data: completeDataSchema
|
||||||
|
}),
|
||||||
|
z.object({
|
||||||
|
type: z.literal('log_line'),
|
||||||
|
server_id: z.string(),
|
||||||
|
timestamp: z.number(),
|
||||||
|
data: logLineDataSchema
|
||||||
})
|
})
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user