From fccb85a9837f23451737ac8b09d0dad055da72e2 Mon Sep 17 00:00:00 2001 From: Sluijsens Date: Wed, 11 Nov 2026 18:08:14 +0100 Subject: [PATCH] Berichten: instellingen naar tabblad, schakelaar rechts in de kop, toegankelijke tabs met ?tab=settings Co-Authored-By: Claude Sonnet 5.5 --- .../contact/components/ContactTabs.tsx | 104 ++++++++++++ .../contact/components/FormSwitchCard.tsx | 84 ++++++---- .../contact/components/ToggleSwitch.tsx | 4 + .../contact/pages/MessagesPage.test.tsx | 69 +++++++- .../features/contact/pages/MessagesPage.tsx | 153 +++++++++++------- frontend/src/i18n/locales/en/translation.json | 6 + frontend/src/i18n/locales/nl/translation.json | 6 + frontend/src/router.tsx | 3 + 8 files changed, 335 insertions(+), 94 deletions(-) create mode 100644 frontend/src/features/contact/components/ContactTabs.tsx diff --git a/frontend/src/features/contact/components/ContactTabs.tsx b/frontend/src/features/contact/components/ContactTabs.tsx new file mode 100644 index 0000000..6b94f93 --- /dev/null +++ b/frontend/src/features/contact/components/ContactTabs.tsx @@ -0,0 +1,104 @@ +import { useRef, type KeyboardEvent, type ReactNode } from 'react'; + +export interface ContactTab { + id: T; + label: string; + panel: ReactNode; +} + +interface Props { + tabs: ContactTab[]; + value: T; + onChange: (id: T) => void; + 'aria-label': string; +} + +/** + * Accessible tabs (WAI-ARIA tabs pattern, automatic activation): role=tablist/tab/tabpanel, roving tabindex, + * Left/Right/Home/End move and select. Only the selected panel is rendered. + */ +export function ContactTabs({ tabs, value, onChange, ...rest }: Props) { + const refs = useRef>({}); + const index = tabs.findIndex((tab) => tab.id === value); + + const move = (to: number) => { + const next = tabs[(to + tabs.length) % tabs.length]; + onChange(next.id); + refs.current[next.id]?.focus(); + }; + + const onKeyDown = (event: KeyboardEvent) => { + switch (event.key) { + case 'ArrowRight': + event.preventDefault(); + move(index + 1); + break; + case 'ArrowLeft': + event.preventDefault(); + move(index - 1); + break; + case 'Home': + event.preventDefault(); + move(0); + break; + case 'End': + event.preventDefault(); + move(tabs.length - 1); + break; + } + }; + + return ( +
+
+ {tabs.map((tab) => { + const selected = tab.id === value; + return ( + + ); + })} +
+ {tabs.map( + (tab) => + tab.id === value && ( +
+ {tab.panel} +
+ ), + )} +
+ ); +} diff --git a/frontend/src/features/contact/components/FormSwitchCard.tsx b/frontend/src/features/contact/components/FormSwitchCard.tsx index 6324c44..7742a8e 100644 --- a/frontend/src/features/contact/components/FormSwitchCard.tsx +++ b/frontend/src/features/contact/components/FormSwitchCard.tsx @@ -5,8 +5,11 @@ import { useContactSettings, useUpdateContactSettings } from '../services/useCon import { contactErrorMessage } from '../errors'; import { ToggleSwitch } from './ToggleSwitch'; -/** The form switch: whether the contact form on the website is open. The state shown is the server's. */ -export function FormSwitchCard() { +/** + * The form switch in the page header, level with the title: the switch with its visible state (On/Off) and a short + * accessible name/tooltip. The state shown is the server's. Explanations live in the Settings tab (FormSwitchCard). + */ +export function FormSwitchHeader() { const { t } = useTranslation(); const { data: settings } = useContactSettings(); const update = useUpdateContactSettings(); @@ -22,33 +25,53 @@ export function FormSwitchCard() { ), }); + return ( +
+ {settings.hostEnabled ? ( + + ) : ( +

+ {t('contact.switch.hostOff')} +

+ )} + {update.isError && ( +

+ {contactErrorMessage(update.error, t)} +

+ )} +
+ ); +} + +/** What the switch means (state, explanation, hints), at the top of the Settings tab. */ +export function FormSwitchCard() { + const { t } = useTranslation(); + const { data: settings } = useContactSettings(); + + if (!settings) return null; + + const open = settings.enabled; + return ( -
-
-

{t('contact.switch.title')}

-

- {open ? t('contact.switch.stateOn') : t('contact.switch.stateOff')} -

-
- {settings.hostEnabled ? ( - - ) : ( -

- {t('contact.switch.hostOff')} -

- )} +
+

{t('contact.switch.title')}

+

+ {open ? t('contact.switch.stateOn') : t('contact.switch.stateOff')} +

{t('contact.switch.explanation')}

{settings.recipientSource === 'none' && ( @@ -65,15 +88,6 @@ export function FormSwitchCard() { {t('contact.switch.firstTimeHint')}

)} - {update.isError && ( -

- {contactErrorMessage(update.error, t)} -

- )} ); diff --git a/frontend/src/features/contact/components/ToggleSwitch.tsx b/frontend/src/features/contact/components/ToggleSwitch.tsx index 277a871..cc20c7c 100644 --- a/frontend/src/features/contact/components/ToggleSwitch.tsx +++ b/frontend/src/features/contact/components/ToggleSwitch.tsx @@ -5,6 +5,8 @@ interface ToggleSwitchProps { onCheckedChange: (checked: boolean) => void; /** The visible label; it is also the accessible name of the switch. */ label: string; + /** Optional tooltip (native title). */ + title?: string; /** The state as text ("On"/"Off"), so the state is never conveyed by colour or position alone. */ stateText: string; disabled?: boolean; @@ -19,6 +21,7 @@ export function ToggleSwitch({ checked, onCheckedChange, label, + title, stateText, disabled = false, 'data-testid': testId, @@ -29,6 +32,7 @@ export function ToggleSwitch({ role="switch" aria-checked={checked} aria-label={label} + title={title} disabled={disabled} onClick={() => onCheckedChange(!checked)} data-testid={testId} diff --git a/frontend/src/features/contact/pages/MessagesPage.test.tsx b/frontend/src/features/contact/pages/MessagesPage.test.tsx index 645565c..1aab87b 100644 --- a/frontend/src/features/contact/pages/MessagesPage.test.tsx +++ b/frontend/src/features/contact/pages/MessagesPage.test.tsx @@ -38,6 +38,11 @@ async function openPage() { await screen.findByTestId('messages-title', {}, LONG); } +async function openSettings() { + await userEvent.click(await screen.findByTestId('contact-tab-settings')); + await screen.findByTestId('contact-panel-settings'); +} + describe('Berichten navigation', () => { it('shows the sidebar entry when the module is loaded and the API answers', async () => { capabilities(WITH_CONTACT); @@ -90,6 +95,7 @@ describe('Berichten navigation', () => { describe('Form switch', () => { it('shows the first-time hint while off and switches on and off with the server answer', async () => { await openPage(); + await openSettings(); expect(await screen.findByTestId('contact-switch-hint')).toBeInTheDocument(); const toggle = screen.getByTestId('contact-switch-toggle'); expect(toggle).toHaveAttribute('aria-checked', 'false'); @@ -115,7 +121,7 @@ describe('Form switch', () => { it('is a real switch: named, stateful, 44px, with the state as text, usable by keyboard', async () => { await openPage(); - const toggle = await screen.findByRole('switch', { name: 'Contact form on the website' }); + const toggle = await screen.findByRole('switch', { name: 'Contact form on/off' }); expect(toggle).toHaveAttribute('aria-checked', 'false'); expect(toggle).toHaveTextContent('Off'); @@ -168,6 +174,7 @@ describe('Form switch', () => { describe('Recipient address', () => { it('shows the field, says that the server setting applies, and never an address it was not given', async () => { await openPage(); + await openSettings(); expect(await screen.findByTestId('contact-recipient-card')).toBeInTheDocument(); expect(screen.getByTestId('contact-recipient-input')).toHaveValue(''); @@ -180,6 +187,7 @@ describe('Recipient address', () => { it('saves a valid address, shows where it comes from and offers to go back', async () => { await openPage(); + await openSettings(); const input = await screen.findByTestId('contact-recipient-input'); await userEvent.type(input, ' inbox@example.org '); @@ -196,6 +204,7 @@ describe('Recipient address', () => { it('does not save an invalid address (client side), and says so', async () => { await openPage(); + await openSettings(); const input = await screen.findByTestId('contact-recipient-input'); await userEvent.type(input, 'not-an-address'); @@ -208,6 +217,7 @@ describe('Recipient address', () => { it('shows a translated message when the server refuses the address', async () => { await openPage(); + await openSettings(); server.use( http.put(`${API_BASE}/api/v1/contact-settings/recipient`, () => HttpResponse.json( @@ -233,6 +243,7 @@ describe('Recipient address', () => { it('goes back to the server setting when cleared', async () => { await openPage(); + await openSettings(); await userEvent.type( await screen.findByTestId('contact-recipient-input'), 'inbox@example.org', @@ -260,6 +271,7 @@ describe('Recipient address', () => { ), ); await openPage(); + await openSettings(); expect(await screen.findByTestId('contact-no-recipient-banner')).toHaveTextContent( 'No recipient address is set', @@ -709,3 +721,58 @@ describe('Clickable row', () => { expect(await screen.findByTestId('contact-message-body')).toHaveTextContent('Hallo'); }); }); + +describe('Tabs and header switch', () => { + it('opens on the messages tab with the switch level with the title and no settings blocks', async () => { + await openPage(); + expect(await screen.findByRole('tab', { name: 'Messages', selected: true })).toBeInTheDocument(); + expect(screen.getByRole('tabpanel')).toHaveAttribute('id', 'contact-panel-messages'); + expect(await screen.findByTestId(`contact-row-${MSG_NEW}`)).toBeInTheDocument(); + expect(screen.queryByTestId('contact-recipient-card')).not.toBeInTheDocument(); + expect(screen.queryByTestId('contact-switch-card')).not.toBeInTheDocument(); + + const header = screen.getByTestId('messages-title').parentElement as HTMLElement; + const toggle = within(header).getByRole('switch', { name: 'Contact form on/off' }); + expect(toggle).toHaveAttribute('title', 'Contact form on/off'); + expect(toggle).toHaveTextContent('Off'); + }); + + it('moves the explanation, hints and recipient to the Settings tab', async () => { + await openPage(); + await openSettings(); + expect(screen.getByRole('tab', { name: 'Settings', selected: true })).toBeInTheDocument(); + const card = await screen.findByTestId('contact-switch-card'); + expect(card).toHaveTextContent('While the form is off'); + expect(screen.getByTestId('contact-switch-state')).toBeInTheDocument(); + expect(screen.getByTestId('contact-recipient-card')).toBeInTheDocument(); + expect(screen.queryByTestId('messages-range')).not.toBeInTheDocument(); + // the switch stays in the header + expect(screen.getAllByRole('switch')).toHaveLength(1); + + await userEvent.click(screen.getByTestId('contact-tab-messages')); + expect(await screen.findByTestId(`contact-row-${MSG_NEW}`)).toBeInTheDocument(); + }); + + it('opens directly on the Settings tab from the URL', async () => { + capabilities(WITH_CONTACT); + renderApp('/messages?tab=settings'); + expect(await screen.findByTestId('contact-recipient-card', {}, LONG)).toBeInTheDocument(); + expect(screen.getByRole('tab', { name: 'Settings', selected: true })).toBeInTheDocument(); + }); + + it('supports the arrow keys, Home and End on the tabs', async () => { + await openPage(); + const messagesTab = await screen.findByRole('tab', { name: 'Messages' }); + messagesTab.focus(); + await userEvent.keyboard('{ArrowRight}'); + expect(await screen.findByRole('tab', { name: 'Settings', selected: true })).toHaveFocus(); + expect(screen.getByRole('tabpanel')).toHaveAttribute('aria-labelledby', 'contact-tab-settings'); + await userEvent.keyboard('{ArrowRight}'); + expect(await screen.findByRole('tab', { name: 'Messages', selected: true })).toHaveFocus(); + await userEvent.keyboard('{End}'); + expect(await screen.findByRole('tab', { name: 'Settings', selected: true })).toHaveFocus(); + await userEvent.keyboard('{Home}'); + expect(await screen.findByRole('tab', { name: 'Messages', selected: true })).toHaveFocus(); + expect(screen.getByRole('tab', { name: 'Settings' })).toHaveAttribute('tabindex', '-1'); + }); +}); diff --git a/frontend/src/features/contact/pages/MessagesPage.tsx b/frontend/src/features/contact/pages/MessagesPage.tsx index 7ecee42..96e40df 100644 --- a/frontend/src/features/contact/pages/MessagesPage.tsx +++ b/frontend/src/features/contact/pages/MessagesPage.tsx @@ -1,8 +1,10 @@ import { useState } from 'react'; import { useTranslation } from 'react-i18next'; import { Mail } from 'lucide-react'; +import { useNavigate, useSearch } from '@tanstack/react-router'; import { Button } from '@/components/ui/button'; -import { FormSwitchCard } from '../components/FormSwitchCard'; +import { FormSwitchCard, FormSwitchHeader } from '../components/FormSwitchCard'; +import { ContactTabs } from '../components/ContactTabs'; import { RecipientCard } from '../components/RecipientCard'; import { MessagesFilterChips } from '../components/MessagesFilterChips'; import { MessagesTable } from '../components/MessagesTable'; @@ -12,8 +14,13 @@ import { useContactMessages, useDeleteContactMessage } from '../services/useCont import { contactErrorMessage } from '../errors'; import type { ContactFilter } from '../services/types'; +type TabId = 'messages' | 'settings'; + export function MessagesPage() { const { t } = useTranslation(); + const navigate = useNavigate(); + const search = useSearch({ strict: false }) as { tab?: string }; + const tab: TabId = search.tab === 'settings' ? 'settings' : 'messages'; const [filter, setFilter] = useState('inbox'); const [page, setPage] = useState(1); const [openId, setOpenId] = useState(null); @@ -33,68 +40,98 @@ export function MessagesPage() { return (
-

- {t('contact.title')} -

+
+

+ {t('contact.title')} +

+ +
- + + aria-label={t('contact.tabs.label')} + value={tab} + onChange={(next) => + void navigate({ + to: '.', + search: { tab: next === 'settings' ? 'settings' : undefined }, + replace: true, + }) + } + tabs={[ + { + id: 'messages', + label: t('contact.tabs.messages'), + panel: ( + <> + - + {isPending &&

{t('common.loading')}

} + {isError && ( +

+ {t('errors.generic')} +

+ )} - + +

{t(`contact.empty.${filter}`)}

+
+ )} + + {data && data.items.length > 0 && ( + <> + +
+ + {t('contact.pager.range', { from, to, total: data.totalCount })} + +
+ + +
+
+ + )} + + ), + }, + { + id: 'settings', + label: t('contact.tabs.settings'), + panel: ( + <> + + + + ), + }, + ]} /> - {isPending &&

{t('common.loading')}

} - {isError && ( -

- {t('errors.generic')} -

- )} - - {data && data.items.length === 0 && ( -
- -

{t(`contact.empty.${filter}`)}

-
- )} - - {data && data.items.length > 0 && ( - <> - -
- - {t('contact.pager.range', { from, to, total: data.totalCount })} - -
- - -
-
- - )} - authenticatedRoute, path: '/messages', + validateSearch: (search: Record): { tab?: 'settings' } => ({ + tab: search.tab === 'settings' ? 'settings' : undefined, + }), component: () => (