import type { ActivityLog } from '../api/types'; import { Badge, type BadgeColor } from './ui'; import i18n from '../i18n'; import { fmtDate, fmtDateTime, fmtTime } from '../lib/format'; const t = (key: string, options?: Record): 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(' ') || 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 (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 (
{logs.map((log) => ( ))}
{t('colDate')} {t('colUser')} {t('colAction')} {t('colEntity')} {t('colDetails')}
{fmtDate(log.createdAt)}
{fmtTime(log.createdAt)}
{actorName(log)}
{log.actor.email}
{log.actor.id}
{actionLabel(log.action)}
{t(`entities.${log.entityType}`, { defaultValue: log.entityType })}
{entityName(log) && (
{entityName(log)}
)}
{detailsLines(log).length > 0 ? (
{detailsLines(log).map((line) => (
{line}
))}
) : ( '—' )}
); }