Berichten: instellingen naar tabblad, schakelaar rechts in de kop, toegankelijke tabs met ?tab=settings

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-11 18:08:14 +01:00
co-authored by Claude Sonnet 5.5
parent eb6472ea61
commit fccb85a983
8 changed files with 335 additions and 94 deletions
@@ -0,0 +1,104 @@
import { useRef, type KeyboardEvent, type ReactNode } from 'react';
export interface ContactTab<T extends string> {
id: T;
label: string;
panel: ReactNode;
}
interface Props<T extends string> {
tabs: ContactTab<T>[];
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<T extends string>({ tabs, value, onChange, ...rest }: Props<T>) {
const refs = useRef<Record<string, HTMLButtonElement | null>>({});
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 (
<div className="space-y-4">
<div
role="tablist"
aria-label={rest['aria-label']}
className="flex gap-1 border-b"
onKeyDown={onKeyDown}
>
{tabs.map((tab) => {
const selected = tab.id === value;
return (
<button
key={tab.id}
ref={(el) => {
refs.current[tab.id] = el;
}}
type="button"
role="tab"
id={`contact-tab-${tab.id}`}
aria-selected={selected}
aria-controls={`contact-panel-${tab.id}`}
tabIndex={selected ? 0 : -1}
onClick={() => onChange(tab.id)}
data-testid={`contact-tab-${tab.id}`}
className={`-mb-px min-h-11 border-b-2 px-4 text-sm font-medium focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring ${
selected
? 'border-primary text-foreground'
: 'border-transparent text-muted-foreground hover:text-foreground'
}`}
>
{tab.label}
</button>
);
})}
</div>
{tabs.map(
(tab) =>
tab.id === value && (
<div
key={tab.id}
role="tabpanel"
id={`contact-panel-${tab.id}`}
aria-labelledby={`contact-tab-${tab.id}`}
tabIndex={0}
className="space-y-4 focus-visible:outline-none"
data-testid={`contact-panel-${tab.id}`}
>
{tab.panel}
</div>
),
)}
</div>
);
}
@@ -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 (
<div className="flex flex-col items-end gap-1">
{settings.hostEnabled ? (
<ToggleSwitch
checked={open}
onCheckedChange={toggle}
label={t('contact.switch.shortLabel')}
title={t('contact.switch.shortLabel')}
stateText={open ? t('contact.switch.on') : t('contact.switch.off')}
disabled={update.isPending}
data-testid="contact-switch-toggle"
/>
) : (
<p className="text-sm text-muted-foreground" data-testid="contact-switch-host-off">
{t('contact.switch.hostOff')}
</p>
)}
{update.isError && (
<p
role="alert"
className="text-sm text-destructive"
data-testid="contact-switch-error"
>
{contactErrorMessage(update.error, t)}
</p>
)}
</div>
);
}
/** 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 (
<Card data-testid="contact-switch-card">
<CardContent className="space-y-3 p-4">
<div className="flex flex-wrap items-center justify-between gap-3">
<div>
<h2 className="font-semibold">{t('contact.switch.title')}</h2>
<p className="text-sm" data-testid="contact-switch-state">
{open ? t('contact.switch.stateOn') : t('contact.switch.stateOff')}
</p>
</div>
{settings.hostEnabled ? (
<ToggleSwitch
checked={open}
onCheckedChange={toggle}
label={t('contact.switch.title')}
stateText={open ? t('contact.switch.on') : t('contact.switch.off')}
disabled={update.isPending}
data-testid="contact-switch-toggle"
/>
) : (
<p
className="text-sm text-muted-foreground"
data-testid="contact-switch-host-off"
>
{t('contact.switch.hostOff')}
</p>
)}
<div>
<h2 className="font-semibold">{t('contact.switch.title')}</h2>
<p className="text-sm" data-testid="contact-switch-state">
{open ? t('contact.switch.stateOn') : t('contact.switch.stateOff')}
</p>
</div>
<p className="text-sm text-muted-foreground">{t('contact.switch.explanation')}</p>
{settings.recipientSource === 'none' && (
@@ -65,15 +88,6 @@ export function FormSwitchCard() {
{t('contact.switch.firstTimeHint')}
</p>
)}
{update.isError && (
<p
role="alert"
className="text-sm text-destructive"
data-testid="contact-switch-error"
>
{contactErrorMessage(update.error, t)}
</p>
)}
</CardContent>
</Card>
);
@@ -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}
@@ -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');
});
});
@@ -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<ContactFilter>('inbox');
const [page, setPage] = useState(1);
const [openId, setOpenId] = useState<string | null>(null);
@@ -33,68 +40,98 @@ export function MessagesPage() {
return (
<div className="space-y-4 p-4">
<h1 className="text-2xl font-semibold" data-testid="messages-title">
{t('contact.title')}
</h1>
<div className="flex flex-wrap items-center justify-between gap-3">
<h1 className="text-2xl font-semibold" data-testid="messages-title">
{t('contact.title')}
</h1>
<FormSwitchHeader />
</div>
<FormSwitchCard />
<ContactTabs<TabId>
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: (
<>
<MessagesFilterChips
value={filter}
unreadCount={data?.unreadCount}
onChange={changeFilter}
/>
<RecipientCard />
{isPending && <p className="text-sm text-muted-foreground">{t('common.loading')}</p>}
{isError && (
<p role="alert" className="text-sm text-destructive">
{t('errors.generic')}
</p>
)}
<MessagesFilterChips
value={filter}
unreadCount={data?.unreadCount}
onChange={changeFilter}
{data && data.items.length === 0 && (
<div
className="flex flex-col items-center gap-3 py-12 text-center"
data-testid="messages-empty-state"
>
<Mail className="size-10 text-muted-foreground" />
<p className="text-muted-foreground">{t(`contact.empty.${filter}`)}</p>
</div>
)}
{data && data.items.length > 0 && (
<>
<MessagesTable items={data.items} onOpen={setOpenId} />
<div className="flex items-center justify-between text-sm">
<span data-testid="messages-range">
{t('contact.pager.range', { from, to, total: data.totalCount })}
</span>
<div className="flex gap-2">
<Button
variant="outline"
size="sm"
disabled={!hasPrevious}
onClick={() => setPage(page - 1)}
data-testid="messages-prev"
>
{t('contact.pager.previous')}
</Button>
<Button
variant="outline"
size="sm"
disabled={!hasNext}
onClick={() => setPage(page + 1)}
data-testid="messages-next"
>
{t('contact.pager.next')}
</Button>
</div>
</div>
</>
)}
</>
),
},
{
id: 'settings',
label: t('contact.tabs.settings'),
panel: (
<>
<FormSwitchCard />
<RecipientCard />
</>
),
},
]}
/>
{isPending && <p className="text-sm text-muted-foreground">{t('common.loading')}</p>}
{isError && (
<p role="alert" className="text-sm text-destructive">
{t('errors.generic')}
</p>
)}
{data && data.items.length === 0 && (
<div
className="flex flex-col items-center gap-3 py-12 text-center"
data-testid="messages-empty-state"
>
<Mail className="size-10 text-muted-foreground" />
<p className="text-muted-foreground">{t(`contact.empty.${filter}`)}</p>
</div>
)}
{data && data.items.length > 0 && (
<>
<MessagesTable items={data.items} onOpen={setOpenId} />
<div className="flex items-center justify-between text-sm">
<span data-testid="messages-range">
{t('contact.pager.range', { from, to, total: data.totalCount })}
</span>
<div className="flex gap-2">
<Button
variant="outline"
size="sm"
disabled={!hasPrevious}
onClick={() => setPage(page - 1)}
data-testid="messages-prev"
>
{t('contact.pager.previous')}
</Button>
<Button
variant="outline"
size="sm"
disabled={!hasNext}
onClick={() => setPage(page + 1)}
data-testid="messages-next"
>
{t('contact.pager.next')}
</Button>
</div>
</div>
</>
)}
<MessageDialog
key={openId ?? 'closed'}
id={openId}