import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, } from "@/Components/ui/dialog"; import { Badge } from "@/Components/ui/badge"; import { ScrollArea } from "@/Components/ui/scroll-area"; interface Activity { id: number; description: string; subject_type: string; event: string; causer: string; created_at: string; properties: { attributes?: Record; old?: Record; }; } interface Props { open: boolean; onOpenChange: (open: boolean) => void; activity: Activity | null; } export default function ActivityDetailDialog({ open, onOpenChange, activity }: Props) { if (!activity) return null; const attributes = activity.properties?.attributes || {}; const old = activity.properties?.old || {}; // Get all keys from both attributes and old to ensure we show all changes const allKeys = Array.from(new Set([...Object.keys(attributes), ...Object.keys(old)])); // Filter out internal keys often logged but not useful for users const filteredKeys = allKeys.filter(key => !['created_at', 'updated_at', 'deleted_at', 'id'].includes(key) ); const getEventBadgeColor = (event: string) => { switch (event) { case 'created': return 'bg-green-500'; case 'updated': return 'bg-blue-500'; case 'deleted': return 'bg-red-500'; default: return 'bg-gray-500'; } }; const getEventLabel = (event: string) => { switch (event) { case 'created': return '新增'; case 'updated': return '更新'; case 'deleted': return '刪除'; default: return event; } }; const formatValue = (value: any) => { if (value === null || value === undefined) return -; if (typeof value === 'boolean') return value ? '是' : '否'; if (typeof value === 'object') return JSON.stringify(value); return String(value); }; return ( 操作詳情 {getEventLabel(activity.event)} {activity.created_at} 由 {activity.causer} 執行的操作
操作對象: {activity.subject_type}
描述: {activity.description}
{activity.event === 'created' ? (
已新增資料 (初始建立)
) : (
欄位
異動前
異動後
{filteredKeys.length > 0 ? (
{filteredKeys.map((key) => { const oldValue = old[key]; const newValue = attributes[key]; // Ensure we catch changes even if one value is missing/null // For deleted events, newValue might be empty, so we just show oldValue const isChanged = JSON.stringify(oldValue) !== JSON.stringify(newValue); return (
{key}
{formatValue(oldValue)}
{activity.event === 'deleted' ? '-' : formatValue(newValue)}
); })}
) : (
無詳細異動內容
)}
)}
); }