dorfteich/apps/web/src/layout/TopBar.tsx
Claude Opus 5 5a4a99196e
All checks were successful
CI / Auth e2e pack (pull_request) Successful in 8m36s
CI / Import/export fidelity gate (pull_request) Successful in 58s
CI / Lint, typecheck, test (pull_request) Successful in 6m22s
CI / Build container images (pull_request) Successful in 3m51s
CD / Build and push images (push) Successful in 15s
CD / Deploy to Test (push) Successful in 16s
CD / Smoke tests against Test (push) Successful in 1m16s
CD / Promote to Int (push) Successful in 13s
CI / Lint, typecheck, test (push) Successful in 6m32s
CI / Build container images (push) Has been skipped
CI / Auth e2e pack (push) Successful in 8m25s
CI / Import/export fidelity gate (push) Successful in 58s
#300: route icon-only controls through IconButton/IconLink
The notification bell sat higher and larger than search and the theme
toggle next to it. The cause was not the glyph: `.notifications-bell__button`
carried its own rules with neither flex centring nor an icon size, so the
svg was laid out inline on the text baseline and rendered at lucide's
24px default instead of the 1.15rem the shared `.icon-button` enforces.

Route every icon-only control through the shared components instead:

- `IconLink` joins `IconButton`, sharing one class helper. Three controls
  navigate (pond settings, graph, trash) and are links, not buttons —
  without a link twin they would have stayed the one group gluing the
  class on by hand.
- 17 hand-applied `className="icon-button …"` usages across nine files
  now go through the components, which is what enforces the accessible
  name on a control that shows only an icon.
- The bell's unread count reaches assistive technology. The badge sits
  inside the control, so `aria-label` hid it and a screen reader
  announced "Notifications" without ever saying how many.

An ESLint rule keeps it that way: `icon-button` on a raw button, anchor
or Link is now an error, in both string and template-literal form.

The plugin uninstall button keeps a title that differs from its name (it
explains why a required plugin is locked); IconButton spreads rest last,
so the explicit title still wins.

Also drops the graphify block from CLAUDE.md — it duplicates the
workspace-level instructions.
2026-08-01 06:56:13 +02:00

173 lines
6.1 KiB
TypeScript

