import type { GameStructure, User } from "@/payload-types"; import { Item, ItemContent, ItemHeader, ItemTitle } from "@/components/ui/item"; import { BookOpenTextIcon, ScrollTextIcon } from "lucide-react"; import { formatType } from "@/utils/event-log/formatType"; interface EventEntry { id: number; timestamp: string; type: string; message: string; system: boolean; actor?: number | User | null; structure?: number | GameStructure | null; } interface RecentEventsProps { events: EventEntry[]; currentUserId?: number; } function timeAgo(dateStr: string): string { const now = new Date(); const date = new Date(dateStr); const diffMs = now.getTime() - date.getTime(); const diffMins = Math.floor(diffMs / 60000); if (diffMins < 1) return "just now"; if (diffMins < 60) return `${diffMins}m ago`; const diffHours = Math.floor(diffMins / 60); if (diffHours < 24) return `${diffHours}h ago`; const diffDays = Math.floor(diffHours / 24); return `${diffDays}d ago`; } export function RecentEvents({ events, currentUserId }: RecentEventsProps) { if (events.length === 0) { return (
Recent Events

No recent events recorded.

); } return (
Recent Events
{events.map((event, index) => { const actor = typeof event.actor === "object" && event.actor !== null ? (event.actor as User) : null; const actorId = actor?.id ?? (typeof event.actor === "number" ? event.actor : null); const isYou = actorId != null && actorId === currentUserId; const actorName = actor?.displayName ?? actor?.username ?? null; const actorLabel = isYou ? "You" : actorName; const isNarrative = event.system === false; return (
{isNarrative && ( )}
{timeAgo(event.timestamp)} {actorLabel && ( {actorLabel} )} {formatType(event.type)}
{event.message}
{index < events.length - 1 &&
}
); })}
); }