Files
temetro/frontend/components/notes/notes-view.tsx
T
Khalid Abdi e8f3ed9ffe frontend: clear the 99 lint errors
`npm run lint` reported 99 errors and 26 warnings across 65 files and
had presumably been failing for a while — next.config.ts sets
eslint.ignoreDuringBuilds, so the build never surfaced it.

68 were in vendored code: components/charts and components/ai-elements,
pulled from upstream registries. Re-linting those reports upstream's
style back at us, and "fixing" them means diverging and eating conflicts
on every update — ai-elements already has exactly this carve-out on the
TypeScript side via ignoreBuildErrors. Ignore both, plus the two registry
files in components/ui (carousel publishes its api from an effect; the
sidebar skeleton picks a random width).

The other 31 were ours, nearly all react-hooks/set-state-in-effect on
the same shape: an effect that re-seeds form state when a dialog opens or
a selection changes. Moved to render-phase adjustment, which is both what
React recommends and a real fix — the effect version paints one frame of
the *previous* record's values before correcting itself. Two carried
sharper bugs: the employee dialog could keep a typed password across a
switch to another member, and use-wallet-sync could carry `linked` over
to a newly-selected patient, briefly offering to push a record to
someone else's wallet.

The rest: useIsMobile and speech-support detection become
useSyncExternalStore (correct on first paint, no mount flash); refs
mirroring state are written in effects rather than during render; the
care-team fetch moves into its effect behind a reload key, dropping an
exhaustive-deps suppression.

Two effects in chat-panel keep the rule disabled with a reason. Both are
what effects are for: draining the queued-message buffer when the
transport goes idle, and resolving ?thread from the URL — the latter
mints an id with nanoid(), so moving it into render would just trade this
error for a purity one.

Also removes a dead /explore-era import and unused directives found on
the way. lint now exits 0, with the ai-elements tsc carve-out unchanged.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-15 20:54:37 +03:00

192 lines
5.5 KiB
TypeScript

"use client";
import { NotebookPen, Plus } from "lucide-react";
import { useEffect, useState } from "react";
import { useTranslation } from "react-i18next";
import { NoteDetailSheet } from "@/components/notes/note-detail-sheet";
import { Button } from "@/components/ui/button";
import {
Empty,
EmptyContent,
EmptyDescription,
EmptyHeader,
EmptyMedia,
EmptyTitle,
} from "@/components/ui/empty";
import {
createNote,
deleteNote,
listNotes,
type Note,
updateNote,
} from "@/lib/notes";
import { notify } from "@/lib/toast";
const newDraft = (): Note => ({
id: "",
title: "",
content: "",
createdAt: "",
updatedAt: "",
});
export function NotesView() {
const { t } = useTranslation();
const [notes, setNotes] = useState<Note[]>([]);
// The note shown in the editor Sheet; null when the Sheet is closed.
const [selected, setSelected] = useState<Note | null>(null);
const [sheetOpen, setSheetOpen] = useState(false);
const [draftKey, setDraftKey] = useState(0);
const [loading, setLoading] = useState(true);
const [saving, setSaving] = useState(false);
useEffect(() => {
let active = true;
listNotes()
.then((data) => {
if (active) setNotes(data);
})
.catch((err) => {
if (active) {
notify.error(
t("notes.loadFailed"),
err instanceof Error ? err.message : t("notes.tryAgain"),
);
}
})
.finally(() => {
if (active) setLoading(false);
});
return () => {
active = false;
};
// `t` intentionally omitted: it is only read to build a failure message,
// and re-fetching on a language change would be pointless.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
const startNew = () => {
setSelected(newDraft());
setDraftKey((k) => k + 1);
setSheetOpen(true);
};
const openNote = (note: Note) => {
setSelected(note);
setSheetOpen(true);
};
const save = async (data: { title: string; content: string }) => {
setSaving(true);
try {
const saved = selected?.id
? await updateNote(selected.id, data)
: await createNote(data);
const list = await listNotes();
setNotes(list);
setSelected(list.find((n) => n.id === saved.id) ?? saved);
notify.success(t("notes.saved"));
} catch (err) {
notify.error(
t("notes.saveFailed"),
err instanceof Error ? err.message : t("notes.tryAgain"),
);
} finally {
setSaving(false);
}
};
const remove = async (id: string) => {
try {
await deleteNote(id);
const list = await listNotes();
setNotes(list);
setSelected(null);
setSheetOpen(false);
notify.success(t("notes.deleted"));
} catch (err) {
notify.error(
t("notes.deleteFailed"),
err instanceof Error ? err.message : t("notes.tryAgain"),
);
}
};
return (
<div className="mx-auto flex w-full max-w-3xl flex-col gap-6 px-6 py-10">
<div className="flex flex-col gap-4 sm:flex-row sm:items-start sm:justify-between">
<div>
<h1 className="font-semibold text-2xl tracking-tight">
{t("notes.title")}
</h1>
<p className="text-muted-foreground text-sm">{t("notes.subtitle")}</p>
</div>
<Button className="rounded-3xl" onClick={startNew} type="button">
<Plus className="size-4" />
{t("notes.new")}
</Button>
</div>
{loading ? (
<div className="rounded-2xl border bg-card/30 px-4 py-10 text-center text-muted-foreground text-sm">
{t("notes.loading")}
</div>
) : notes.length === 0 ? (
<div className="flex flex-1 items-center justify-center rounded-2xl border bg-card/30 py-16">
<Empty>
<EmptyHeader>
<EmptyMedia variant="icon">
<NotebookPen />
</EmptyMedia>
<EmptyTitle>{t("notes.emptyTitle")}</EmptyTitle>
<EmptyDescription>
{t("notes.emptyDescription")}
</EmptyDescription>
</EmptyHeader>
<EmptyContent>
<Button onClick={startNew} type="button">
<Plus className="size-4" />
{t("notes.new")}
</Button>
</EmptyContent>
</Empty>
</div>
) : (
<div className="divide-y divide-border overflow-hidden rounded-2xl border bg-card/30">
{notes.map((n) => (
<button
className="flex w-full flex-col items-start gap-0.5 px-4 py-3 text-start transition-colors hover:bg-accent/50"
key={n.id}
onClick={() => openNote(n)}
type="button"
>
<span className="w-full truncate font-medium text-foreground text-sm">
{n.title || t("notes.untitled")}
</span>
<span className="text-muted-foreground text-xs">
{t("notes.updated", {
date: new Date(n.updatedAt).toLocaleDateString(),
})}
</span>
</button>
))}
</div>
)}
<NoteDetailSheet
editorKey={selected?.id || `draft-${draftKey}`}
note={selected}
onDelete={selected?.id ? () => remove(selected.id) : undefined}
onOpenChange={(o) => {
setSheetOpen(o);
if (!o) setSelected(null);
}}
onSave={save}
open={sheetOpen}
saving={saving}
/>
</div>
);
}