File size: 1,852 Bytes
ff34739
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"use client";

import { useEffect, useState } from "react";
import { toast } from "sonner";
import { Textarea } from "@/components/ui/textarea";
import { Button } from "@/components/ui/button";

export function NotesEditor({ projectId }: { projectId: string }) {
  const [notes, setNotes] = useState("");
  const [saved, setSaved] = useState("");
  const [busy, setBusy] = useState(false);

  useEffect(() => {
    let cancelled = false;
    fetch(`/api/projects/${projectId}/inputs/notes`)
      .then((r) => r.json())
      .then((d: { notes?: string }) => {
        if (!cancelled) {
          setNotes(d.notes ?? "");
          setSaved(d.notes ?? "");
        }
      })
      .catch(() => {});
    return () => {
      cancelled = true;
    };
  }, [projectId]);

  const dirty = notes !== saved;

  async function save() {
    setBusy(true);
    try {
      const res = await fetch(`/api/projects/${projectId}/inputs/notes`, {
        method: "PUT",
        headers: { "content-type": "application/json" },
        body: JSON.stringify({ notes }),
      });
      if (res.ok) {
        setSaved(notes);
        toast.success("Notes saved");
      } else {
        toast.error("Could not save notes");
      }
    } finally {
      setBusy(false);
    }
  }

  return (
    <div className="space-y-2">
      <Textarea
        value={notes}
        onChange={(e) => setNotes(e.target.value)}
        rows={6}
        placeholder="Sample prep notes, batch, operator, kit lot, anything the assistant should know about this run…"
        className="resize-y font-mono text-xs"
      />
      <div className="flex justify-end">
        <Button size="sm" variant={dirty ? "default" : "outline"} onClick={save} disabled={!dirty || busy}>
          {busy ? "Saving…" : dirty ? "Save notes" : "Saved"}
        </Button>
      </div>
    </div>
  );
}