import { useEffect, useState } from "react"; import { get, post } from "../api"; import { useT } from "../i18n"; import { navigate } from "../router"; import { toast } from "../dialogs"; import { Icon } from "./Icon"; import { Spinner } from "./Activity"; interface Draft { spec: string; setup?: string | null; proposal: { name?: string; slug?: string; description?: string } | null; connections: { id: string; name: string; kind: string; owner: string }[]; } export const isDraftChat = (cwd: string) => /\/_drafts\/[^/]+$/.test(cwd); const slugify = (s: string) => s .toLowerCase() .replace(/ä/g, "ae") .replace(/ö/g, "oe") .replace(/ü/g, "ue") .replace(/ß/g, "ss") .replace(/[^a-z0-9]+/g, "-") .replace(/^-+|-+$/g, "") .slice(0, 60); /** Bar above the composer of a spec chat: file the spec as a new repository and start the workflow. */ export function DraftBar({ sessionId }: { sessionId: string }) { const t = useT(); const [open, setOpen] = useState(false); return ( <>
{t("draft.hint")}
{open && setOpen(false)} />} ); } function CreateProjectDialog({ sessionId, onClose }: { sessionId: string; onClose: () => void }) { const t = useT(); const [draft, setDraft] = useState(); const [name, setName] = useState(""); const [slug, setSlug] = useState(""); const [slugTouched, setSlugTouched] = useState(false); const [description, setDescription] = useState(""); const [connection, setConnection] = useState(""); const [owner, setOwner] = useState(""); const [busy, setBusy] = useState(false); useEffect(() => { get(`/factory/drafts/${sessionId}`) .then((d) => { setDraft(d); setName(d.proposal?.name ?? ""); setSlug(d.proposal?.slug ? slugify(d.proposal.slug) : ""); setSlugTouched(!!d.proposal?.slug); setDescription(d.proposal?.description ?? ""); setConnection(d.connections[0]?.id ?? ""); setOwner(d.connections[0]?.owner ?? ""); }) .catch((e) => { toast(e.message); onClose(); }); }, [sessionId]); if (!draft) return (
); const conn = draft.connections.find((c) => c.id === connection); const ready = draft.spec.trim() && name.trim() && slug.length >= 2 && connection; const create = async () => { setBusy(true); try { const r = await post<{ repo: string; issue: number }>(`/factory/drafts/${sessionId}/create`, { connection, name, slug, description, owner }); await post("/factory/poll", {}).catch(() => undefined); toast(t("draft.created", { repo: r.repo, n: r.issue }), "success"); onClose(); navigate("/factory"); } catch (e: any) { toast(e.message); setBusy(false); } }; return (
e.stopPropagation()}>

{t("draft.create")}

{draft.connections.length === 0 ? (
{t("draft.noConnection")}{" "}
) : ( <> {!draft.spec.trim() &&
{t("draft.noSpec")}
} {draft.spec.trim() && !draft.setup &&
{t("draft.noSetup")}
}
{t("draft.private", { conn: conn?.name || "" })}
{t("draft.specPreview")}
{draft.spec || "–"}
)}
); } export const isIssueChat = (cwd: string) => /\/_issues\/[^/]+$/.test(cwd); interface ProposedIssue { title: string; description: string; scope: string; depends: string; } interface IssueProposal { repo: string; proposal: { spec?: string; issues: ProposedIssue[] } | null; } /** Bar above the composer of an issue chat: file the proposed issues on the forge. */ export function IssueBar({ sessionId }: { sessionId: string }) { const t = useT(); const [open, setOpen] = useState(false); return ( <>
{t("issues.hint")}
{open && setOpen(false)} />} ); } function CreateIssuesDialog({ sessionId, onClose }: { sessionId: string; onClose: () => void }) { const t = useT(); const [data, setData] = useState(); const [items, setItems] = useState<(ProposedIssue & { on: boolean })[]>([]); const [spec, setSpec] = useState(""); const [start, setStart] = useState(false); const [busy, setBusy] = useState(false); useEffect(() => { get(`/factory/issue-chats/${sessionId}`) .then((d) => { setData(d); setSpec(d.proposal?.spec ?? ""); setItems((d.proposal?.issues ?? []).map((i) => ({ title: i.title ?? "", description: i.description ?? "", scope: i.scope ?? "", depends: i.depends ?? "", on: true }))); }) .catch((e) => { toast(e.message); onClose(); }); }, [sessionId]); if (!data) return (
); const chosen = items.filter((i) => i.on && i.title.trim()); const set = (k: number, patch: Partial) => setItems(items.map((x, j) => (j === k ? { ...x, ...patch } : x))); const create = async () => { setBusy(true); try { const r = await post<{ created: { number: number }[] }>(`/factory/issue-chats/${sessionId}/create`, { spec, start, issues: chosen.map(({ on, ...i }) => i), }); toast(t("issues.created", { n: r.created.length }), "success"); onClose(); navigate("/factory"); } catch (e: any) { toast(e.message); setBusy(false); } }; return (
e.stopPropagation()}>

{t("issues.create")}

{!data.proposal ? (
{t("issues.noProposal")}
) : ( <>
{items.map((i, k) => (
{i.scope}
{i.depends && i.depends.toLowerCase() !== "none" &&
{t("issues.depends", { d: i.depends })}
}
{t("issues.description")}