Skip to content

Commit 561c3bd

Browse files
perf(web): the prompt link page compiles with the React Compiler
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent a702563 commit 561c3bd

1 file changed

Lines changed: 59 additions & 31 deletions

File tree

‎apps/web/src/components/promptLinks/NewThreadLinkView.tsx‎

Lines changed: 59 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ type LinkState =
4949
readonly project: EnvironmentProject;
5050
};
5151

52+
type LinkLaunch =
53+
| { readonly kind: "thread"; readonly thread: ReturnType<typeof useThreadShells>[number] }
54+
| { readonly kind: "draft"; readonly project: EnvironmentProject }
55+
| { readonly kind: "scratch" }
56+
| { readonly kind: "settled" };
57+
5258
const openedLinkKeys = new Set<string>();
5359

5460
export function NewThreadLinkView({ link }: { readonly link: NewThreadLink }) {
@@ -67,7 +73,9 @@ export function NewThreadLinkView({ link }: { readonly link: NewThreadLink }) {
6773
const [state, setState] = useState<LinkState>(() =>
6874
openedLinkKeys.has(locationKey) ? { status: "opened" } : { status: "opening" },
6975
);
70-
const startedRef = useRef(false);
76+
const [started, setStarted] = useState(false);
77+
const [launch, setLaunch] = useState<LinkLaunch | null>(null);
78+
const launchedRef = useRef<LinkLaunch | null>(null);
7179

7280
const recentProjects = () =>
7381
sortScopedProjectsForSidebar(
@@ -125,8 +133,7 @@ export function NewThreadLinkView({ link }: { readonly link: NewThreadLink }) {
125133
);
126134
};
127135

128-
const openWithoutProject = async () => {
129-
setState({ status: "opening" });
136+
const openScratchDraft = async () => {
130137
const environmentId =
131138
scratchEnvironmentId(lastActiveEnvironmentId()) ?? scratchEnvironmentId(null);
132139
const project = environmentId === null ? null : await openScratchProject(environmentId);
@@ -143,6 +150,11 @@ export function NewThreadLinkView({ link }: { readonly link: NewThreadLink }) {
143150
);
144151
};
145152

153+
const openWithoutProject = () => {
154+
setState({ status: "opening" });
155+
void openScratchDraft();
156+
};
157+
146158
const cloneRepository = async (repository: PromptLinkRepository) => {
147159
const environmentId = lastActiveEnvironmentId();
148160
const environment = environments.find((entry) => entry.environmentId === environmentId);
@@ -203,44 +215,60 @@ export function NewThreadLinkView({ link }: { readonly link: NewThreadLink }) {
203215
await openDraft(project);
204216
};
205217

206-
const start = useEffectEvent(() => {
207-
if (startedRef.current) return;
208-
startedRef.current = true;
218+
if (ready && state.status === "opening" && !started) {
219+
setStarted(true);
220+
const pullRequestThread =
221+
link.kind === "pull-request"
222+
? findPromptLinkPullRequestThread(threads, link.pullRequest)
223+
: undefined;
224+
const repository = link.kind === "new" ? link.repository : link.pullRequest;
225+
const project =
226+
repository === null ? undefined : findPromptLinkProject(recentProjects(), repository);
227+
if (pullRequestThread !== undefined) {
228+
setState({ status: "opened" });
229+
setLaunch({ kind: "thread", thread: pullRequestThread });
230+
} else if (repository === null) {
231+
setLaunch({ kind: "scratch" });
232+
} else if (project === undefined) {
233+
setState({ status: "missing-repository", repository });
234+
setLaunch({ kind: "settled" });
235+
} else if (link.kind === "pull-request") {
236+
setState({ status: "pull-request-checkout", project });
237+
setLaunch({ kind: "settled" });
238+
} else {
239+
setLaunch({ kind: "draft", project });
240+
}
241+
}
242+
243+
const runLaunch = useEffectEvent((next: LinkLaunch) => {
209244
openedLinkKeys.add(locationKey);
210-
if (link.kind === "pull-request") {
211-
const thread = findPromptLinkPullRequestThread(threads, link.pullRequest);
212-
if (thread !== undefined) {
213-
setState({ status: "opened" });
214-
const threadRef = scopeThreadRef(thread.environmentId, thread.id);
245+
switch (next.kind) {
246+
case "thread": {
247+
const threadRef = scopeThreadRef(next.thread.environmentId, next.thread.id);
215248
if (link.prompt !== null) placeLinkedPrompt(threadRef, link.prompt);
216249
void navigate({
217250
to: "/$environmentId/$threadId",
218-
params: { environmentId: thread.environmentId, threadId: thread.id },
251+
params: { environmentId: next.thread.environmentId, threadId: next.thread.id },
219252
replace: true,
220253
});
221254
return;
222255
}
256+
case "scratch":
257+
void openScratchDraft();
258+
return;
259+
case "draft":
260+
void openDraft(next.project);
261+
return;
262+
case "settled":
223263
}
224-
const repository = link.kind === "new" ? link.repository : link.pullRequest;
225-
if (repository === null) {
226-
void openWithoutProject();
227-
return;
228-
}
229-
const project = findPromptLinkProject(recentProjects(), repository);
230-
if (project === undefined) {
231-
setState({ status: "missing-repository", repository });
232-
return;
233-
}
234-
if (link.kind === "pull-request") {
235-
setState({ status: "pull-request-checkout", project });
236-
return;
237-
}
238-
void openDraft(project);
239264
});
240265

241266
useEffect(() => {
242-
if (ready && state.status === "opening") start();
243-
}, [ready, state.status]);
267+
// Strict Mode replays effects; each launch opens at most once.
268+
if (launch === null || launchedRef.current === launch) return;
269+
launchedRef.current = launch;
270+
runLaunch(launch);
271+
}, [launch]);
244272

245273
switch (state.status) {
246274
case "opening":
@@ -253,7 +281,7 @@ export function NewThreadLinkView({ link }: { readonly link: NewThreadLink }) {
253281
variant="outline"
254282
onClick={() => {
255283
openedLinkKeys.delete(locationKey);
256-
startedRef.current = false;
284+
setStarted(false);
257285
setState({ status: "opening" });
258286
}}
259287
>
@@ -275,7 +303,7 @@ export function NewThreadLinkView({ link }: { readonly link: NewThreadLink }) {
275303
<Button size="sm" onClick={() => void cloneRepository(state.repository)}>
276304
Clone {name}
277305
</Button>
278-
<Button size="sm" variant="outline" onClick={() => void openWithoutProject()}>
306+
<Button size="sm" variant="outline" onClick={openWithoutProject}>
279307
Use without project
280308
</Button>
281309
</LinkPage>

0 commit comments

Comments
 (0)