diff --git a/apps/web/src/components/projects/context-editor.tsx b/apps/web/src/components/projects/context-editor.tsx new file mode 100644 index 0000000..839e9c0 --- /dev/null +++ b/apps/web/src/components/projects/context-editor.tsx @@ -0,0 +1,69 @@ +import { api } from "@code/backend/convex/_generated/api"; +import type { Id } from "@code/backend/convex/_generated/dataModel"; +import { Button } from "@code/ui/components/button"; +import { useMutation, useQuery } from "convex/react"; +import { LoaderCircle } from "lucide-react"; +import { useState } from "react"; + +export const ContextEditor = ({ + projectId, +}: { + readonly projectId: Id<"projects">; +}) => { + const instructions = useQuery(api.projects.getInstructions, { + projectId, + }); + const updateInstructions = useMutation(api.projects.updateInstructions); + const [draft, setDraft] = useState(); + const [saving, setSaving] = useState(false); + + const currentDraft = draft ?? instructions ?? ""; + const dirty = draft !== undefined && draft !== (instructions ?? ""); + + const save = async () => { + if (!dirty || saving) { + return; + } + setSaving(true); + try { + await updateInstructions({ + instructions: draft ?? "", + projectId, + }); + setDraft(undefined); + } finally { + setSaving(false); + } + }; + + return ( +
+ +