/** * History display utilities for edit pages. * * Extracted from pages/component/[id]/edit.vue, pieces/[id]/edit.vue, * product/[id]/edit.vue – each had an identical copy. */ // --------------------------------------------------------------------------- // Formatters // --------------------------------------------------------------------------- export const historyActionLabel = (action: string): string => { if (action === 'create') return 'Création' if (action === 'delete') return 'Suppression' if (action === 'restore') return 'Restauration' return 'Modification' } const historyDateFormatter = new Intl.DateTimeFormat('fr-FR', { dateStyle: 'medium', timeStyle: 'short', }) export const formatHistoryDate = (value: string): string => { const date = new Date(value) if (Number.isNaN(date.getTime())) return value return historyDateFormatter.format(date) } export const formatHistoryValue = (value: unknown): string => { if (value === null || value === undefined || value === '') return '—' if (Array.isArray(value)) { if (value.length === 0) return '—' return value.map((item) => formatHistoryValue(item)).join(', ') } if (typeof value === 'object') { const maybeRecord = value as Record const name = typeof maybeRecord.name === 'string' ? maybeRecord.name : null const id = typeof maybeRecord.id === 'string' ? maybeRecord.id : null if (name && id) return `${name} (#${id})` if (name) return name if (id) return `#${id}` try { return JSON.stringify(value) } catch { return String(value) } } return String(value) } // --------------------------------------------------------------------------- // Diff entries // --------------------------------------------------------------------------- interface DiffChange { from?: unknown to?: unknown } export interface HistoryDiffEntry { field: string label: string fromLabel: string toLabel: string } export const historyDiffEntries = ( entry: { diff?: Record | null }, fieldLabels: Record, ): HistoryDiffEntry[] => { const diff = entry.diff ?? {} return Object.entries(diff).map(([field, change]) => ({ field, label: fieldLabels[field] ?? field, fromLabel: formatHistoryValue(change?.from), toLabel: formatHistoryValue(change?.to), })) }