diff --git a/apps/app/src/admin/labels.ts b/apps/app/src/admin/labels.ts index 5fe43009..d5b7694a 100644 --- a/apps/app/src/admin/labels.ts +++ b/apps/app/src/admin/labels.ts @@ -126,10 +126,11 @@ export function formatShortParisDateTime(iso: string, locale: Locale): string { return SHORT_DATE_TIME[locale].format(new Date(iso)) } -type SheetSummary = OfficialSheetAdminSummary - /** « version publiée 3 » or « aucune version publiée », then « brouillon version 4 » if any. */ -function versionParts(t: Translate, sheet: SheetSummary): string[] { +function versionParts( + t: Translate, + sheet: OfficialSheetAdminSummary, +): string[] { const number = (key: string, value: number) => t(key).replace('{number}', String(value)) const current = sheet.current_version @@ -145,7 +146,7 @@ function versionParts(t: Translate, sheet: SheetSummary): string[] { } /** « revue n° 2 : À relire » when the sheet has a round. */ -function roundPart(t: Translate, sheet: SheetSummary): string[] { +function roundPart(t: Translate, sheet: OfficialSheetAdminSummary): string[] { if (!sheet.latest_round) return [] return [ t('admin.sheets.latest_round') @@ -157,7 +158,7 @@ function roundPart(t: Translate, sheet: SheetSummary): string[] { /** The detail line of a sheet row: « Cheval · Active · version publiée 3 · … » (board 28). */ export function sheetDetails( t: Translate, - sheet: SheetSummary, + sheet: OfficialSheetAdminSummary, species: readonly CatalogSpecies[], locale: Locale, ): string { diff --git a/apps/app/src/admin/sheetList.ts b/apps/app/src/admin/sheetList.ts index 4ad57c3d..26b579c8 100644 --- a/apps/app/src/admin/sheetList.ts +++ b/apps/app/src/admin/sheetList.ts @@ -1,5 +1,5 @@ import type { OfficialSheetAdminSummary } from '@faunosteo/supabase' -import { matchesSheetName } from '@/catalogue/useCatalogue' +import { sheetNameMatcher } from '@/catalogue/useCatalogue' import type { Locale } from '@/i18n/types' // shortcut: search and sort run in the browser on the whole list; they move to @@ -13,7 +13,8 @@ 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)) + const matches = sheetNameMatcher(query) + return sheets.filter((sheet) => matches(sheet.name)) } /** @@ -27,8 +28,15 @@ export function sortSheets( 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 collator = new Intl.Collator(locale, { + sensitivity: 'base', + numeric: true, + }) + // Each date read once, before the sort, rather than twice per comparison. + const times = new Map( + sheets.map((sheet) => [sheet, Date.parse(sheet.last_modified_at)]), + ) + const time = (sheet: Sheet) => times.get(sheet) ?? 0 const byName = (a: Sheet, b: Sheet) => collator.compare(a.name[locale], b.name[locale]) || a.sheet_id.localeCompare(b.sheet_id) diff --git a/apps/app/src/app/routes/app/admin/AdminSheetsRoute.tsx b/apps/app/src/app/routes/app/admin/AdminSheetsRoute.tsx index 4d637e6f..94621a97 100644 --- a/apps/app/src/app/routes/app/admin/AdminSheetsRoute.tsx +++ b/apps/app/src/app/routes/app/admin/AdminSheetsRoute.tsx @@ -1,4 +1,4 @@ -import { useRef, useState, type KeyboardEvent } from 'react' +import { useMemo, useRef, useState, type KeyboardEvent } from 'react' import { Link } from 'react-router-dom' import { listCatalogSpecies, @@ -47,6 +47,32 @@ import { useT } from '@/i18n/useT' type Sheet = OfficialSheetAdminSummary +const ROW_LINK = + 'flex min-h-11 flex-col gap-1 rounded-xl px-4 py-3 hover:bg-[var(--color-surface-strong)] ' + + 'focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[var(--color-accent)] ' + + 'sm:grid sm:grid-cols-[minmax(0,1fr)_auto] sm:gap-x-4' + +const ROW_DATE = + 'text-sm text-[var(--color-ink-muted)] sm:col-start-2 sm:row-start-1 sm:whitespace-nowrap ' + + 'sm:text-right' + +const CLEAR_BUTTON = + 'absolute right-0 top-0 flex size-10 max-sm:size-11 items-center justify-center rounded-lg ' + + 'text-[var(--color-ink-muted)] focus-visible:outline-none focus-visible:ring-2 ' + + 'focus-visible:ring-[var(--color-accent)]' + +const EMPTY_LINE = + 'rounded-2xl border border-dashed border-[var(--color-border)] p-6 text-center ' + + 'text-[var(--color-ink)]' + +const NO_RESULT = + 'space-y-2 rounded-lg border border-[var(--color-border)] bg-[var(--color-surface-strong)] ' + + 'p-3 text-sm' + +const LIST = + 'divide-y divide-[var(--color-border)] rounded-2xl border border-[var(--color-border)] ' + + 'bg-[var(--color-surface)]' + /** The list and the species names, in one read: both are capability reads at aal2. */ async function loadSheets(client: FaunosteoClient) { const [sheets, species] = await Promise.all([ @@ -56,30 +82,33 @@ async function loadSheets(client: FaunosteoClient) { return { sheets, species } } -/** What the page shows of the loaded list: the four filters, the search and the sort. */ -function useListView() { - const [filters, setFilters] = useState(NO_FILTER) +/** The search: its text, its field, and the one way of clearing it. */ +function useSearch() { const [query, setQuery] = useState('') - const [sort, setSort] = useState('modified') const searchRef = useRef(null) /** Every way of clearing (« × », Escape, the no-result button) gives the field the focus back. */ const clearSearch = () => { setQuery('') searchRef.current?.focus() } - return { - filters, - setFilters, - query, - setQuery, - sort, - setSort, - searchRef, - clearSearch, - } + return { query, setQuery, searchRef, clearSearch } +} + +/** What the page shows of the loaded list: the four filters, the search and the sort. */ +function useListView() { + const [filters, setFilters] = useState(NO_FILTER) + const [sort, setSort] = useState('modified') + const search = useSearch() + return { filters, setFilters, sort, setSort, ...search } } type ListView = ReturnType +interface ResultsProps { + sheets: Sheet[] + species: CatalogSpecies[] + view: ListView +} + function SheetRow({ sheet, species, @@ -91,10 +120,7 @@ function SheetRow({ const locale = useLocaleStore((s) => s.locale) return (
  • - + {localized(sheet.name, locale)} @@ -104,7 +130,7 @@ function SheetRow({ {sheetDetails(t, sheet, species, locale)} - + @@ -143,7 +169,7 @@ function SearchField({ view }: { view: ListView }) { type="button" aria-label={t('admin.sheets.clear_search')} onClick={clearSearch} - className="absolute right-0 top-0 flex size-10 max-sm:size-11 items-center justify-center rounded-lg text-[var(--color-ink-muted)] focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[var(--color-accent)]" + className={CLEAR_BUTTON} > @@ -274,10 +300,7 @@ function CountLine({ shown, total }: { shown: number; total: number }) { function EmptyLine({ text }: { text: string }) { return ( -

    +

    {text}

    ) @@ -290,7 +313,7 @@ function EmptyLine({ text }: { text: string }) { function SearchNoResult({ view }: { view: ListView }) { const t = useT() return ( -
    +

    {t('admin.sheets.no_result').replace('{q}', () => view.query.trim())} @@ -301,6 +324,7 @@ function SearchNoResult({ view }: { view: ListView }) { variant="secondary" size="sm" onClick={view.clearSearch} + className="max-sm:h-11" > {t('admin.sheets.clear_search')} @@ -309,23 +333,26 @@ function SearchNoResult({ view }: { view: ListView }) { ) } -interface ResultsProps { - sheets: Sheet[] - species: CatalogSpecies[] - view: ListView +/** The rows shown, computed again only when the list, filters, search, sort or language change. */ +function useShownSheets(sheets: Sheet[], view: ListView): Sheet[] { + const locale = useLocaleStore((s) => s.locale) + const { filters, query, sort } = view + return useMemo( + () => + sortSheets( + searchSheets(filterSheets(sheets, filters), query), + sort, + locale, + ), + [sheets, filters, query, sort, locale], + ) } /** The count line and the rows, or the empty state that fits (board 28 note). */ function Results({ sheets, species, view }: ResultsProps) { const t = useT() - const locale = useLocaleStore((s) => s.locale) + const shown = useShownSheets(sheets, view) if (sheets.length === 0) return - const filtered = filterSheets(sheets, view.filters) - const shown = sortSheets( - searchSheets(filtered, view.query), - view.sort, - locale, - ) const filtering = Object.values(view.filters).some(Boolean) return (

    @@ -337,10 +364,7 @@ function Results({ sheets, species, view }: ResultsProps) { ))} {shown.length > 0 && ( -
      +
        {shown.map((sheet) => ( ))} diff --git a/apps/app/src/catalogue/useCatalogue.ts b/apps/app/src/catalogue/useCatalogue.ts index fbc443df..6392ea6e 100644 --- a/apps/app/src/catalogue/useCatalogue.ts +++ b/apps/app/src/catalogue/useCatalogue.ts @@ -54,10 +54,23 @@ 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 { +/** The search folded once, then tested on each name: what a long list needs per keystroke. */ +export function sheetNameMatcher( + query: string, +): (name: { fr: string; en: string }) => boolean { const term = folded(query.trim()) - return term === '' || folded(name.fr).includes(term) || folded(name.en).includes(term) + return (name) => + term === '' || + folded(name.fr).includes(term) || + folded(name.en).includes(term) +} + +/** 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 { + return sheetNameMatcher(query)(name) } /** Search by sheet name only, French or English (BDR-095), then the species chip. */