Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
315 lines
11 KiB
TypeScript
315 lines
11 KiB
TypeScript
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>
|
||
);
|
||
}
|
||
|
||
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 (
|
||
<>
|
||
<div className="draft-bar">
|
||
<Icon name="scroll" />
|
||
<span>{t("issues.hint")}</span>
|
||
<span className="spacer" />
|
||
<button className="small primary" onClick={() => setOpen(true)}>
|
||
<Icon name="check" /> {t("issues.create")}
|
||
</button>
|
||
</div>
|
||
{open && <CreateIssuesDialog sessionId={sessionId} onClose={() => setOpen(false)} />}
|
||
</>
|
||
);
|
||
}
|
||
|
||
function CreateIssuesDialog({ sessionId, onClose }: { sessionId: string; onClose: () => void }) {
|
||
const t = useT();
|
||
const [data, setData] = useState<IssueProposal>();
|
||
const [items, setItems] = useState<(ProposedIssue & { on: boolean })[]>([]);
|
||
const [spec, setSpec] = useState("");
|
||
const [start, setStart] = useState(false);
|
||
const [busy, setBusy] = useState(false);
|
||
|
||
useEffect(() => {
|
||
get<IssueProposal>(`/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 (
|
||
<div className="modal-backdrop">
|
||
<div className="modal">
|
||
<Spinner />
|
||
</div>
|
||
</div>
|
||
);
|
||
|
||
const chosen = items.filter((i) => i.on && i.title.trim());
|
||
const set = (k: number, patch: Partial<ProposedIssue & { on: boolean }>) => 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 (
|
||
<div className="modal-backdrop" onClick={busy ? undefined : onClose}>
|
||
<div className="modal draft-modal" role="dialog" aria-label={t("issues.create")} onClick={(e) => e.stopPropagation()}>
|
||
<h3>{t("issues.create")}</h3>
|
||
{!data.proposal ? (
|
||
<div className="notice err">{t("issues.noProposal")}</div>
|
||
) : (
|
||
<>
|
||
<label className="factory-form" style={{ display: "block" }}>
|
||
<span>{t("issues.spec")}</span>
|
||
<input className="mono" value={spec} onChange={(e) => setSpec(e.target.value)} />
|
||
</label>
|
||
<div className="issue-list">
|
||
{items.map((i, k) => (
|
||
<div key={k} className="issue-item">
|
||
<label className="row" style={{ gap: 8 }}>
|
||
<input type="checkbox" checked={i.on} onChange={(e) => set(k, { on: e.target.checked })} />
|
||
<input style={{ flex: 1 }} value={i.title} onChange={(e) => set(k, { title: e.target.value })} />
|
||
</label>
|
||
<div className="hint">{i.scope}</div>
|
||
{i.depends && i.depends.toLowerCase() !== "none" && <div className="hint">{t("issues.depends", { d: i.depends })}</div>}
|
||
<details>
|
||
<summary>{t("issues.description")}</summary>
|
||
<textarea rows={4} value={i.description} onChange={(e) => set(k, { description: e.target.value })} />
|
||
</details>
|
||
</div>
|
||
))}
|
||
</div>
|
||
<label className="row" style={{ gap: 8, marginTop: 8 }}>
|
||
<input type="checkbox" checked={start} onChange={(e) => setStart(e.target.checked)} />
|
||
<span>{t("issues.start")}</span>
|
||
</label>
|
||
<div className="hint">{t("issues.startHint")}</div>
|
||
</>
|
||
)}
|
||
<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={!data.proposal || chosen.length === 0 || !spec.trim() || busy}>
|
||
{busy ? <Spinner small /> : <Icon name="check" />} {t("issues.createGo", { n: chosen.length })}
|
||
</button>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
);
|
||
}
|