UI: scaffold the web app - shell, themes, i18n, auth plumbing
React 19 + TypeScript + Vite + TanStack Query/Router in web/, built on the dev machine and shipped as static files (SPEC section 6). Typechecks and builds clean; the System page reads live /health through the Vite proxy, which proves browser -> proxy -> FastAPI -> PostgreSQL end to end. Themes: light, dark and blue (dark navy), as design tokens on data-theme. An inline script applies the stored theme before first paint. No colour literal lives outside tokens.css. check-contrast.mjs asserts WCAG AA for every text/background pair in every theme, with no dependencies. It failed on its first run and found three --border-strong values below the 3:1 that WCAG 1.4.11 wants for interactive boundaries; all three are fixed and 36 pairs now pass. Colour contrast is the classic silently-regressing property, so it gets a test rather than a promise. i18n is hand-written and typed rather than react-i18next: en.ts defines the key set, de.ts is typed against it, so a missing German string fails the build instead of quietly showing English to a German user. Authentication is built but NOT enforced, deliberately. /auth/session, /auth/login and /auth/logout do not exist yet, so router.tsx does not wrap routes in RequireAuth and a banner says so plainly. There is no development bypass: a bypass that works is the kind of code that survives into production, and an auth bypass that works is the worst possible bug. Gallery and Devices render honest empty states rather than mock rows. Fake data hides the layout problems real data exposes. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
5a098e44d8
commit
384b09371a
28 files changed
+3968
-1
No files matched your search
@@ -0,0 +1,96 @@
|
||||
/* A small typed translator instead of a library.
|
||||
*
|
||||
* Two languages and a few hundred strings do not need i18next, and the
|
||||
* hand-written catalogue buys something i18next does not give by default:
|
||||
* a missing German string fails the build rather than falling back silently
|
||||
* to English in front of the user.
|
||||
*
|
||||
* If this ever needs plurals with real grammar, ICU MessageFormat or
|
||||
* react-i18next is the upgrade path and the call sites do not change shape.
|
||||
*/
|
||||
|
||||
import { createContext, useCallback, useContext, useEffect, useMemo, useState } from "react";
|
||||
import type { ReactNode } from "react";
|
||||
|
||||
import { de } from "./de";
|
||||
import { en } from "./en";
|
||||
import type { Catalogue, TranslationKey } from "./en";
|
||||
|
||||
export const LOCALES = ["de", "en"] as const;
|
||||
export type Locale = (typeof LOCALES)[number];
|
||||
|
||||
const CATALOGUES: Record<Locale, Catalogue> = { de, en };
|
||||
const STORAGE_KEY = "poorbird.locale";
|
||||
|
||||
/** Values substituted into `{placeholders}`. */
|
||||
export type TranslationVars = Record<string, string | number>;
|
||||
|
||||
export type Translate = (key: TranslationKey, vars?: TranslationVars) => string;
|
||||
|
||||
interface I18nValue {
|
||||
locale: Locale;
|
||||
setLocale: (locale: Locale) => void;
|
||||
t: Translate;
|
||||
}
|
||||
|
||||
const I18nContext = createContext<I18nValue | null>(null);
|
||||
|
||||
function isLocale(value: unknown): value is Locale {
|
||||
return typeof value === "string" && (LOCALES as readonly string[]).includes(value);
|
||||
}
|
||||
|
||||
function initialLocale(): Locale {
|
||||
try {
|
||||
const stored = localStorage.getItem(STORAGE_KEY);
|
||||
if (isLocale(stored)) return stored;
|
||||
} catch {
|
||||
// Private mode or blocked storage. Fall through to the browser setting.
|
||||
}
|
||||
return navigator.language.toLowerCase().startsWith("de") ? "de" : "en";
|
||||
}
|
||||
|
||||
function interpolate(template: string, vars?: TranslationVars): string {
|
||||
if (!vars) return template;
|
||||
return template.replace(/\{(\w+)\}/g, (whole, name: string) => {
|
||||
const value = vars[name];
|
||||
// Leaving the placeholder visible beats printing "undefined" at a user.
|
||||
return value === undefined ? whole : String(value);
|
||||
});
|
||||
}
|
||||
|
||||
export function I18nProvider({ children }: { children: ReactNode }) {
|
||||
const [locale, setLocaleState] = useState<Locale>(initialLocale);
|
||||
|
||||
useEffect(() => {
|
||||
document.documentElement.lang = locale;
|
||||
try {
|
||||
localStorage.setItem(STORAGE_KEY, locale);
|
||||
} catch {
|
||||
// Not worth telling the user about; the choice just will not persist.
|
||||
}
|
||||
}, [locale]);
|
||||
|
||||
const setLocale = useCallback((next: Locale) => setLocaleState(next), []);
|
||||
|
||||
const t = useCallback<Translate>(
|
||||
(key, vars) => interpolate(CATALOGUES[locale][key], vars),
|
||||
[locale]
|
||||
);
|
||||
|
||||
const value = useMemo<I18nValue>(() => ({ locale, setLocale, t }), [locale, setLocale, t]);
|
||||
|
||||
return <I18nContext value={value}>{children}</I18nContext>;
|
||||
}
|
||||
|
||||
export function useI18n(): I18nValue {
|
||||
const value = useContext(I18nContext);
|
||||
if (value === null) {
|
||||
throw new Error("useI18n must be used inside <I18nProvider>");
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
/** Shorthand for the common case: `const t = useT()`. */
|
||||
export function useT(): Translate {
|
||||
return useI18n().t;
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
import type { Catalogue } from "./en";
|
||||
|
||||
/* Typed as Catalogue, so a missing or misspelled key fails `npm run build`.
|
||||
* That is the whole reason this file is hand-written rather than loaded as
|
||||
* JSON at runtime: a missing German string should never reach a browser. */
|
||||
export const de: Catalogue = {
|
||||
"app.name": "poorBird",
|
||||
"app.tagline": "Gartenvogel-Kamera",
|
||||
|
||||
"nav.gallery": "Galerie",
|
||||
"nav.devices": "Geräte",
|
||||
"nav.system": "System",
|
||||
|
||||
"common.loading": "Wird geladen…",
|
||||
"common.retry": "Erneut versuchen",
|
||||
"common.error": "Etwas ist schiefgelaufen",
|
||||
"common.notMeasured": "—",
|
||||
"common.yes": "Ja",
|
||||
"common.no": "Nein",
|
||||
|
||||
"theme.label": "Design",
|
||||
"theme.light": "Hell",
|
||||
"theme.dark": "Dunkel",
|
||||
"theme.blue": "Blau",
|
||||
|
||||
"lang.label": "Sprache",
|
||||
"lang.en": "Englisch",
|
||||
"lang.de": "Deutsch",
|
||||
|
||||
"auth.notEnforced":
|
||||
"Die Anmeldung ist noch nicht aktiv — die Auth-Endpunkte kommen in Phase 3.",
|
||||
"auth.signIn": "Anmelden",
|
||||
"auth.username": "Benutzername",
|
||||
"auth.password": "Passwort",
|
||||
"auth.totp": "Bestätigungscode",
|
||||
"auth.totpHint": "Sechs Ziffern aus deiner Authenticator-App",
|
||||
"auth.signingIn": "Anmeldung läuft…",
|
||||
"auth.failed": "Anmeldung fehlgeschlagen. Benutzername und Passwort prüfen.",
|
||||
"auth.unavailable":
|
||||
"Den Anmelde-Endpunkt gibt es noch nicht. Diese Seite ist die Hülle dafür.",
|
||||
|
||||
"gallery.title": "Galerie",
|
||||
"gallery.empty.title": "Noch keine Aufnahmen",
|
||||
"gallery.empty.body":
|
||||
"Die Kamera hat noch nichts hochgeladen. Der Upload kommt in Phase 2; bis dahin bleibt diese Liste leer.",
|
||||
|
||||
"devices.title": "Geräte",
|
||||
"devices.empty.title": "Keine Geräte registriert",
|
||||
"devices.empty.body":
|
||||
"Eine Kamera erscheint hier, sobald sie sich registriert hat. Das kommt in Phase 2.",
|
||||
|
||||
"system.title": "System",
|
||||
"system.status": "Status",
|
||||
"system.status.ok": "In Ordnung",
|
||||
"system.status.degraded": "Eingeschränkt",
|
||||
"system.version": "Version",
|
||||
"system.uptime": "Laufzeit",
|
||||
"system.database": "Datenbank",
|
||||
"system.database.ok": "Verbunden",
|
||||
"system.database.down": "Nicht erreichbar",
|
||||
"system.devices": "Geräte",
|
||||
"system.captures": "Aufnahmen",
|
||||
"system.lastBackup": "Letzte Sicherung",
|
||||
"system.lastBackup.never": "Nie",
|
||||
"system.disk": "Bildspeicher",
|
||||
"system.disk.free": "{free} frei von {total}",
|
||||
"system.disk.unavailable": "Pfad nicht verfügbar",
|
||||
"system.enrollSecret": "Registrierungs-Secret",
|
||||
"system.enrollSecret.set": "Konfiguriert",
|
||||
"system.enrollSecret.unset": "Nicht gesetzt",
|
||||
"system.unreachable.title": "Server nicht erreichbar",
|
||||
"system.unreachable.body":
|
||||
"Die API hat nicht geantwortet. Läuft sie auf Port 8090?",
|
||||
};
|
||||
@@ -0,0 +1,80 @@
|
||||
/* English is the source of truth for the key set.
|
||||
*
|
||||
* `as const` makes TypeScript remember every literal key, which is what lets
|
||||
* de.ts be checked for completeness at build time. Add a key here first.
|
||||
*/
|
||||
export const en = {
|
||||
"app.name": "poorBird",
|
||||
"app.tagline": "Garden bird camera",
|
||||
|
||||
"nav.gallery": "Gallery",
|
||||
"nav.devices": "Devices",
|
||||
"nav.system": "System",
|
||||
|
||||
"common.loading": "Loading…",
|
||||
"common.retry": "Try again",
|
||||
"common.error": "Something went wrong",
|
||||
"common.notMeasured": "—",
|
||||
"common.yes": "Yes",
|
||||
"common.no": "No",
|
||||
|
||||
"theme.label": "Theme",
|
||||
"theme.light": "Light",
|
||||
"theme.dark": "Dark",
|
||||
"theme.blue": "Blue",
|
||||
|
||||
"lang.label": "Language",
|
||||
"lang.en": "English",
|
||||
"lang.de": "German",
|
||||
|
||||
"auth.notEnforced":
|
||||
"Sign-in is not enforced yet — the auth endpoints arrive in Phase 3.",
|
||||
"auth.signIn": "Sign in",
|
||||
"auth.username": "Username",
|
||||
"auth.password": "Password",
|
||||
"auth.totp": "Authentication code",
|
||||
"auth.totpHint": "Six digits from your authenticator app",
|
||||
"auth.signingIn": "Signing in…",
|
||||
"auth.failed": "Sign-in failed. Check your username and password.",
|
||||
"auth.unavailable":
|
||||
"The sign-in endpoint does not exist yet. This screen is the shell it will use.",
|
||||
|
||||
"gallery.title": "Gallery",
|
||||
"gallery.empty.title": "No captures yet",
|
||||
"gallery.empty.body":
|
||||
"The camera has not uploaded anything. Ingest arrives in Phase 2; until then this list stays empty.",
|
||||
|
||||
"devices.title": "Devices",
|
||||
"devices.empty.title": "No devices enrolled",
|
||||
"devices.empty.body":
|
||||
"A camera appears here after it enrolls. Enrollment arrives in Phase 2.",
|
||||
|
||||
"system.title": "System",
|
||||
"system.status": "Status",
|
||||
"system.status.ok": "Healthy",
|
||||
"system.status.degraded": "Degraded",
|
||||
"system.version": "Version",
|
||||
"system.uptime": "Uptime",
|
||||
"system.database": "Database",
|
||||
"system.database.ok": "Connected",
|
||||
"system.database.down": "Unreachable",
|
||||
"system.devices": "Devices",
|
||||
"system.captures": "Captures",
|
||||
"system.lastBackup": "Last backup",
|
||||
"system.lastBackup.never": "Never",
|
||||
"system.disk": "Image storage",
|
||||
"system.disk.free": "{free} free of {total}",
|
||||
"system.disk.unavailable": "Path not available",
|
||||
"system.enrollSecret": "Enrollment secret",
|
||||
"system.enrollSecret.set": "Configured",
|
||||
"system.enrollSecret.unset": "Not set",
|
||||
"system.unreachable.title": "Cannot reach the server",
|
||||
"system.unreachable.body":
|
||||
"The API did not respond. Is it running on port 8090?",
|
||||
} as const;
|
||||
|
||||
/** Every valid translation key. A typo in `t("...")` is a compile error. */
|
||||
export type TranslationKey = keyof typeof en;
|
||||
|
||||
/** The shape every other language must fill completely. */
|
||||
export type Catalogue = Record<TranslationKey, string>;
|
||||
Reference in new issue
Block a user