Files
temetro/frontend/components/notes/notes-view.tsx
T
Khalid Abdi 49c39bd853 Tasks/notes/prescriptions/appointments/patients UX fixes
- Task dialog: rename Title→Subject, add a "Details" textarea (notes),
  widen to sm:max-w-lg.
- Tasks & Notes: replace split-panes with full-width lists that open a
  right-side Sheet on click (TaskDetailSheet, NoteDetailSheet), mirroring
  the Patients table → detail Sheet pattern.
- Prescriptions: rows are now clickable and open PrescriptionDetailSheet
  with the full Rx details (incl. duration + notes).
- New prescription dialog: Duration is a dropdown of presets with an
  "Other" → custom input, plus a Notes textarea; NewPrescription gains
  notes and threads duration/notes through to the list.
- Appointments: header search filtering by patient/type/provider,
  grouped by date, with an empty state.
- Patients: pressing Enter in the search box opens the top match's record.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-06 18:59:19 +03:00

185 lines
5.1 KiB
TypeScript

"use client";
import { NotebookPen, Plus } from "lucide-react";
import { useEffect, useState } from "react";
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 [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(
"Couldn't load notes",
err instanceof Error ? err.message : "Please try again.",
);
}
})
.finally(() => {
if (active) setLoading(false);
});
return () => {
active = false;
};
}, []);
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("Note saved");
} catch (err) {
notify.error(
"Couldn't save note",
err instanceof Error ? err.message : "Please try again.",
);
} finally {
setSaving(false);
}
};
const remove = async (id: string) => {
try {
await deleteNote(id);
const list = await listNotes();
setNotes(list);
setSelected(null);
setSheetOpen(false);
notify.success("Note deleted");
} catch (err) {
notify.error(
"Couldn't delete note",
err instanceof Error ? err.message : "Please try again.",
);
}
};
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">Notes</h1>
<p className="text-muted-foreground text-sm">
Clinical notes. Click a note to open it.
</p>
</div>
<Button className="rounded-3xl" onClick={startNew} type="button">
<Plus className="size-4" />
New note
</Button>
</div>
{loading ? (
<div className="rounded-2xl border bg-card/30 px-4 py-10 text-center text-muted-foreground text-sm">
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>No notes yet</EmptyTitle>
<EmptyDescription>
Create a note to start writing.
</EmptyDescription>
</EmptyHeader>
<EmptyContent>
<Button onClick={startNew} type="button">
<Plus className="size-4" />
New note
</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-left 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 || "Untitled note"}
</span>
<span className="text-muted-foreground text-xs">
Updated {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>
);
}