152 lines
5.8 KiB
TypeScript
152 lines
5.8 KiB
TypeScript
import type { ActivityLog } from '../api/types';
|
|
import { Badge, type BadgeColor } from './ui';
|
|
import i18n from '../i18n';
|
|
import { fmtDate, fmtDateTime, fmtTime, maskEmail } from '../lib/format';
|
|
|
|
const t = (key: string, options?: Record<string, unknown>): string =>
|
|
String(i18n.t(`logs:${key}`, options as never));
|
|
|
|
export function actionLabel(action: string) {
|
|
return t(`actions.${action}`, { defaultValue: action });
|
|
}
|
|
|
|
export function actionColor(action: string): BadgeColor {
|
|
if (action.startsWith('plan.')) return 'blue';
|
|
if (action.startsWith('session.')) return 'green';
|
|
if (action.startsWith('gym.')) return 'gray';
|
|
if (action.startsWith('coaching.')) return 'red';
|
|
// akcje security konta — tak samo wyraźne jak relacje trenerskie
|
|
if (action.startsWith('user.')) return 'red';
|
|
if (action.startsWith('membership.')) return 'blue';
|
|
return 'yellow';
|
|
}
|
|
|
|
export function formatLogDate(iso: string) {
|
|
return fmtDateTime(iso);
|
|
}
|
|
|
|
export function actorName(log: ActivityLog) {
|
|
const { firstName, lastName, email } = log.actor;
|
|
return [firstName, lastName].filter(Boolean).join(' ') || maskEmail(email);
|
|
}
|
|
|
|
/** Nazwa obiektu z szczegółów wpisu (tytuł planu, nazwa siłowni, nazwa obwodu). */
|
|
function entityName(log: ActivityLog) {
|
|
const d = log.details;
|
|
if (!d) return null;
|
|
return (d.title ?? d.name ?? d.gymName ?? null) as string | null;
|
|
}
|
|
|
|
/** Klucze pokazywane w innych kolumnach (aktor w „Użytkownik", nazwa w „Obiekt"). */
|
|
const HIDDEN_DETAIL_KEYS = new Set(['actorId', 'title', 'name', 'gymName']);
|
|
|
|
function formatDetailValue(log: ActivityLog, key: string, value: unknown): string {
|
|
if (key === 'email' && typeof value === 'string') return maskEmail(value);
|
|
if (Array.isArray(value)) return value.map((v) => String(v)).join(', ');
|
|
if (value && typeof value === 'object') {
|
|
return Object.entries(value)
|
|
.map(([k, v]) => `${k}: ${formatDetailValue(log, k, v)}`)
|
|
.join(', ');
|
|
}
|
|
let text = String(value);
|
|
if (key === 'value' && typeof value === 'number') {
|
|
const unit =
|
|
log.action === 'weight.added' || log.action === 'weight.removed'
|
|
? ' kg'
|
|
: log.action === 'circumference.added' || log.action === 'circumference.removed'
|
|
? ' cm'
|
|
: '';
|
|
text += unit;
|
|
}
|
|
return text;
|
|
}
|
|
|
|
/** Szczegóły wpisu jako linie: `start: 2026-08-24`, `wartość: 82.5 kg` — jedna pod drugą. */
|
|
export function detailsLines(log: ActivityLog): string[] {
|
|
const d = log.details;
|
|
if (!d) return [];
|
|
return Object.entries(d)
|
|
.filter(([k]) => !HIDDEN_DETAIL_KEYS.has(k))
|
|
.map(([k, v]) => `${t(`details.${k}`, { defaultValue: k })}: ${formatDetailValue(log, k, v)}`);
|
|
}
|
|
|
|
/** Szczegóły jako jeden tekst: `start: 2026-08-24 · wartość: 82.5 kg`. */
|
|
export function detailsText(log: ActivityLog) {
|
|
const parts = detailsLines(log);
|
|
return parts.length > 0 ? parts.join(' · ') : null;
|
|
}
|
|
|
|
/** Tabela logów aktywności (wspólny rendering: globalne logi i logi pojedynczej siłowni). */
|
|
export function LogTable({ logs }: { logs: ActivityLog[] }) {
|
|
return (
|
|
<div className="overflow-x-auto rounded-[10px] border border-line bg-(image:--card-grad) shadow-card">
|
|
<table className="w-full text-[0.95rem]">
|
|
<thead>
|
|
<tr className="border-b border-line text-left text-dim">
|
|
<th className="px-4 py-3 text-[10.5px] font-bold uppercase tracking-[0.11em]">
|
|
{t('colDate')}
|
|
</th>
|
|
<th className="px-4 py-3 text-[10.5px] font-bold uppercase tracking-[0.11em]">
|
|
{t('colUser')}
|
|
</th>
|
|
<th className="px-4 py-3 text-[10.5px] font-bold uppercase tracking-[0.11em]">
|
|
{t('colAction')}
|
|
</th>
|
|
<th className="px-4 py-3 text-[10.5px] font-bold uppercase tracking-[0.11em]">
|
|
{t('colEntity')}
|
|
</th>
|
|
<th className="px-4 py-3 text-[10.5px] font-bold uppercase tracking-[0.11em]">
|
|
{t('colDetails')}
|
|
</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
{logs.map((log) => (
|
|
<tr
|
|
key={log.id}
|
|
className="border-b border-line/60 transition-colors last:border-b-0 hover:bg-line/20"
|
|
>
|
|
<td className="px-4 py-3 align-top font-display text-dim tabular-nums">
|
|
<div className="whitespace-nowrap">{fmtDate(log.createdAt)}</div>
|
|
<div className="whitespace-nowrap text-[0.875rem]">{fmtTime(log.createdAt)}</div>
|
|
</td>
|
|
<td className="px-4 py-3">
|
|
<div className="font-semibold">{actorName(log)}</div>
|
|
<div className="text-[0.875rem] text-dim">{maskEmail(log.actor.email)}</div>
|
|
<div className="font-mono text-[0.75rem] text-dim/70" title={log.actor.id}>
|
|
{log.actor.id}
|
|
</div>
|
|
</td>
|
|
<td className="px-4 py-3 align-top">
|
|
<Badge
|
|
color={actionColor(log.action)}
|
|
className="h-auto max-w-[13rem] whitespace-normal"
|
|
>
|
|
{actionLabel(log.action)}
|
|
</Badge>
|
|
</td>
|
|
<td className="px-4 py-3">
|
|
<div>{t(`entities.${log.entityType}`, { defaultValue: log.entityType })}</div>
|
|
{entityName(log) && (
|
|
<div className="text-[0.875rem] text-dim">{entityName(log)}</div>
|
|
)}
|
|
</td>
|
|
<td className="px-4 py-3 text-dim">
|
|
{detailsLines(log).length > 0 ? (
|
|
<div className="space-y-1">
|
|
{detailsLines(log).map((line) => (
|
|
<div key={line}>{line}</div>
|
|
))}
|
|
</div>
|
|
) : (
|
|
'—'
|
|
)}
|
|
</td>
|
|
</tr>
|
|
))}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
);
|
|
}
|