Files
codecraft/web/src/components/FactoryDraft.tsx
T

186 lines
6.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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 (
<>
<div className="draft-bar">
<Icon name="scroll" />
<span>{t("draft.hint")}</span>
<span className="spacer" />
<button className="small primary" onClick={() => setOpen(true)}>
<Icon name="check" /> {t("draft.create")}
</button>
</div>
{open && <CreateProjectDialog sessionId={sessionId} onClose={() => setOpen(false)} />}
</>
);
}
function CreateProjectDialog({ sessionId, onClose }: { sessionId: string; onClose: () => void }) {
const t = useT();
const [draft, setDraft] = useState<Draft>();
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<Draft>(`/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 (
<div className="modal-backdrop">
<div className="modal">
<Spinner />
</div>
</div>
);
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 (
<div className="modal-backdrop" onClick={busy ? undefined : onClose}>
<div className="modal draft-modal" role="dialog" aria-label={t("draft.create")} onClick={(e) => e.stopPropagation()}>
<h3>{t("draft.create")}</h3>
{draft.connections.length === 0 ? (
<div className="notice err">
{t("draft.noConnection")}{" "}
<button className="link-btn" onClick={() => navigate("/factory")}>
{t("factory.tab.settings")}
</button>
</div>
) : (
<>
{!draft.spec.trim() && <div className="notice err">{t("draft.noSpec")}</div>}
{draft.spec.trim() && !draft.setup && <div className="notice">{t("draft.noSetup")}</div>}
<div className="factory-form">
<label>
<span>{t("draft.name")}</span>
<input
autoFocus
value={name}
onChange={(e) => {
setName(e.target.value);
if (!slugTouched) setSlug(slugify(e.target.value));
}}
/>
</label>
<label>
<span>{t("draft.slug")}</span>
<input
value={slug}
className="mono"
onChange={(e) => {
setSlug(slugify(e.target.value));
setSlugTouched(true);
}}
/>
</label>
<label style={{ gridColumn: "1 / -1" }}>
<span>{t("draft.description")}</span>
<input value={description} onChange={(e) => setDescription(e.target.value)} />
</label>
<label>
<span>{t("draft.connection")}</span>
<select
value={connection}
onChange={(e) => {
setConnection(e.target.value);
setOwner(draft.connections.find((c) => c.id === e.target.value)?.owner ?? "");
}}
>
{draft.connections.map((c) => (
<option key={c.id} value={c.id}>
{c.name || c.kind} ({c.kind})
</option>
))}
</select>
</label>
<label>
<span>{t("draft.owner")}</span>
<input value={owner} placeholder={t("draft.ownerPlaceholder")} onChange={(e) => setOwner(e.target.value)} />
</label>
</div>
<div className="hint" style={{ margin: "6px 0" }}>
{t("draft.private", { conn: conn?.name || "" })}
</div>
<details>
<summary>{t("draft.specPreview")}</summary>
<pre className="draft-spec">{draft.spec || "–"}</pre>
</details>
</>
)}
<div className="row" style={{ justifyContent: "flex-end", gap: 8, marginTop: 12 }}>
<button onClick={onClose} disabled={busy}>
{t("common.cancel")}
</button>
<button className="primary" onClick={create} disabled={!ready || busy}>
{busy ? <Spinner small /> : <Icon name="check" />} {t("draft.createGo")}
</button>
</div>
</div>
</div>
);
}