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 || "–"}
)}
); }