Files
faunosteo/apps/app/src/admin/labels.ts
T
bchanot 1758276087 refactor(app): sheet list hooks split, rows memoised, phone clear button
- useSearch holds the query, its field and clearSearch; useListView keeps
  the filters and the sort (8 bindings down to 5)
- the search term is folded once per search (sheetNameMatcher beside
  matchesSheetName, whose signature and behaviour stay), each date read
  once before the sort, the shown rows memoised on list, filters, search,
  sort and language
- long className strings hoisted into module constants, no class changed
- the no-result block's clear button takes the page's 44 px phone height
- labels.ts: the SheetSummary alias removed; ResultsProps declared first

No test changed: the existing unit suites are the oracle.
2026-11-16 17:57:18 +01:00

207 lines
5.8 KiB
TypeScript

import type {
CatalogSpecies,
OfficialSheetAdminSummary,
SheetAdminPerson,
SheetAdminRound,
SheetFailureKind,
} from '@faunosteo/supabase'
import type { Locale } from '@/i18n/types'
import { PARIS_TIME_ZONE } from '@/lib/dates'
type Translate = (key: string) => string
/**
* The failure kinds the administration screens and the draft editor's saves can
* meet (the RPCs they call, their helpers and the shared session kinds), each with an
* `admin.failure.<kind>` message. Any other kind cannot come from these
* calls; it reads as the generic `unavailable`, never as a raw code.
*/
const ADMIN_FAILURE_KINDS: ReadonlySet<SheetFailureKind> =
new Set<SheetFailureKind>([
'authentication_required',
'mfa_required',
'forbidden',
'rights.account',
'rights.capability',
'rights.mfa',
'rights.owner',
'not_found',
'grant.active',
'grant.ineligible',
'grant.revoked',
'grant.target_invalid',
'draft.frozen',
'draft.not_a_draft',
'round.open',
'view.asset.unknown',
'document.shape',
'document.schema_version',
'document.species',
'document.too_large',
'sheet.inactive',
'sheet.state',
'sheet.historical_read_only',
'sheet.species_invalid',
'sheet.slug_taken',
'species.exists',
'species.not_active',
'species.not_inactive',
'working_copy.source_invalid',
'working_copy.exists',
'working_copy.assets_missing',
'invalid_input',
'rate_limited',
'conflict_retry',
'session_expired',
'network',
'unavailable',
])
export function failureMessage(t: Translate, kind: SheetFailureKind): string {
return t(
`admin.failure.${ADMIN_FAILURE_KINDS.has(kind) ? kind : 'unavailable'}`,
)
}
/** A sheet or species name in the interface language. */
export function localized(
name: { fr: string; en: string },
locale: Locale,
): string {
return name[locale]
}
export function speciesName(species: CatalogSpecies, locale: Locale): string {
return locale === 'fr' ? species.name_fr : species.name_en
}
/** The species' name when the list knows it, else its id (never an empty cell). */
export function speciesLabel(
id: string,
species: readonly CatalogSpecies[],
locale: Locale,
): string {
const row = species.find((s) => s.id === id)
return row ? speciesName(row, locale) : id
}
export function personName(
t: Translate,
person: SheetAdminPerson | null,
): string {
return person?.display_name || t('admin.person.unnamed')
}
/** A server timestamp as a date in the interface language (in `timeZone` when given). */
export function formatDay(
iso: string,
locale: Locale,
timeZone?: string,
): string {
return new Date(iso).toLocaleDateString(locale, {
day: 'numeric',
month: 'long',
year: 'numeric',
...(timeZone ? { timeZone } : {}),
})
}
function shortDateTime(tag: string): Intl.DateTimeFormat {
return new Intl.DateTimeFormat(tag, {
day: 'numeric',
month: 'short',
year: 'numeric',
hour: '2-digit',
minute: '2-digit',
timeZone: PARIS_TIME_ZONE,
})
}
const SHORT_DATE_TIME: Record<Locale, Intl.DateTimeFormat> = {
fr: shortDateTime('fr-FR'),
en: shortDateTime('en-GB'),
}
/** « 9 nov. 2026, 12:06 » / « 9 Nov 2026, 12:06 », in Paris time (A8). */
export function formatShortParisDateTime(iso: string, locale: Locale): string {
return SHORT_DATE_TIME[locale].format(new Date(iso))
}
/** « version publiée 3 » or « aucune version publiée », then « brouillon version 4 » if any. */
function versionParts(
t: Translate,
sheet: OfficialSheetAdminSummary,
): string[] {
const number = (key: string, value: number) =>
t(key).replace('{number}', String(value))
const current = sheet.current_version
? number(
'admin.sheets.current_version',
sheet.current_version.version_number,
)
: t('admin.sheets.no_published')
const draft = sheet.draft_version
? [number('admin.sheets.draft', sheet.draft_version.version_number)]
: []
return [current, ...draft]
}
/** « revue n° 2 : À relire » when the sheet has a round. */
function roundPart(t: Translate, sheet: OfficialSheetAdminSummary): string[] {
if (!sheet.latest_round) return []
return [
t('admin.sheets.latest_round')
.replace('{round}', String(sheet.latest_round.round))
.replace('{status}', roundLabel(t, sheet.latest_round).title),
]
}
/** The detail line of a sheet row: « Cheval · Active · version publiée 3 · … » (board 28). */
export function sheetDetails(
t: Translate,
sheet: OfficialSheetAdminSummary,
species: readonly CatalogSpecies[],
locale: Locale,
): string {
return [
speciesLabel(sheet.species_id, species, locale),
t(`admin.sheet_status.${sheet.status}`),
...versionParts(t, sheet),
...roundPart(t, sheet),
].join(' · ')
}
/**
* The round's state (§3). A failed verification names its cause only when
* the server recorded one (`failure_outcome`, BDR-095): the title of that
* outcome and its ratified sentence; otherwise the generic status label.
*/
export function roundLabel(
t: Translate,
round: Pick<SheetAdminRound, 'status'> & {
failure_outcome?: SheetAdminRound['failure_outcome']
},
): { title: string; cause: string | null } {
if (round.status === 'verification_failed' && round.failure_outcome) {
return {
title: t(`admin.outcome.${round.failure_outcome}.title`),
cause: t(`admin.outcome.${round.failure_outcome}.body`),
}
}
return { title: t(`admin.round_status.${round.status}`), cause: null }
}
export const ROUND_TONE = {
verifying: 'neutral',
open: 'accent',
approved: 'accent',
changes_requested: 'warn',
withdrawn: 'neutral',
verification_failed: 'danger',
} as const
export const VERSION_TONE = {
draft: 'neutral',
published: 'accent',
deprecated: 'warn',
} as const