From 9efc7b7647f5c4f3a34a2ebc1789fdcabc13c4d1 Mon Sep 17 00:00:00 2001 From: bchanot Date: Mon, 16 Nov 2026 20:30:27 +0100 Subject: [PATCH] feat(app): collapsible sheet filters on phone, one empty announcement --- .../app/routes/app/admin/AdminSheetsRoute.tsx | 112 +++++++++++++++++- apps/app/src/i18n/en.ts | 3 + apps/app/src/i18n/fr.ts | 3 + apps/app/tests/unit/routes.admin.test.tsx | 107 +++++++++++++++++ 4 files changed, 220 insertions(+), 5 deletions(-) diff --git a/apps/app/src/app/routes/app/admin/AdminSheetsRoute.tsx b/apps/app/src/app/routes/app/admin/AdminSheetsRoute.tsx index 94621a97..9cf77d30 100644 --- a/apps/app/src/app/routes/app/admin/AdminSheetsRoute.tsx +++ b/apps/app/src/app/routes/app/admin/AdminSheetsRoute.tsx @@ -99,7 +99,24 @@ function useListView() { const [filters, setFilters] = useState(NO_FILTER) const [sort, setSort] = useState('modified') const search = useSearch() - return { filters, setFilters, sort, setSort, ...search } + const [open, setOpen] = useState(false) + const toggleRef = useRef(null) + /** The reset button disappears with the last filter: the focus moves to the toggle. */ + const resetFilters = () => { + setFilters(NO_FILTER) + toggleRef.current?.focus() + } + return { + ...search, + filters, + setFilters, + sort, + setSort, + open, + setOpen, + toggleRef, + resetFilters, + } } type ListView = ReturnType @@ -255,7 +272,7 @@ const STATUS_FILTERS = [ ] as const /** The four filters of §11.1, unchanged; species offered are those of the loaded list. */ -function Filters({ sheets, species, view }: ResultsProps) { +function FilterGrid({ sheets, species, view }: ResultsProps) { const t = useT() const locale = useLocaleStore((s) => s.locale) const speciesOptions = [...new Set(sheets.map((s) => s.species_id))].map( @@ -285,6 +302,84 @@ function Filters({ sheets, species, view }: ResultsProps) { ) } +const FILTERS_PANEL_ID = 'admin-sheets-filters' + +/** Phone only: « Filtres » or « Filtres (n) », n the filters that are not empty. */ +function FiltersToggle({ view }: { view: ListView }) { + const t = useT() + const active = Object.values(view.filters).filter(Boolean).length + return ( + + ) +} + +/** Phone only, inside the panel, while at least one filter is not empty. */ +function ResetFilters({ view }: { view: ListView }) { + const t = useT() + if (!Object.values(view.filters).some(Boolean)) return null + return ( + + ) +} + +// A closed panel is collapsed (grid row 0fr) and invisible, so nothing in it takes +// focus or a tap; visibility changes with the transition. `sm:` keeps the grid as +// it was on a desktop: always shown, no button. +const PANEL = + 'grid transition-[grid-template-rows,visibility] duration-200 ' + + 'motion-reduce:transition-none sm:block' + +/** The filters: a collapsible panel on a phone (closed by default), the grid from `sm`. */ +function Filters(props: ResultsProps) { + const { view } = props + const state = view.open + ? 'max-sm:mt-3 max-sm:grid-rows-[1fr]' + : 'max-sm:grid-rows-[0fr] max-sm:invisible' + return ( +
+ +
+
+ + +
+
+
+ ) +} + /** « {n} fiches » when everything is shown, « {n} fiches sur {total} » otherwise. */ function CountLine({ shown, total }: { shown: number; total: number }) { const plural = usePlural() @@ -298,9 +393,16 @@ function CountLine({ shown, total }: { shown: number; total: number }) { ) } -function EmptyLine({ text }: { text: string }) { +/** `announce` false when the count line already speaks for the empty result. */ +function EmptyLine({ + text, + announce = true, +}: { + text: string + announce?: boolean +}) { return ( -

+

{text}

) @@ -359,7 +461,7 @@ function Results({ sheets, species, view }: ResultsProps) { {shown.length === 0 && (filtering ? ( - + ) : ( ))} diff --git a/apps/app/src/i18n/en.ts b/apps/app/src/i18n/en.ts index 4a5329cb..c6c9becf 100644 --- a/apps/app/src/i18n/en.ts +++ b/apps/app/src/i18n/en.ts @@ -1038,6 +1038,9 @@ export const en: Dict = { 'admin.sheets.count_of_one': '{count} sheet of {total}', 'admin.sheets.count_of_other': '{count} sheets of {total}', 'admin.sheets.modified': 'Modified on {date}', + 'admin.sheets.filters': 'Filters', + 'admin.sheets.filters_active': 'Filters ({count})', + 'admin.sheets.filters_reset': 'Reset filters', 'admin.sheets.no_result': 'No sheet name contains “{q}”.', 'admin.sheet_form.lede': 'The sheet starts empty: views, annotations and findings are entered later in the editor.', 'admin.sheet_form.species': 'Species', diff --git a/apps/app/src/i18n/fr.ts b/apps/app/src/i18n/fr.ts index 34333016..bc753683 100644 --- a/apps/app/src/i18n/fr.ts +++ b/apps/app/src/i18n/fr.ts @@ -1069,6 +1069,9 @@ export const fr: Dict = { 'admin.sheets.count_of_one': '{count} fiche sur {total}', 'admin.sheets.count_of_other': '{count} fiches sur {total}', 'admin.sheets.modified': 'Modifiée le {date}', + 'admin.sheets.filters': 'Filtres', + 'admin.sheets.filters_active': 'Filtres ({count})', + 'admin.sheets.filters_reset': 'Réinitialiser les filtres', 'admin.sheets.no_result': 'Aucune fiche ne contient « {q} » dans son nom.', 'admin.sheet_form.lede': "La fiche démarre vide : vues, annotations et constats se saisissent ensuite dans l'éditeur.", 'admin.sheet_form.species': 'Espèce', diff --git a/apps/app/tests/unit/routes.admin.test.tsx b/apps/app/tests/unit/routes.admin.test.tsx index 856d546b..0e83e3b2 100644 --- a/apps/app/tests/unit/routes.admin.test.tsx +++ b/apps/app/tests/unit/routes.admin.test.tsx @@ -920,6 +920,113 @@ describe('Official sheets list (H1)', () => { ) expect(screen.getByText('1 sheet of 3')).toBeInTheDocument() }) + + it('announces an empty filtered result once: the count line, not a status', async () => { + const user = userEvent.setup() + await mountThree() + await user.selectOptions( + screen.getByLabelText('Statut de version'), + 'published', + ) + const note = screen.getByText('Aucune fiche ne correspond à ces filtres.') + expect(note.closest('[role="status"]')).toBeNull() + expect(screen.getByText('0 fiche sur 3')).toHaveAttribute( + 'aria-live', + 'polite', + ) + }) + + describe('collapsible filters on a phone', () => { + const toggle = (name: RegExp | string = /^Filtres/) => + screen.getByRole('button', { name }) + const panel = () => document.getElementById('admin-sheets-filters')! + const reset = () => + screen.queryByRole('button', { name: 'Réinitialiser les filtres' }) + + it('is closed by default: aria-expanded false, controls the panel, hidden on a phone only', async () => { + await mountThree() + expect(toggle()).toHaveTextContent('Filtres') + expect(toggle()).toHaveAttribute('aria-expanded', 'false') + expect(toggle()).toHaveAttribute('aria-controls', 'admin-sheets-filters') + expect(toggle()).toHaveClass('sm:hidden') + expect(panel()).toHaveAttribute('data-open', 'false') + expect(panel().className).toContain('max-sm:invisible') + expect(panel().className).toContain('motion-reduce:transition-none') + expect(panel().className).not.toMatch(/(^|\s)invisible/) + }) + + it('opens and closes on the button, with the four filters inside', async () => { + const user = userEvent.setup() + await mountThree() + await user.click(toggle()) + expect(toggle()).toHaveAttribute('aria-expanded', 'true') + expect(panel()).toHaveAttribute('data-open', 'true') + expect(panel().className).not.toContain('max-sm:invisible') + for (const label of [ + 'Espèce', + 'État de la fiche', + 'Statut de version', + 'État de la dernière revue', + ]) + expect(within(panel()).getByLabelText(label)).toBeInTheDocument() + await user.click(toggle()) + expect(toggle()).toHaveAttribute('aria-expanded', 'false') + expect(panel().className).toContain('max-sm:invisible') + }) + + it('counts the active filters on the button; closing keeps them', async () => { + const user = userEvent.setup() + const list = await mountThree() + await user.click(toggle()) + await user.selectOptions(screen.getByLabelText('Espèce'), 'equine') + await user.selectOptions( + screen.getByLabelText('État de la fiche'), + 'active', + ) + expect(toggle()).toHaveTextContent('Filtres (2)') + await user.click(toggle(/^Filtres \(2\)/)) + expect(toggle()).toHaveTextContent('Filtres (2)') + await user.click(toggle()) + expect(screen.getByLabelText('Espèce')).toHaveValue('equine') + expect(screen.getByLabelText('État de la fiche')).toHaveValue('active') + expect(list.getAllByRole('link').length).toBeGreaterThan(0) + }) + + it('resets every filter, keeps the search, and hands the focus to the button', async () => { + const user = userEvent.setup() + await mountThree() + expect(reset()).toBeNull() + await user.click(toggle()) + await user.type(searchField(), 'chev') + await user.selectOptions( + screen.getByLabelText('Statut de version'), + 'published', + ) + expect(toggle()).toHaveTextContent('Filtres (1)') + await user.click(reset()!) + expect(screen.getByLabelText('Statut de version')).toHaveValue('') + expect(toggle()).toHaveTextContent(/^Filtres$/) + expect(searchField()).toHaveValue('chev') + expect(reset()).toBeNull() + expect(toggle()).toHaveFocus() + }) + + it('reads in English', async () => { + useLocaleStore.setState({ locale: 'en' }) + const user = userEvent.setup() + fake.rpcResults.list_official_sheets_admin = { data: threeSheets() } + mount('/app/admin/sheets', [grant('sheet_editor')]) + await screen.findByRole('list', { name: 'Official sheet list' }) + await user.click(screen.getByRole('button', { name: 'Filters' })) + await user.selectOptions(screen.getByLabelText('Version status'), 'draft') + expect( + screen.getByRole('button', { name: 'Filters (1)' }), + ).toBeInTheDocument() + expect( + screen.getByRole('button', { name: 'Reset filters' }), + ).toBeInTheDocument() + }) + }) }) describe('Sheet creation (A1)', () => {