import { Dialog, DialogContent, DialogHeader, DialogTitle, } from '@/components/ui/dialog'; import { Badge } from '@/components/ui/badge'; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from '@/components/ui/table'; import type { FormHistory } from './columns'; type AttributeChangesDialogProps = { open: boolean; onOpenChange: (open: boolean) => void; item: FormHistory | null; }; const eventBadgeVariant: Record = { created: 'default', updated: 'secondary', deleted: 'destructive', }; const eventLabel: Record = { created: 'Ditambahkan', updated: 'Diperbarui', deleted: 'Dihapus', }; function renderValue(value: unknown): React.ReactNode { if (value === null || value === undefined) { return -; } if (typeof value === 'boolean') { return value ? 'Ya' : 'Tidak'; } if (Array.isArray(value)) { if (value.length === 0) { return -; } return value.join(', '); } if (typeof value === 'object') { return JSON.stringify(value); } return String(value); } function flattenChanges(attributeChanges: Record): { field: string; old?: unknown; new?: unknown }[] { const entries: { field: string; old?: unknown; new?: unknown }[] = []; for (const [key, change] of Object.entries(attributeChanges)) { entries.push({ field: key, old: change.old, new: change.new, }); } return entries; } export function AttributeChangesDialog({ open, onOpenChange, item }: AttributeChangesDialogProps) { if (!item) { return null; } const changes = item.attribute_changes; const hasChanges = changes && Object.keys(changes).length > 0; const flattened = hasChanges ? flattenChanges(changes) : []; return ( {item.description} {eventLabel[item.event] ?? item.event}
Oleh: {item.causer?.full_name ?? item.causer?.username ?? '-'}
{item.formatted_created_at}
{hasChanges && (
Field Lama Baru {flattened.map((row) => ( {row.field} {row.old !== undefined ? ( {renderValue(row.old)} ) : ( - )} {row.new !== undefined ? ( {renderValue(row.new)} ) : ( - )} ))}
)} {!hasChanges && (

Tidak ada perubahan data.

)}
); }