Open History to see the last commit
1import { useEffect, useRef, useState } from "react";
2import { ArrowUpRight, LoaderCircle, X } from "lucide-react";
3import type { AIGeneration } from "../shared/types";
4import { api } from "./store";
5import type { Theme } from "./theme";
6
7export default function AIBuildDialog({
8 onClose,
9}: {
10 theme: Theme;
11 onClose: () => void;
12}) {
13 const dialog = useRef<HTMLDialogElement>(null);
14 const [prompt, setPrompt] = useState("");
15 const [busy, setBusy] = useState(false);
16 const [error, setError] = useState("");
17 const reservation = useRef<{ id: string; prompt: string } | null>(null);
18 useEffect(() => {
19 dialog.current?.showModal();
20 }, []);
21 async function build() {
22 if (busy || prompt.trim().length < 3) return;
23 setBusy(true);
24 setError("");
25 // Reuse this operation on a lost response; never create a second workshop.
26 if (!reservation.current || reservation.current.prompt !== prompt.trim())
27 reservation.current = { id: crypto.randomUUID(), prompt: prompt.trim() };
28 const input = reservation.current;
29 try {
30 const row = await api<AIGeneration>("/ai/generations", {
31 method: "POST",
32 body: JSON.stringify({ ...input, newWorld: true }),
33 }).catch(async (e) => {
34 try {
35 return await api<AIGeneration>(`/ai/generations/${input.id}`);
36 } catch {
37 throw e;
38 }
39 });
40 if (!row.target_slug)
41 throw new Error(
42 "The AI workshop could not be opened. Please try again.",
43 );
44 location.assign(`/w/${row.target_slug}?ai=1&frame=1`);
45 } catch (e) {
46 setError((e as Error).message);
47 setBusy(false);
48 }
49 }
50 return (
51 <dialog
52 ref={dialog}
53 className="ai-dialog"
54 aria-labelledby="ai-title"
55 onCancel={(e) => {
56 e.preventDefault();
57 if (!busy) onClose();
58 }}
59 onKeyDown={(e) => e.stopPropagation()}
60 >
61 <button
62 className="modal-close"
63 aria-label="Close AI builder"
64 disabled={busy}
65 onClick={onClose}
66 >
67 <X size={18} />
68 </button>
69 <div className="ai-copy">
70 <h2 id="ai-title">What shall we build?</h2>
71 <p className="ai-intro">
72 Start a model, then keep shaping it together.
73 </p>
74 <form
75 onSubmit={(e) => {
76 e.preventDefault();
77 void build();
78 }}
79 >
80 <label className="sr-only" htmlFor="ai-prompt">
81 What would you like to build?
82 </label>
83 <textarea
84 id="ai-prompt"
85 autoFocus
86 rows={4}
87 maxLength={1200}
88 placeholder="A cosy cabin with a red roof and a little pine tree…"
89 value={prompt}
90 disabled={busy}
91 onChange={(e) => setPrompt(e.target.value)}
92 />
93 <div className="ai-examples">
94 {[
95 "A little forest lookout with a red roof",
96 "A yellow exploration rover with chunky wheels",
97 "A seaside lighthouse with a keeper's house",
98 ].map((example) => (
99 <button
100 key={example}
101 type="button"
102 disabled={busy}
103 onClick={() => setPrompt(example)}
104 >
105 {example}
106 <ArrowUpRight size={12} />
107 </button>
108 ))}
109 </div>
110 {error && (
111 <p className="ai-error" role="alert">
112 {error}
113 </p>
114 )}
115 <div className="ai-actions">
116 <button
117 className="primary-button"
118 disabled={busy || prompt.trim().length < 3}
119 >
120 {busy ? (
121 <>
122 <LoaderCircle size={16} className="spin" />
123 Opening your canvas…
124 </>
125 ) : (
126 <>
127 Build
128 <ArrowUpRight size={16} />
129 </>
130 )}
131 </button>
132 </div>
133 </form>
134 </div>
135 </dialog>
136 );
137}