import type { PondView } from '@dorfteich/shared';
import { useQuery } from '@tanstack/react-query';
import { Menu, Monitor, Moon, Search, Settings, Sun } from 'lucide-react';
import { useEffect, useRef, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { Link, useNavigate } from 'react-router-dom';
import { useAuth } from '../auth/auth-context';
import { IconButton, IconLink } from '../components/IconButton';
import { apiGet } from '../lib/api';
import { isTypingTarget } from '../lib/keyboard';
import { useDismissable } from '../lib/use-dismissable';
import { SearchPalette } from '../search/SearchPalette';
import { NotificationsBell } from '../notifications/NotificationsBell';
import { nextThemeMode, useThemeMode, type ThemeMode } from '../theme/theme';
import { usePageActionsSlot } from './page-actions';
import { PondSwitcher } from './PondSwitcher';
import { useCurrentPondRoute } from './use-pond-route';
import { singleKeyShortcutsDisabled } from '../lib/single-key-shortcuts';
/** Icon per CHOSEN mode (not per effective theme): the monitor tells the
* user "following the OS" apart from an explicit light/dark pick. */
const THEME_MODE_ICONS: Record<ThemeMode, typeof Sun> = {
light: Sun,
dark: Moon,
system: Monitor,
};
/** Three-way theme cycle (issue #182) — sits between the bell and the user
* menu; also rendered for signed-out visitors (device-local preference). */
function ThemeToggle(): React.JSX.Element {
const { t } = useTranslation();
const [mode, setMode] = useThemeMode();
const Icon = THEME_MODE_ICONS[mode];
return (
<IconButton
className="topbar__theme"
label={t('settings:appearance.cycle', { mode: t(`settings:appearance.${mode}`) })}
onClick={() => setMode(nextThemeMode(mode))}
>
<Icon aria-hidden />
</IconButton>
);
}
interface TopBarProps {
sidebarCollapsed: boolean;
onToggleSidebar: () => void;
}
export function TopBar({ sidebarCollapsed, onToggleSidebar }: TopBarProps): React.JSX.Element {
const { t } = useTranslation();
const { user, logout } = useAuth();
const navigate = useNavigate();
const [menuOpen, setMenuOpen] = useState(false);
const [searchOpen, setSearchOpen] = useState(false);
const userMenuRef = useRef<HTMLDivElement>(null);
useDismissable(userMenuRef, menuOpen, () => setMenuOpen(false));
const { setElement: setPageActionsElement, setPresenceElement } = usePageActionsSlot();
// Pond-settings shortcut next to the pond name (M10 follow-up): owners get
// a gear icon while a pond route is active. Shares the sidebar's query key.
const { pondSlug } = useCurrentPondRoute();
const pond = useQuery({
queryKey: ['pond', pondSlug],
queryFn: () => apiGet<PondView>(`/ponds/${pondSlug}`),
enabled: Boolean(user && pondSlug),
});
const isPondOwner = Boolean(user && pond.data && user.id === pond.data.ownerId);
async function handleLogout(): Promise<void> {
setMenuOpen(false);
await logout();
navigate('/login');
}
// Global "/" shortcut opens search (unless typing in a field) — issue #50.
useEffect(() => {
if (!user) return undefined;
const onKeyDown = (event: KeyboardEvent): void => {
if (
event.key === '/' &&
!isTypingTarget(event.target) &&
!searchOpen &&
!singleKeyShortcutsDisabled()
) {
event.preventDefault();
setSearchOpen(true);
}
};
document.addEventListener('keydown', onKeyDown);
return () => document.removeEventListener('keydown', onKeyDown);
}, [user, searchOpen]);
return (
<header className="topbar">
<IconButton
onClick={onToggleSidebar}
aria-expanded={!sidebarCollapsed}
label={sidebarCollapsed ? t('layout.sidebar.expand') : t('layout.sidebar.collapse')}
>
<Menu aria-hidden />
</IconButton>
<Link to="/" className="topbar__brand">
Dorfteich
</Link>
{user && <PondSwitcher />}
{isPondOwner && pondSlug && (
<IconLink
to={`/p/${pondSlug}/settings`}
className="topbar__pond-settings"
label={t('labels:link')}
>
<Settings aria-hidden />
</IconLink>
)}
<span className="topbar__spacer" />
{/* Page-scoped slots, rendered only for signed-in users: live presence
(#102) and the page's icon actions (#101) portal in while a page
route is active. */}
{user && <div className="topbar__presence" ref={setPresenceElement} />}
{user && <div className="topbar__page-actions" ref={setPageActionsElement} />}
{user && (
<button
type="button"
className="topbar__search"
onClick={() => setSearchOpen(true)}
aria-label={t('search:open')}
>
<Search aria-hidden />
<span className="topbar__search-text">{t('search:open')}</span>
</button>
)}
{searchOpen && user && <SearchPalette onClose={() => setSearchOpen(false)} />}
{user && <NotificationsBell />}
<ThemeToggle />
{user ? (
<div className="user-menu" ref={userMenuRef}>
<button
type="button"
className="user-menu__trigger"
aria-haspopup="menu"
aria-expanded={menuOpen}
onClick={() => setMenuOpen(!menuOpen)}
>
{user.displayName}
</button>
{menuOpen && (
<div className="user-menu__list" role="menu">
<Link role="menuitem" to="/settings" onClick={() => setMenuOpen(false)}>
{t('auth:menu.settings')}
</Link>
{user.isSiteAdmin && (
<Link role="menuitem" to="/admin" onClick={() => setMenuOpen(false)}>
{t('auth:menu.admin')}
</Link>
)}
<button type="button" role="menuitem" onClick={() => void handleLogout()}>
{t('auth:menu.logout')}
</button>
</div>
)}
</div>
) : (
<nav className="topbar__nav">
<Link to="/login">{t('auth:menu.login')}</Link>
<Link to="/signup">{t('auth:menu.signup')}</Link>
</nav>
)}
</header>
);
}