diff --git a/apps/app/src/admin/labels.ts b/apps/app/src/admin/labels.ts index a0ab8ce4..646958b5 100644 --- a/apps/app/src/admin/labels.ts +++ b/apps/app/src/admin/labels.ts @@ -5,6 +5,7 @@ import type { SheetFailureKind, } from '@faunosteo/supabase' import type { Locale } from '@/i18n/types' +import { PARIS_TIME_ZONE } from '@/lib/dates' type Translate = (key: string) => string @@ -104,6 +105,26 @@ export function formatDay( }) } +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 = { + 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)) +} + /** * 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 diff --git a/apps/app/src/admin/sheetList.ts b/apps/app/src/admin/sheetList.ts new file mode 100644 index 00000000..4ad57c3d --- /dev/null +++ b/apps/app/src/admin/sheetList.ts @@ -0,0 +1,38 @@ +import type { OfficialSheetAdminSummary } from '@faunosteo/supabase' +import { matchesSheetName } from '@/catalogue/useCatalogue' +import type { Locale } from '@/i18n/types' + +// shortcut: search and sort run in the browser on the whole list; they move to +// the server with the list's paging (RR-13, TODO.md). + +/** The display orders of the admin list; `modified` is the default (D7). */ +export const SHEET_SORTS = ['modified', 'name'] as const +export type SheetSort = (typeof SHEET_SORTS)[number] + +type Sheet = OfficialSheetAdminSummary + +/** Search on the sheet name only, as the catalogue does (D7). */ +export function searchSheets(sheets: readonly Sheet[], query: string): Sheet[] { + return sheets.filter((sheet) => matchesSheetName(sheet.name, query)) +} + +/** + * Display order only: a new array, the input untouched. `modified` puts the + * newest `last_modified_at` first; equal dates, and `name`, follow the name in + * the interface language, then the id, so the order never depends on the + * server's. + */ +export function sortSheets( + sheets: readonly Sheet[], + sort: SheetSort, + locale: Locale, +): Sheet[] { + const collator = new Intl.Collator(locale, { sensitivity: 'base', numeric: true }) + const time = (sheet: Sheet) => Date.parse(sheet.last_modified_at) + const byName = (a: Sheet, b: Sheet) => + collator.compare(a.name[locale], b.name[locale]) || + a.sheet_id.localeCompare(b.sheet_id) + return [...sheets].sort( + (a, b) => (sort === 'modified' ? time(b) - time(a) : 0) || byName(a, b), + ) +} diff --git a/apps/app/src/catalogue/useCatalogue.ts b/apps/app/src/catalogue/useCatalogue.ts index b32d9798..fbc443df 100644 --- a/apps/app/src/catalogue/useCatalogue.ts +++ b/apps/app/src/catalogue/useCatalogue.ts @@ -54,19 +54,22 @@ export function folded(text: string): string { return text.normalize('NFD').replace(/\p{Diacritic}/gu, '').toLowerCase() } +/** A sheet name contains the search, in French or in English, accents and case ignored. */ +export function matchesSheetName(name: { fr: string; en: string }, query: string): boolean { + const term = folded(query.trim()) + return term === '' || folded(name.fr).includes(term) || folded(name.en).includes(term) +} + /** Search by sheet name only, French or English (BDR-095), then the species chip. */ export function filterCatalog( entries: readonly CatalogEntry[], query: string, speciesId: string | null, ): CatalogEntry[] { - const term = folded(query.trim()) return entries.filter( (entry) => (speciesId === null || entry.species_id === speciesId) && - (term === '' || - folded(entry.name.fr).includes(term) || - folded(entry.name.en).includes(term)), + matchesSheetName(entry.name, query), ) } diff --git a/apps/app/tests/unit/admin.labels.test.ts b/apps/app/tests/unit/admin.labels.test.ts index b53c2d2c..213e5c8f 100644 --- a/apps/app/tests/unit/admin.labels.test.ts +++ b/apps/app/tests/unit/admin.labels.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from 'vitest' -import { failureMessage } from '@/admin/labels' +import { failureMessage, formatShortParisDateTime } from '@/admin/labels' import { en } from '@/i18n/en' import { fr } from '@/i18n/fr' @@ -16,3 +16,13 @@ describe('failure messages of the submission', () => { }, ) }) + +describe('formatShortParisDateTime (A8)', () => { + it('reads « 9 nov. 2026, 12:06 » in French and « 9 Nov 2026, 12:06 » in English, in Paris time', () => { + expect(formatShortParisDateTime('2026-11-09T11:06:00.123456+00:00', 'fr')).toBe('9 nov. 2026, 12:06') + expect(formatShortParisDateTime('2026-11-09T11:06:00.123456+00:00', 'en')).toBe('9 Nov 2026, 12:06') + }) + it('follows the Paris summer offset', () => { + expect(formatShortParisDateTime('2026-07-01T22:30:00+00:00', 'fr')).toBe('2 juil. 2026, 00:30') + }) +}) diff --git a/apps/app/tests/unit/admin.sheetList.test.ts b/apps/app/tests/unit/admin.sheetList.test.ts new file mode 100644 index 00000000..4eeaf28b --- /dev/null +++ b/apps/app/tests/unit/admin.sheetList.test.ts @@ -0,0 +1,44 @@ +import { describe, expect, it } from 'vitest' +import type { OfficialSheetAdminSummary } from '@faunosteo/supabase' +import { searchSheets, sortSheets } from '@/admin/sheetList' + +const sheet = (id: string, fr: string, en: string, at: string) => + ({ sheet_id: id, name: { fr, en }, last_modified_at: at }) as OfficialSheetAdminSummary +const A = sheet('a', 'Âne, squelette', 'Donkey, skeleton', '2026-11-03T10:02:00+00:00') +const C = sheet('c', 'Cheval (tête)', 'Horse (head)', '2026-11-09T11:06:00.123456+00:00') +const E = sheet('e', 'Émeu', 'Emu', '2026-11-05T15:20:00+00:00') +const Z = sheet('z', 'Zèbre', 'Zebra', '2026-11-05T15:20:00+00:00') +const ids = (list: OfficialSheetAdminSummary[]) => list.map((s) => s.sheet_id).join('') + +describe('searchSheets', () => { + it('matches the French or the English name, accents and case ignored', () => { + expect(ids(searchSheets([A, C, E, Z], 'ane'))).toBe('a') + expect(ids(searchSheets([A, C, E, Z], 'HORSE'))).toBe('c') + expect(ids(searchSheets([A, C, E, Z], 'émeu'))).toBe('e') + }) + it('ignores surrounding spaces; an empty search keeps every sheet; brackets are plain text', () => { + expect(ids(searchSheets([A, C], ' cheval '))).toBe('c') + expect(ids(searchSheets([A, C], ' '))).toBe('ac') + expect(ids(searchSheets([A, C], '(tête)'))).toBe('c') + }) +}) + +describe('sortSheets', () => { + it('modified: newest first, equal dates by name', () => { + expect(ids(sortSheets([A, Z, E, C], 'modified', 'fr'))).toBe('ceza') + }) + it('name: the collation of the interface language, not code points', () => { + expect(ids(sortSheets([Z, E, C, A], 'name', 'fr'))).toBe('acez') + expect(ids(sortSheets([Z, E, C, A], 'name', 'en'))).toBe('aecz') + }) + it('equal names fall back to the id, whatever the input order', () => { + const twin = { ...A, sheet_id: 'b' } + expect(ids(sortSheets([twin, A], 'name', 'fr'))).toBe('ab') + expect(ids(sortSheets([A, twin], 'modified', 'fr'))).toBe('ab') + }) + it('returns a new array and leaves its input alone', () => { + const input = [Z, A] + expect(sortSheets(input, 'name', 'fr')).not.toBe(input) + expect(ids(input)).toBe('za') + }) +})