refactor(front): extract shared utils and rewire pages

This commit is contained in:
Matthieu
2026-02-06 17:16:16 +01:00
parent 1fbd1d1b2e
commit 9ee348fff0
36 changed files with 1686 additions and 2194 deletions

View File

@@ -0,0 +1,78 @@
/**
* 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'
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),
}))
}