freezmo/components/FolderBar.tsx
Martin Dvorak 618ad5fb9c Add an English/Czech interface language switch
English stays the default; Czech is a full translation. Only the app chrome
is translated - notes and generated decks keep their own language, which is a
core feature, not a setting.

i18n
- lib/i18n-dict.ts: flat en/cs dictionaries (441 keys each), translate() and
  translatePlural() with real Czech plural rules (1 sada / 2-4 sady / 5+ sad).
  English is the default and the fallback for a missing key.
- lib/i18n.ts: browser store (freezmo.locale.v1), useI18n() hook, and the
  x-freezmo-locale header. The hook's t/plural are bound to the locale React
  is rendering with, so the first client render matches the server HTML and
  then re-renders in the stored language; module-level t is only for event
  handlers and non-React helpers.
- <html lang> follows the choice (inline script + setLocale).

UI
- Settings -> Language segmented control (English / Cestina), plus an EN/CS
  button on the key gate for first run.
- Every screen translated: sidebar, header, tab bar, library, deck cards,
  folder bar, create flow, all seven activities, study chrome, results,
  settings + health panel, API-key gate and help, install sheet, offline and
  the deck-not-found page.
- Plural-aware counts everywhere (decks, folders, activities, runs, photos,
  documents, pages, words, wrong attempts).

Server
- API routes and guards (generate, grade, health, http-guard, rate-limit,
  ollama error mapping) answer in the request's language, so error cards and
  the health panel never fall back to English mid-sentence.

Fixes
- A hydration mismatch when a non-default locale was stored silently dropped
  the .dark class after hydration; render-time t() is now bound to the
  hook's locale (UploadScreen.describeExtraction, ResultsScreen.answerFor,
  DeckCard's folder fallback).
- Bumped the service worker cache to freezmo-v4 for this deploy.

Tests: i18n 27/27 (gate toggle, persistence, html lang, Czech plurals,
settings switch) and a rebuilt regression suite 72/72 (English default,
library, create, all 7 activity types, results, settings, install sheet,
offline, PWA, security, Czech smoke).
2026-10-08 13:56:32 +00:00

129 lines
4.1 KiB
TypeScript

"use client";
import { useRef, useState } from "react";
import { FolderIcon, PlusIcon } from "@/components/icons";
import { createFolder } from "@/lib/folders";
import { useI18n } from "@/lib/i18n";
import type { Folder } from "@/lib/schemas";
import { cn } from "@/lib/utils";
export const ALL_FOLDERS = "all";
/**
* Folder switcher. Reads like a row of quiet pills (ChatGPT sidebar items):
* the active one is filled, the rest are text with a hover wash.
*/
export function FolderBar({
folders,
counts,
selected,
onSelect,
onCreated,
className,
}: {
folders: Folder[];
counts: Record<string, number>;
selected: string;
onSelect: (id: string) => void;
onCreated: (id: string) => void;
className?: string;
}) {
const [creating, setCreating] = useState(false);
const [draft, setDraft] = useState("");
const [error, setError] = useState<string | null>(null);
const inputRef = useRef<HTMLInputElement>(null);
const { t } = useI18n();
const submit = () => {
const result = createFolder(draft);
if ("error" in result) {
setError(result.error);
return;
}
onCreated(result.folder.id);
setDraft("");
setError(null);
setCreating(false);
};
const pill = (active: boolean) =>
cn(
"inline-flex shrink-0 items-center gap-2 rounded-full px-3.5 py-1.5 text-[13px] font-medium transition",
active
? "bg-[var(--surface-sunken)] text-[var(--text)]"
: "text-[var(--text-muted)] hover:bg-[var(--hover)] hover:text-[var(--text)]",
);
return (
<div className={cn("no-scrollbar -mx-4 overflow-x-auto px-4 sm:mx-0 sm:px-0", className)}>
<div className="flex items-center gap-1.5">
<button
type="button"
onClick={() => onSelect(ALL_FOLDERS)}
aria-pressed={selected === ALL_FOLDERS}
className={pill(selected === ALL_FOLDERS)}
>
{t("common.allDecks")}
</button>
{folders.map((folder) => (
<button
key={folder.id}
type="button"
onClick={() => onSelect(folder.id)}
aria-pressed={selected === folder.id}
className={pill(selected === folder.id)}
>
<FolderIcon className="size-4 opacity-80" />
{folder.name}
<span className={cn("tabular-nums", selected === folder.id ? "opacity-70" : "text-[var(--text-subtle)]")}>
{counts[folder.id] ?? 0}
</span>
</button>
))}
{creating ? (
<span className="inline-flex shrink-0 items-center gap-1 rounded-full border border-[var(--hairline)] bg-[var(--surface-strong)] py-1 pl-3 pr-1">
<input
ref={inputRef}
autoFocus
value={draft}
onChange={(event) => setDraft(event.target.value)}
onKeyDown={(event) => {
event.stopPropagation();
if (event.key === "Enter") submit();
if (event.key === "Escape") {
setCreating(false);
setDraft("");
setError(null);
}
}}
placeholder={t("common.folderName")}
aria-label={t("common.newFolderName")}
maxLength={60}
className="w-28 bg-transparent text-[13px] text-[var(--text)] outline-none placeholder:text-[var(--text-subtle)]"
/>
<button
type="button"
onClick={submit}
className="btn-primary h-7 px-3 text-[12px]"
>
{t("common.add")}
</button>
</span>
) : (
<button
type="button"
onClick={() => setCreating(true)}
className="inline-flex shrink-0 items-center gap-2 rounded-full px-3.5 py-1.5 text-[13px] font-medium text-[var(--text-subtle)] transition hover:bg-[var(--hover)] hover:text-[var(--text)]"
>
<PlusIcon className="size-4" />
{t("common.newFolder")}
</button>
)}
</div>
{error ? <p className="pt-1 text-xs font-medium text-rose-600 dark:text-rose-300">{error}</p> : null}
</div>
);
}