import { useEffect, useMemo, useRef, useState } from "react"; import { Link, useParams } from "react-router"; import { logout } from "wasp/client/auth"; import { getSpec, resetSection, updateSection, useQuery } from "wasp/client/operations"; import { SECTION_TEMPLATES, isSectionDone } from "./sections.js"; import "./styles.css"; export const SpecPage = ({ user }) => { const { id } = useParams(); const specId = Number(id); const { data: spec, isLoading, error } = useQuery(getSpec, { id: specId }); const [drafts, setDrafts] = useState({}); const [current, setCurrent] = useState(0); const [mode, setMode] = useState("edit"); const [saveState, setSaveState] = useState("idle"); const timerRef = useRef(null); // Seed drafts from the server exactly once per loaded section. useEffect(() => { if (spec?.sections) { setDrafts((prev) => { const next = { ...prev }; spec.sections.forEach((s) => { if (next[s.key] === undefined) next[s.key] = s.content; }); return next; }); } }, [spec]); const sections = spec?.sections ?? []; const sec = sections[current]; const doneCount = useMemo( () => sections.filter((s) => isSectionDone(drafts[s.key])).length, [sections, drafts] ); const handleChange = (value) => { if (!sec) return; setDrafts((d) => ({ ...d, [sec.key]: value })); setSaveState("saving"); clearTimeout(timerRef.current); timerRef.current = setTimeout(async () => { try { await updateSection({ specId, key: sec.key, content: value }); setSaveState("saved"); } catch (err) { setSaveState("error"); } }, 700); }; const markdown = useMemo(() => { if (!spec) return ""; return ( `# Tech Spec — ${spec.title}\n\n` + sections .map((s, i) => `## ${i + 1}. ${s.title}\n\n${(drafts[s.key] ?? s.content).trim()}`) .join("\n\n---\n\n") ); }, [spec, sections, drafts]); if (isLoading) { return
Loading…
; } if (error) { return

Error: {error.message}

← Back
; } if (!spec) return null; return (
SpecForge {doneCount}/{sections.length}{doneCount === sections.length ? " · ready for harness" : ""}
Section {String(current + 1).padStart(2, "0")} of {sections.length}
{sec?.title}
{sec?.hint}
{mode === "edit" ? (