Files
TREK/client/src/components/Admin/useAuditLog.ts
T
Maurice 44c6fbf372 fix(settings): let every date follow a date format of the user's choice
Every date in TREK was written in the interface language's format, and
English is en-US, so an English interface always showed and read
mm/dd/yyyy with no way to change it. A few native date inputs followed
the browser instead, so one screen could mix two formats.

date_format is now a user setting with an admin default, next to the
time format: automatic (the language's format, as before), YYYY-MM-DD,
YYYY/MM/DD, DD.MM.YYYY, DD/MM/YYYY or MM/DD/YYYY. The date picker shows,
pre-fills, hints and reads typed dates in it, and every date written in
numbers across the app (lists, cards, admin panels, CSV and PDF exports)
goes through one formatter. Dates written with a month name keep the
language's wording. The remaining native date inputs are the shared
picker now. The display settings MCP tool reads and writes the key.

Closes #2971
2026-11-03 23:38:37 +00:00

116 lines
3.1 KiB
TypeScript

import { useCallback, useEffect, useRef, useState } from 'react';
import { adminApi } from '../../api/client';
import { useFormatDate } from '../../hooks/useDateFormat';
export interface AuditEntry {
id: number;
created_at: string;
user_id: number | null;
username: string | null;
user_email: string | null;
action: string;
resource: string | null;
details: Record<string, unknown> | null;
ip: string | null;
}
const PAGE_SIZE = 100;
/** A details object as one line of JSON, or a dash when there is nothing to show. */
export function fmtAuditDetails(d: Record<string, unknown> | null): string {
if (!d || Object.keys(d).length === 0) return '—';
try {
return JSON.stringify(d);
} catch {
return '—';
}
}
/** Who did it: the username, else the email, else the bare user id. */
export function auditUserLabel(e: AuditEntry): string {
if (e.username) return e.username;
if (e.user_email) return e.user_email;
if (e.user_id != null) return `#${e.user_id}`;
return '—';
}
/**
* The audit log behind both admin shells: the first page on mount, a refresh that starts
* over and a "load more" that appends the next page. The desktop table and the phone
* cards render their own markup over it.
*/
export function useAuditLog(serverTimezone?: string) {
const { formatDateTime } = useFormatDate();
const [entries, setEntries] = useState<AuditEntry[]>([]);
const [total, setTotal] = useState(0);
const [loading, setLoading] = useState(true);
// Never rendered, only carried between pages, so loadMore cannot capture a page number
// that is already spent.
const offsetRef = useRef(0);
const loadFirstPage = useCallback(async () => {
setLoading(true);
try {
const data = (await adminApi.auditLog({ limit: PAGE_SIZE, offset: 0 })) as {
entries: AuditEntry[];
total: number;
};
setEntries(data.entries || []);
setTotal(data.total ?? 0);
offsetRef.current = 0;
} catch {
setEntries([]);
setTotal(0);
offsetRef.current = 0;
} finally {
setLoading(false);
}
}, []);
const loadMore = useCallback(async () => {
const nextOffset = offsetRef.current + PAGE_SIZE;
setLoading(true);
try {
const data = (await adminApi.auditLog({ limit: PAGE_SIZE, offset: nextOffset })) as {
entries: AuditEntry[];
total: number;
};
setEntries((prev) => [...prev, ...(data.entries || [])]);
setTotal(data.total ?? 0);
offsetRef.current = nextOffset;
} catch {
/* keep existing */
} finally {
setLoading(false);
}
}, []);
useEffect(() => {
loadFirstPage();
}, [loadFirstPage]);
const fmtTime = (iso: string) => {
try {
return formatDateTime(new Date(iso.endsWith('Z') ? iso : iso + 'Z'), {
dateStyle: 'short',
timeStyle: 'medium',
timeZone: serverTimezone || undefined,
});
} catch {
return iso;
}
};
return {
entries,
total,
loading,
loadFirstPage,
loadMore,
fmtTime,
fmtDetails: fmtAuditDetails,
userLabel: auditUserLabel,
};
}