freezmo/components/FolderBar.tsx
Martin Dvorak 982391ca45 Codebase audit: fix bugs, performance issues, dead code, and edge cases
1. Bugs fixed:
- Backup schema now accepts theme: "aero" so backups exported under
  Frutiger Aero pass parseBackup on import, and applyImport in "replace"
  mode replaces SRS schedules, answer history, and chats instead of
  merging into existing SRS or skipping empty arrays.
- Typed FillBlank cards bind the input value to chosen ?? value instead
  of chosen ?? "", and normalise() preserves Unicode letters (\p{L}\p{N})
  when grading non-ASCII answers.
- OpenAI-compatible vision calls (contentFor in lib/openai-compat.ts)
  prefix data:image/jpeg;base64, when routes pass stripped base64.
- Streaming calls (ollamaChatStream, providerChatStream) keep their
  AbortController active across the entire body stream and API routes
  forward request.signal so stopping a chat or cancelling a run aborts
  the upstream request immediately.
- HomeworkCard uses homeworkModeLabelKey so "check" records display the
  Checked label instead of Step-by-step guide.
- Desktop dropzones on /create and /homework accept dropped documents as
  well as images.
- LibraryScreen filters reported questions out of its due and mistake
  counts to match ReviewClient.
- Clearing a provider key resets generation if it pointed at that
  provider; chat conversations persist and restore provider; ModelPicker
  keys items by provider:name.
- RouteTransition no longer mutates module-level lastPath during render.
- Service worker asset handler falls back to Response.error() when an
  uncached asset fetch fails offline.
- Confetti and .ice-orb / .ice-bubble respect the in-app reduce-motion
  toggle.

2. Performance & cleanup:
- Deduplicate window storage listeners across all six localStorage
  stores (one listener while subscribed instead of N^2 notifications).
- Use todayAnswerCount in LibraryScreen and pass sorted folders to
  DeckCard.
- Memoize MessageRow with stable callbacks in ChatScreen and compute
  last message IDs in a single pass.
- Close ImageBitmap after drawing in lib/image.ts and destroy pdf.js
  documents in finally in lib/documents.ts.
- Avoid re-parsing serialized sync payloads in POST /api/sync.
- Deduplicate copyText (lib/share.ts -> lib/clipboard.ts), chatMessageId,
  describeExtraction, ALL_FOLDERS, useIsPhone, and remove unused helpers
  and boilerplate public/*.svg files.

Version 0.5.8, SW -> freezmo-v80.
2026-10-14 19:00:17 +00:00

130 lines
4.2 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 { ALL_FOLDERS } from "@/lib/nav";
import type { Folder } from "@/lib/schemas";
import { cn } from "@/lib/utils";
export { ALL_FOLDERS };
/**
* 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(
"folder-pill 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="folder-pill 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>
);
}