Files
Inventory_frontend/app/shared/utils/historyDisplayUtils.ts
Matthieu 767c9a7424 feat(versioning) : add entity versioning frontend with restore flow
- useEntityVersions composable (list, preview, restore API calls)
- EntityVersionList component with auto-refresh after save
- VersionRestoreModal with context-aware messages per entity type
- Integrate into machine, composant, piece, product detail pages
- Add restore action label to historyDisplayUtils
- Show structure slots in composant/piece consultation mode

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-26 14:58:39 +01:00

80 lines
2.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 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<string, unknown>
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<string, DiffChange> | null },
fieldLabels: Record<string, string>,
): 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),
}))
}