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.
This commit is contained in:
1 parent
374e300895
commit
1758276087
4 files changed
+99
-53
No files matched your search
@@ -126,10 +126,11 @@ export function formatShortParisDateTime(iso: string, locale: Locale): string {
|
|||||||
return SHORT_DATE_TIME[locale].format(new Date(iso))
|
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. */
|
/** « 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) =>
|
const number = (key: string, value: number) =>
|
||||||
t(key).replace('{number}', String(value))
|
t(key).replace('{number}', String(value))
|
||||||
const current = sheet.current_version
|
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. */
|
/** « 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 []
|
if (!sheet.latest_round) return []
|
||||||
return [
|
return [
|
||||||
t('admin.sheets.latest_round')
|
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). */
|
/** The detail line of a sheet row: « Cheval · Active · version publiée 3 · … » (board 28). */
|
||||||
export function sheetDetails(
|
export function sheetDetails(
|
||||||
t: Translate,
|
t: Translate,
|
||||||
sheet: SheetSummary,
|
sheet: OfficialSheetAdminSummary,
|
||||||
species: readonly CatalogSpecies[],
|
species: readonly CatalogSpecies[],
|
||||||
locale: Locale,
|
locale: Locale,
|
||||||
): string {
|
): string {
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import type { OfficialSheetAdminSummary } from '@faunosteo/supabase'
|
import type { OfficialSheetAdminSummary } from '@faunosteo/supabase'
|
||||||
import { matchesSheetName } from '@/catalogue/useCatalogue'
|
import { sheetNameMatcher } from '@/catalogue/useCatalogue'
|
||||||
import type { Locale } from '@/i18n/types'
|
import type { Locale } from '@/i18n/types'
|
||||||
|
|
||||||
// shortcut: search and sort run in the browser on the whole list; they move to
|
// 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). */
|
/** Search on the sheet name only, as the catalogue does (D7). */
|
||||||
export function searchSheets(sheets: readonly Sheet[], query: string): Sheet[] {
|
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,
|
sort: SheetSort,
|
||||||
locale: Locale,
|
locale: Locale,
|
||||||
): Sheet[] {
|
): Sheet[] {
|
||||||
const collator = new Intl.Collator(locale, { sensitivity: 'base', numeric: true })
|
const collator = new Intl.Collator(locale, {
|
||||||
const time = (sheet: Sheet) => Date.parse(sheet.last_modified_at)
|
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) =>
|
const byName = (a: Sheet, b: Sheet) =>
|
||||||
collator.compare(a.name[locale], b.name[locale]) ||
|
collator.compare(a.name[locale], b.name[locale]) ||
|
||||||
a.sheet_id.localeCompare(b.sheet_id)
|
a.sheet_id.localeCompare(b.sheet_id)
|
||||||
|
|||||||
@@ -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 { Link } from 'react-router-dom'
|
||||||
import {
|
import {
|
||||||
listCatalogSpecies,
|
listCatalogSpecies,
|
||||||
@@ -47,6 +47,32 @@ import { useT } from '@/i18n/useT'
|
|||||||
|
|
||||||
type Sheet = OfficialSheetAdminSummary
|
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. */
|
/** The list and the species names, in one read: both are capability reads at aal2. */
|
||||||
async function loadSheets(client: FaunosteoClient) {
|
async function loadSheets(client: FaunosteoClient) {
|
||||||
const [sheets, species] = await Promise.all([
|
const [sheets, species] = await Promise.all([
|
||||||
@@ -56,30 +82,33 @@ async function loadSheets(client: FaunosteoClient) {
|
|||||||
return { sheets, species }
|
return { sheets, species }
|
||||||
}
|
}
|
||||||
|
|
||||||
/** What the page shows of the loaded list: the four filters, the search and the sort. */
|
/** The search: its text, its field, and the one way of clearing it. */
|
||||||
function useListView() {
|
function useSearch() {
|
||||||
const [filters, setFilters] = useState<SheetFilters>(NO_FILTER)
|
|
||||||
const [query, setQuery] = useState('')
|
const [query, setQuery] = useState('')
|
||||||
const [sort, setSort] = useState<SheetSort>('modified')
|
|
||||||
const searchRef = useRef<HTMLInputElement>(null)
|
const searchRef = useRef<HTMLInputElement>(null)
|
||||||
/** Every way of clearing (« × », Escape, the no-result button) gives the field the focus back. */
|
/** Every way of clearing (« × », Escape, the no-result button) gives the field the focus back. */
|
||||||
const clearSearch = () => {
|
const clearSearch = () => {
|
||||||
setQuery('')
|
setQuery('')
|
||||||
searchRef.current?.focus()
|
searchRef.current?.focus()
|
||||||
}
|
}
|
||||||
return {
|
return { query, setQuery, searchRef, clearSearch }
|
||||||
filters,
|
}
|
||||||
setFilters,
|
|
||||||
query,
|
/** What the page shows of the loaded list: the four filters, the search and the sort. */
|
||||||
setQuery,
|
function useListView() {
|
||||||
sort,
|
const [filters, setFilters] = useState<SheetFilters>(NO_FILTER)
|
||||||
setSort,
|
const [sort, setSort] = useState<SheetSort>('modified')
|
||||||
searchRef,
|
const search = useSearch()
|
||||||
clearSearch,
|
return { filters, setFilters, sort, setSort, ...search }
|
||||||
}
|
|
||||||
}
|
}
|
||||||
type ListView = ReturnType<typeof useListView>
|
type ListView = ReturnType<typeof useListView>
|
||||||
|
|
||||||
|
interface ResultsProps {
|
||||||
|
sheets: Sheet[]
|
||||||
|
species: CatalogSpecies[]
|
||||||
|
view: ListView
|
||||||
|
}
|
||||||
|
|
||||||
function SheetRow({
|
function SheetRow({
|
||||||
sheet,
|
sheet,
|
||||||
species,
|
species,
|
||||||
@@ -91,10 +120,7 @@ function SheetRow({
|
|||||||
const locale = useLocaleStore((s) => s.locale)
|
const locale = useLocaleStore((s) => s.locale)
|
||||||
return (
|
return (
|
||||||
<li>
|
<li>
|
||||||
<Link
|
<Link to={`/app/admin/sheets/${sheet.sheet_id}`} className={ROW_LINK}>
|
||||||
to={`/app/admin/sheets/${sheet.sheet_id}`}
|
|
||||||
className="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"
|
|
||||||
>
|
|
||||||
<span className="flex min-w-0 flex-wrap items-center gap-2">
|
<span className="flex min-w-0 flex-wrap items-center gap-2">
|
||||||
<span className="min-w-0 font-medium text-[var(--color-ink)] [overflow-wrap:anywhere]">
|
<span className="min-w-0 font-medium text-[var(--color-ink)] [overflow-wrap:anywhere]">
|
||||||
{localized(sheet.name, locale)}
|
{localized(sheet.name, locale)}
|
||||||
@@ -104,7 +130,7 @@ function SheetRow({
|
|||||||
<span className="text-sm text-[var(--color-ink-muted)] sm:col-start-1">
|
<span className="text-sm text-[var(--color-ink-muted)] sm:col-start-1">
|
||||||
{sheetDetails(t, sheet, species, locale)}
|
{sheetDetails(t, sheet, species, locale)}
|
||||||
</span>
|
</span>
|
||||||
<span className="text-sm text-[var(--color-ink-muted)] sm:col-start-2 sm:row-start-1 sm:whitespace-nowrap sm:text-right">
|
<span className={ROW_DATE}>
|
||||||
<ModifiedDate iso={sheet.last_modified_at} />
|
<ModifiedDate iso={sheet.last_modified_at} />
|
||||||
</span>
|
</span>
|
||||||
</Link>
|
</Link>
|
||||||
@@ -143,7 +169,7 @@ function SearchField({ view }: { view: ListView }) {
|
|||||||
type="button"
|
type="button"
|
||||||
aria-label={t('admin.sheets.clear_search')}
|
aria-label={t('admin.sheets.clear_search')}
|
||||||
onClick={clearSearch}
|
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}
|
||||||
>
|
>
|
||||||
<Icon name="close" size={16} />
|
<Icon name="close" size={16} />
|
||||||
</button>
|
</button>
|
||||||
@@ -274,10 +300,7 @@ function CountLine({ shown, total }: { shown: number; total: number }) {
|
|||||||
|
|
||||||
function EmptyLine({ text }: { text: string }) {
|
function EmptyLine({ text }: { text: string }) {
|
||||||
return (
|
return (
|
||||||
<p
|
<p role="status" className={EMPTY_LINE}>
|
||||||
role="status"
|
|
||||||
className="rounded-2xl border border-dashed border-[var(--color-border)] p-6 text-center text-[var(--color-ink)]"
|
|
||||||
>
|
|
||||||
{text}
|
{text}
|
||||||
</p>
|
</p>
|
||||||
)
|
)
|
||||||
@@ -290,7 +313,7 @@ function EmptyLine({ text }: { text: string }) {
|
|||||||
function SearchNoResult({ view }: { view: ListView }) {
|
function SearchNoResult({ view }: { view: ListView }) {
|
||||||
const t = useT()
|
const t = useT()
|
||||||
return (
|
return (
|
||||||
<div className="space-y-2 rounded-lg border border-[var(--color-border)] bg-[var(--color-surface-strong)] p-3 text-sm">
|
<div className={NO_RESULT}>
|
||||||
<p className="[overflow-wrap:anywhere]">
|
<p className="[overflow-wrap:anywhere]">
|
||||||
<strong>
|
<strong>
|
||||||
{t('admin.sheets.no_result').replace('{q}', () => view.query.trim())}
|
{t('admin.sheets.no_result').replace('{q}', () => view.query.trim())}
|
||||||
@@ -301,6 +324,7 @@ function SearchNoResult({ view }: { view: ListView }) {
|
|||||||
variant="secondary"
|
variant="secondary"
|
||||||
size="sm"
|
size="sm"
|
||||||
onClick={view.clearSearch}
|
onClick={view.clearSearch}
|
||||||
|
className="max-sm:h-11"
|
||||||
>
|
>
|
||||||
<Icon name="close" size={14} className="mr-1" />
|
<Icon name="close" size={14} className="mr-1" />
|
||||||
{t('admin.sheets.clear_search')}
|
{t('admin.sheets.clear_search')}
|
||||||
@@ -309,23 +333,26 @@ function SearchNoResult({ view }: { view: ListView }) {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
interface ResultsProps {
|
/** The rows shown, computed again only when the list, filters, search, sort or language change. */
|
||||||
sheets: Sheet[]
|
function useShownSheets(sheets: Sheet[], view: ListView): Sheet[] {
|
||||||
species: CatalogSpecies[]
|
const locale = useLocaleStore((s) => s.locale)
|
||||||
view: ListView
|
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). */
|
/** The count line and the rows, or the empty state that fits (board 28 note). */
|
||||||
function Results({ sheets, species, view }: ResultsProps) {
|
function Results({ sheets, species, view }: ResultsProps) {
|
||||||
const t = useT()
|
const t = useT()
|
||||||
const locale = useLocaleStore((s) => s.locale)
|
const shown = useShownSheets(sheets, view)
|
||||||
if (sheets.length === 0) return <EmptyLine text={t('admin.sheets.empty')} />
|
if (sheets.length === 0) return <EmptyLine text={t('admin.sheets.empty')} />
|
||||||
const filtered = filterSheets(sheets, view.filters)
|
|
||||||
const shown = sortSheets(
|
|
||||||
searchSheets(filtered, view.query),
|
|
||||||
view.sort,
|
|
||||||
locale,
|
|
||||||
)
|
|
||||||
const filtering = Object.values(view.filters).some(Boolean)
|
const filtering = Object.values(view.filters).some(Boolean)
|
||||||
return (
|
return (
|
||||||
<div className="space-y-3">
|
<div className="space-y-3">
|
||||||
@@ -337,10 +364,7 @@ function Results({ sheets, species, view }: ResultsProps) {
|
|||||||
<SearchNoResult view={view} />
|
<SearchNoResult view={view} />
|
||||||
))}
|
))}
|
||||||
{shown.length > 0 && (
|
{shown.length > 0 && (
|
||||||
<ul
|
<ul aria-label={t('admin.sheets.results_label')} className={LIST}>
|
||||||
aria-label={t('admin.sheets.results_label')}
|
|
||||||
className="divide-y divide-[var(--color-border)] rounded-2xl border border-[var(--color-border)] bg-[var(--color-surface)]"
|
|
||||||
>
|
|
||||||
{shown.map((sheet) => (
|
{shown.map((sheet) => (
|
||||||
<SheetRow key={sheet.sheet_id} sheet={sheet} species={species} />
|
<SheetRow key={sheet.sheet_id} sheet={sheet} species={species} />
|
||||||
))}
|
))}
|
||||||
|
|||||||
@@ -54,10 +54,23 @@ export function folded(text: string): string {
|
|||||||
return text.normalize('NFD').replace(/\p{Diacritic}/gu, '').toLowerCase()
|
return text.normalize('NFD').replace(/\p{Diacritic}/gu, '').toLowerCase()
|
||||||
}
|
}
|
||||||
|
|
||||||
/** A sheet name contains the search, in French or in English, accents and case ignored. */
|
/** The search folded once, then tested on each name: what a long list needs per keystroke. */
|
||||||
export function matchesSheetName(name: { fr: string; en: string }, query: string): boolean {
|
export function sheetNameMatcher(
|
||||||
|
query: string,
|
||||||
|
): (name: { fr: string; en: string }) => boolean {
|
||||||
const term = folded(query.trim())
|
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. */
|
/** Search by sheet name only, French or English (BDR-095), then the species chip. */
|
||||||
|
|||||||
Reference in new issue
Block a user