Files
ServerKit/frontend/src/components/NotificationBell.jsx
T
Juan DenisandClaude Opus 5 2103bb87bd feat(i18n): translate the shared shell; seed the common vocabulary
Plan 79 §E wave 1. The shell is first because it is on every screen: a
translated page inside an English frame reads as broken, while a translated
frame around an English page reads as in-progress.

Converted: Sidebar (31 literals -> 1, the brand name, which must not be
translated), sidebarItems, CommandPalette, PageTopbar, MobileTopBar,
NotificationBell, EmptyState, ErrorState, ErrorBoundary, ConfirmDialog,
AppLoader. Ceiling 8225 -> 8132, and es.json now covers all 98 keys, so the
shell is provably bilingual rather than merely keyed.

Three shapes needed real tooling rather than a t() call, and each was found by
converting a file rather than by planning:

- Labels in data files (sidebarItems, palette docs entries) cannot call t():
  a table resolved at module load translates once, at import, and never
  follows a locale switch. They now declare `labelKey` beside `label`; the
  extractor collects the pair at its declaration site and useLabel resolves it
  at render. The ratchet understands the pairing, so converting a data file
  visibly moves the number (sidebarItems 27 -> 0).
- Sentences with an element inside them (ConfirmDialog's "Type <x> to
  confirm") go through <Trans>, which the extractor now reads. Splitting them
  into prefix/suffix keys would have been untranslatable: word order around
  the value differs by language and a split sentence cannot be reordered.
- Class components and module scope (ErrorBoundary, AppLoader) import a bare
  `t` from the i18n module. Reaching for `i18next.t` at the call site would
  have left that copy out of en.json, since the extractor matches bare calls.

The declarative-pair heuristic initially swept up `archKey: 'amd64'` next to
`arch: 'x64 (amd64)'` -- "Key" already means "identifier" in places -- so a
pair now only counts when the value looks like a dotted translation key.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-19 23:34:56 -04:00

165 lines
7.7 KiB
React
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { useState, useRef, useEffect } from 'react';
import { useNavigate } from 'react-router-dom';
import { Bell, Check, X } from 'lucide-react';
import { useNotifications } from '../contexts/NotificationsContext';
import { timeAgo } from '../utils/time';
import { useTranslation } from 'react-i18next';
// Severity → dot color, mirroring the email/brand palette.
const SEVERITY_DOT = {
critical: '#fb6f6f',
warning: '#f5b945',
success: '#3ddc97',
info: '#6d7cff',
test: '#9aa1b2',
};
export default function NotificationBell() {
const { t } = useTranslation();
const ctx = useNotifications();
const [open, setOpen] = useState(false);
const ref = useRef(null);
const navigate = useNavigate();
// The provider is only mounted for authenticated users; render nothing otherwise.
const { items = [], unreadCount = 0, markRead, markAllRead, refresh, dismissNotice } = ctx || {};
useEffect(() => {
if (!open) return undefined;
const onDocClick = (e) => {
if (ref.current && !ref.current.contains(e.target)) setOpen(false);
};
const onEsc = (e) => { if (e.key === 'Escape') setOpen(false); };
document.addEventListener('mousedown', onDocClick);
document.addEventListener('keydown', onEsc);
return () => {
document.removeEventListener('mousedown', onDocClick);
document.removeEventListener('keydown', onEsc);
};
}, [open]);
if (!ctx) return null;
const toggle = () => {
const next = !open;
setOpen(next);
if (next && refresh) refresh();
};
const onItemClick = (item) => {
// System notices route to their fix; bus notifications mark read and
// then deep-link to their subject when they carry an action_path.
if (item.kind === 'notice') {
setOpen(false);
if (item.action_path) navigate(item.action_path);
return;
}
if (!item.read && markRead) markRead(item.delivery_id);
if (item.action_path) {
setOpen(false);
navigate(item.action_path);
}
};
const badge = unreadCount > 99 ? '99+' : unreadCount;
return (
<div className="sk-notif" ref={ref}>
<button
type="button"
className="sk-notif__bell"
onClick={toggle}
aria-label={unreadCount
? t('notifications.bellUnread', 'Notifications, {{count}} unread', { count: unreadCount })
: t('notifications.bell', 'Notifications')}
aria-haspopup="true"
aria-expanded={open}
>
<Bell size={16} aria-hidden="true" />
{unreadCount > 0 && <span className="sk-notif__badge">{badge}</span>}
</button>
{open && (
<div className="sk-notif__panel" role="menu" aria-label={t('notifications.bell', 'Notifications')}>
<div className="sk-notif__head">
<span className="sk-notif__heading">{t('notifications.heading', 'Notifications')}</span>
{unreadCount > 0 && (
<button type="button" className="sk-notif__markall" onClick={markAllRead}>
<Check size={13} aria-hidden="true" /> {t('notifications.markAllRead', 'Mark all read')}
</button>
)}
</div>
<div className="sk-notif__list">
{items.length === 0 ? (
<div className="sk-notif__empty">{t('notifications.empty', 'You’re all caught up.')}</div>
) : (
items.map((item) => (
item.kind === 'notice' ? (
<div key={item.delivery_id} className="sk-notif__item is-notice">
<button
type="button"
className="sk-notif__hit"
onClick={() => onItemClick(item)}
>
<span
className="sk-notif__dot"
style={{ background: SEVERITY_DOT[item.severity] || SEVERITY_DOT.info }}
aria-hidden="true"
/>
<span className="sk-notif__content">
<span className="sk-notif__title">{item.title}</span>
{item.body && <span className="sk-notif__text">{item.body}</span>}
{item.action_label && (
<span className="sk-notif__time">{item.action_label} →</span>
)}
</span>
</button>
<button
type="button"
className="sk-notif__dismiss"
onClick={() => dismissNotice && dismissNotice(item.notice_id)}
aria-label={t('notifications.dismissItem', 'Dismiss {{title}}', { title: item.title })}
>
<X size={14} aria-hidden="true" />
</button>
</div>
) : (
<button
key={item.delivery_id}
type="button"
className={`sk-notif__item${item.read ? '' : ' is-unread'}`}
onClick={() => onItemClick(item)}
>
<span
className="sk-notif__dot"
style={{ background: SEVERITY_DOT[item.severity] || SEVERITY_DOT.info }}
aria-hidden="true"
/>
<span className="sk-notif__content">
<span className="sk-notif__title">{item.title}</span>
{item.body && <span className="sk-notif__text">{item.body}</span>}
<span className="sk-notif__time">
{timeAgo(item.created_at)}
{item.action_path && <span className="sk-notif__cta"> · {item.action_label || 'Open'} →</span>}
</span>
</span>
</button>
)
))
)}
</div>
<button
type="button"
className="sk-notif__seeall"
onClick={() => { setOpen(false); navigate('/notifications'); }}
>
{t('notifications.seeAll', 'See all notifications')}
</button>
</div>
)}
</div>
);
}