feat: add server column to global activity log

This commit is contained in:
2026-03-23 22:12:10 +01:00
parent 36c31fa790
commit 2d85b63775
5 changed files with 81 additions and 64 deletions

View File

@@ -1,13 +1,16 @@
import { requireAuth } from '@/lib/auth/server'; import { requireAuth } from '@/lib/auth/server';
import { getServers } from '@/lib/api/server/servers';
import { ActivityLogTable } from '@/components/activityLog/ActivityLogTable'; import { ActivityLogTable } from '@/components/activityLog/ActivityLogTable';
export default async function ActivityLogPage() { export default async function ActivityLogPage() {
await requireAuth(); const session = await requireAuth();
const servers = await getServers(session.token!);
const serverOptions = servers.map((s) => ({ id: s.id, name: s.name }));
return ( return (
<main className="p-5"> <main className="p-5">
<h1 className="mb-5 text-lg font-semibold text-primary">Activity Log</h1> <h1 className="mb-5 text-lg font-semibold text-primary">Activity Log</h1>
<ActivityLogTable /> <ActivityLogTable servers={serverOptions} />
</main> </main>
); );
} }

View File

@@ -8,8 +8,14 @@ import { Button } from '@/components/ui/Button';
const PAGE_SIZE = 10; const PAGE_SIZE = 10;
interface ServerOption {
id: string;
name: string;
}
interface ActivityLogTableProps { interface ActivityLogTableProps {
serverId?: string; serverId?: string;
servers?: ServerOption[];
} }
function formatDate(iso: string): string { function formatDate(iso: string): string {
@@ -47,7 +53,7 @@ const ACTION_BADGE: Record<ActionType, string> = {
leaderboard_update: 'bg-overlay text-muted' leaderboard_update: 'bg-overlay text-muted'
}; };
export function ActivityLogTable({ serverId }: ActivityLogTableProps) { export function ActivityLogTable({ serverId, servers }: ActivityLogTableProps) {
const [logs, setLogs] = useState<ActivityLog[]>([]); const [logs, setLogs] = useState<ActivityLog[]>([]);
const [page, setPage] = useState(1); const [page, setPage] = useState(1);
const [hasMore, setHasMore] = useState(true); const [hasMore, setHasMore] = useState(true);
@@ -55,6 +61,7 @@ export function ActivityLogTable({ serverId }: ActivityLogTableProps) {
const [error, setError] = useState<string | null>(null); const [error, setError] = useState<string | null>(null);
const [filterAction, setFilterAction] = useState<ActionType | ''>(''); const [filterAction, setFilterAction] = useState<ActionType | ''>('');
const [filterServerId, setFilterServerId] = useState('');
const [filterStartDate, setFilterStartDate] = useState(''); const [filterStartDate, setFilterStartDate] = useState('');
const [filterEndDate, setFilterEndDate] = useState(''); const [filterEndDate, setFilterEndDate] = useState('');
@@ -65,9 +72,8 @@ export function ActivityLogTable({ serverId }: ActivityLogTableProps) {
setIsLoading(true); setIsLoading(true);
setError(null); setError(null);
const params = { ...filter, page: currentPage, page_size: PAGE_SIZE }; const params = { ...filter, page: currentPage, page_size: PAGE_SIZE };
const result = serverId if (serverId) params.server_id = serverId;
? await getActivityLogAction(serverId, params) const result = await getActivityLogAction(params);
: await getGlobalActivityLogAction(params);
setIsLoading(false); setIsLoading(false);
if (!result.success) { if (!result.success) {
setError(result.message); setError(result.message);
@@ -86,6 +92,7 @@ export function ActivityLogTable({ serverId }: ActivityLogTableProps) {
const applyFilters = () => { const applyFilters = () => {
const filter: ActivityLogFilter = {}; const filter: ActivityLogFilter = {};
if (filterAction) filter.action = filterAction as ActionType; if (filterAction) filter.action = filterAction as ActionType;
if (filterServerId) filter.server_id = filterServerId;
if (filterStartDate) filter.start_date = new Date(filterStartDate).toISOString(); if (filterStartDate) filter.start_date = new Date(filterStartDate).toISOString();
if (filterEndDate) filter.end_date = new Date(filterEndDate).toISOString(); if (filterEndDate) filter.end_date = new Date(filterEndDate).toISOString();
setAppliedFilter(filter); setAppliedFilter(filter);
@@ -96,6 +103,7 @@ export function ActivityLogTable({ serverId }: ActivityLogTableProps) {
const clearFilters = () => { const clearFilters = () => {
setFilterAction(''); setFilterAction('');
setFilterServerId('');
setFilterStartDate(''); setFilterStartDate('');
setFilterEndDate(''); setFilterEndDate('');
setAppliedFilter({}); setAppliedFilter({});
@@ -112,9 +120,9 @@ export function ActivityLogTable({ serverId }: ActivityLogTableProps) {
return ( return (
<div className="flex flex-col gap-4"> <div className="flex flex-col gap-4">
<div className="flex flex-wrap items-end gap-3 rounded-lg border border-border bg-canvas p-4"> <div className="border-border bg-canvas flex flex-wrap items-end gap-3 rounded-lg border p-4">
<div className="flex flex-col gap-1"> <div className="flex flex-col gap-1">
<label className="text-xs text-subtle">Action type</label> <label className="text-subtle text-xs">Action type</label>
<select <select
value={filterAction} value={filterAction}
onChange={(e) => setFilterAction(e.target.value as ActionType | '')} onChange={(e) => setFilterAction(e.target.value as ActionType | '')}
@@ -128,8 +136,25 @@ export function ActivityLogTable({ serverId }: ActivityLogTableProps) {
))} ))}
</select> </select>
</div> </div>
{servers && servers.length > 0 && (
<div className="flex flex-col gap-1">
<label className="text-subtle text-xs">Server</label>
<select
value={filterServerId}
onChange={(e) => setFilterServerId(e.target.value)}
className="form-input w-48 text-sm"
>
<option value="">All servers</option>
{servers.map((s) => (
<option key={s.id} value={s.id}>
{s.name}
</option>
))}
</select>
</div>
)}
<div className="flex flex-col gap-1"> <div className="flex flex-col gap-1">
<label className="text-xs text-subtle">From</label> <label className="text-subtle text-xs">From</label>
<input <input
type="date" type="date"
value={filterStartDate} value={filterStartDate}
@@ -138,7 +163,7 @@ export function ActivityLogTable({ serverId }: ActivityLogTableProps) {
/> />
</div> </div>
<div className="flex flex-col gap-1"> <div className="flex flex-col gap-1">
<label className="text-xs text-subtle">To</label> <label className="text-subtle text-xs">To</label>
<input <input
type="date" type="date"
value={filterEndDate} value={filterEndDate}
@@ -154,31 +179,34 @@ export function ActivityLogTable({ serverId }: ActivityLogTableProps) {
</Button> </Button>
</div> </div>
{error && ( {error && <p className="text-red text-sm">{error}</p>}
<p className="text-sm text-red">{error}</p>
)}
<div className="overflow-hidden rounded-lg border border-border bg-canvas"> <div className="border-border bg-canvas overflow-hidden rounded-lg border">
{logs.length === 0 && !isLoading ? ( {logs.length === 0 && !isLoading ? (
<div className="px-4 py-12 text-center"> <div className="px-4 py-12 text-center">
<div className="mb-3 text-4xl">📋</div> <div className="mb-3 text-4xl">📋</div>
<p className="text-sm text-muted">No activity logged yet.</p> <p className="text-muted text-sm">No activity logged yet.</p>
</div> </div>
) : ( ) : (
<div className="overflow-x-auto"> <div className="overflow-x-auto">
<table className="min-w-full"> <table className="min-w-full">
<thead> <thead>
<tr className="border-b border-border-muted bg-base"> <tr className="border-border-muted bg-base border-b">
<th className="px-4 py-2 text-left text-xs font-medium uppercase tracking-wider text-subtle"> <th className="text-subtle px-4 py-2 text-left text-xs font-medium tracking-wider uppercase">
Time Time
</th> </th>
<th className="px-4 py-2 text-left text-xs font-medium uppercase tracking-wider text-subtle"> <th className="text-subtle px-4 py-2 text-left text-xs font-medium tracking-wider uppercase">
User User
</th> </th>
<th className="px-4 py-2 text-left text-xs font-medium uppercase tracking-wider text-subtle"> {!serverId && (
<th className="text-subtle px-4 py-2 text-left text-xs font-medium tracking-wider uppercase">
Server
</th>
)}
<th className="text-subtle px-4 py-2 text-left text-xs font-medium tracking-wider uppercase">
Action Action
</th> </th>
<th className="px-4 py-2 text-left text-xs font-medium uppercase tracking-wider text-subtle"> <th className="text-subtle px-4 py-2 text-left text-xs font-medium tracking-wider uppercase">
Details Details
</th> </th>
</tr> </tr>
@@ -187,14 +215,17 @@ export function ActivityLogTable({ serverId }: ActivityLogTableProps) {
{logs.map((log) => ( {logs.map((log) => (
<tr <tr
key={log.id} key={log.id}
className="border-b border-border-muted transition-colors hover:bg-hover last:border-0" className="border-border-muted hover:bg-hover border-b transition-colors last:border-0"
> >
<td className="whitespace-nowrap px-4 py-3 text-sm text-muted"> <td className="text-muted px-4 py-3 text-sm whitespace-nowrap">
{formatDate(log.createdAt)} {formatDate(log.createdAt)}
</td> </td>
<td className="px-4 py-3 text-sm text-secondary"> <td className="text-secondary px-4 py-3 text-sm">{log.username}</td>
{log.username} {!serverId && (
</td> <td className="text-secondary px-4 py-3 text-sm">
{log.server?.name ?? log.serverId}
</td>
)}
<td className="px-4 py-3"> <td className="px-4 py-3">
<span <span
className={`inline-flex items-center rounded-full px-2 py-0.5 text-xs font-medium ${ACTION_BADGE[log.action] ?? 'bg-canvas text-muted'}`} className={`inline-flex items-center rounded-full px-2 py-0.5 text-xs font-medium ${ACTION_BADGE[log.action] ?? 'bg-canvas text-muted'}`}
@@ -202,9 +233,7 @@ export function ActivityLogTable({ serverId }: ActivityLogTableProps) {
{ACTION_TYPE_LABELS[log.action] ?? log.action} {ACTION_TYPE_LABELS[log.action] ?? log.action}
</span> </span>
</td> </td>
<td className="px-4 py-3 text-sm text-muted"> <td className="text-muted px-4 py-3 text-sm">{parseDetails(log.details)}</td>
{parseDetails(log.details)}
</td>
</tr> </tr>
))} ))}
</tbody> </tbody>
@@ -212,12 +241,10 @@ export function ActivityLogTable({ serverId }: ActivityLogTableProps) {
</div> </div>
)} )}
{isLoading && ( {isLoading && <div className="text-muted px-4 py-4 text-center text-sm">Loading…</div>}
<div className="px-4 py-4 text-center text-sm text-muted">Loading…</div>
)}
{hasMore && !isLoading && logs.length > 0 && ( {hasMore && !isLoading && logs.length > 0 && (
<div className="border-t border-border-muted px-4 py-3 text-center"> <div className="border-border-muted border-t px-4 py-3 text-center">
<Button variant="ghost" size="sm" onClick={loadMore}> <Button variant="ghost" size="sm" onClick={loadMore}>
Load more Load more
</Button> </Button>

View File

@@ -1,31 +1,15 @@
'use server'; 'use server';
import { requireAuth } from '@/lib/auth/server'; import { requireAuth } from '@/lib/auth/server';
import { getActivityLog, getGlobalActivityLog } from '@/lib/api/server/activityLog'; import { getActivityLog } from '@/lib/api/server/activityLog';
import type { ActivityLog, ActivityLogFilter } from '@/lib/schemas/activityLog'; import type { ActivityLog, ActivityLogFilter } from '@/lib/schemas/activityLog';
export async function getActivityLogAction( export async function getActivityLogAction(
serverId: string,
filter?: ActivityLogFilter filter?: ActivityLogFilter
): Promise<{ success: true; data: ActivityLog[] } | { success: false; message: string }> { ): Promise<{ success: true; data: ActivityLog[] } | { success: false; message: string }> {
try { try {
const session = await requireAuth(); const session = await requireAuth();
const data = await getActivityLog(session.token!, serverId, filter); const data = await getActivityLog(session.token!, filter);
return { success: true, data };
} catch (error) {
return {
success: false,
message: error instanceof Error ? error.message : 'Failed to fetch activity log'
};
}
}
export async function getGlobalActivityLogAction(
filter?: ActivityLogFilter
): Promise<{ success: true; data: ActivityLog[] } | { success: false; message: string }> {
try {
const session = await requireAuth();
const data = await getGlobalActivityLog(session.token!, filter);
return { success: true, data }; return { success: true, data };
} catch (error) { } catch (error) {
return { return {

View File

@@ -1,5 +1,9 @@
import { fetchServerAPI } from './base'; import { fetchServerAPI } from './base';
import { activityLogSchema, type ActivityLog, type ActivityLogFilter } from '@/lib/schemas/activityLog'; import {
activityLogSchema,
type ActivityLog,
type ActivityLogFilter
} from '@/lib/schemas/activityLog';
function buildQuery(filter?: ActivityLogFilter): string { function buildQuery(filter?: ActivityLogFilter): string {
if (!filter) return ''; if (!filter) return '';
@@ -9,6 +13,7 @@ function buildQuery(filter?: ActivityLogFilter): string {
if (filter.sort_by) params.set('sort_by', filter.sort_by); if (filter.sort_by) params.set('sort_by', filter.sort_by);
if (filter.sort_desc !== undefined) params.set('sort_desc', String(filter.sort_desc)); if (filter.sort_desc !== undefined) params.set('sort_desc', String(filter.sort_desc));
if (filter.user_id) params.set('user_id', filter.user_id); if (filter.user_id) params.set('user_id', filter.user_id);
if (filter.server_id) params.set('server_id', filter.server_id);
if (filter.action) params.set('action', filter.action); if (filter.action) params.set('action', filter.action);
if (filter.start_date) params.set('start_date', filter.start_date); if (filter.start_date) params.set('start_date', filter.start_date);
if (filter.end_date) params.set('end_date', filter.end_date); if (filter.end_date) params.set('end_date', filter.end_date);
@@ -17,23 +22,11 @@ function buildQuery(filter?: ActivityLogFilter): string {
} }
export async function getActivityLog( export async function getActivityLog(
token: string,
serverId: string,
filter?: ActivityLogFilter
): Promise<ActivityLog[]> {
const query = buildQuery(filter);
const response = await fetchServerAPI<ActivityLog[]>(
`/server/${serverId}/activity-log${query}`,
token
);
return activityLogSchema.array().parse(response.data);
}
export async function getGlobalActivityLog(
token: string, token: string,
filter?: ActivityLogFilter filter?: ActivityLogFilter
): Promise<ActivityLog[]> { ): Promise<ActivityLog[]> {
const query = buildQuery(filter); const query = buildQuery(filter);
const response = await fetchServerAPI<ActivityLog[]>(`/activity-log${query}`, token); const response = await fetchServerAPI<ActivityLog[]>(`/activity-log${query}`, token);
console.log(response.data);
return activityLogSchema.array().parse(response.data); return activityLogSchema.array().parse(response.data);
} }

View File

@@ -10,9 +10,18 @@ export const actionTypeSchema = z.enum([
export type ActionType = z.infer<typeof actionTypeSchema>; export type ActionType = z.infer<typeof actionTypeSchema>;
// Minimal server shape embedded in activity log responses (avoids circular import with server.ts)
const embeddedServerSchema = z.object({
id: z.string(),
name: z.string()
}).passthrough();
export type EmbeddedServer = z.infer<typeof embeddedServerSchema>;
export const activityLogSchema = z.object({ export const activityLogSchema = z.object({
id: z.string(), id: z.string(),
serverId: z.string(), serverId: z.string(),
server: embeddedServerSchema.optional(),
userId: z.string(), userId: z.string(),
username: z.string(), username: z.string(),
action: actionTypeSchema, action: actionTypeSchema,
@@ -36,6 +45,7 @@ export const activityLogFilterSchema = z.object({
sort_by: z.string().optional(), sort_by: z.string().optional(),
sort_desc: z.boolean().optional(), sort_desc: z.boolean().optional(),
user_id: z.string().optional(), user_id: z.string().optional(),
server_id: z.string().optional(),
action: actionTypeSchema.optional(), action: actionTypeSchema.optional(),
start_date: z.string().optional(), start_date: z.string().optional(),
end_date: z.string().optional() end_date: z.string().optional()