Skip to content

Commit a92853c

Browse files
authored
fix: ensure auto-workspace creation waits until all parameters are ready (coder#12419)
* fix: ensure auto-workspace creation waits until all parameters are ready * refactor: move creation blocking logic to main callback * fix: let creation start if experimental feature is off
1 parent 0fe109d commit a92853c

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

site/src/pages/CreateWorkspacePage/CreateWorkspacePage.tsx

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { type FC, useCallback, useEffect, useState } from "react";
1+
import { type FC, useCallback, useEffect, useState, useRef } from "react";
22
import { Helmet } from "react-helmet-async";
33
import { useMutation, useQuery, useQueryClient } from "react-query";
44
import { useNavigate, useParams, useSearchParams } from "react-router-dom";
@@ -94,19 +94,25 @@ const CreateWorkspacePage: FC = () => {
9494
);
9595

9696
// Auto fill parameters
97+
const autofillEnabled = experiments.includes("auto-fill-parameters");
9798
const userParametersQuery = useQuery({
9899
queryKey: ["userParameters"],
99100
queryFn: () => getUserParameters(templateQuery.data!.id),
100-
enabled:
101-
experiments.includes("auto-fill-parameters") && templateQuery.isSuccess,
101+
enabled: autofillEnabled && templateQuery.isSuccess,
102102
});
103103
const autofillParameters = getAutofillParameters(
104104
searchParams,
105105
userParametersQuery.data ? userParametersQuery.data : [],
106106
);
107107

108+
const autoCreationStartedRef = useRef(false);
108109
const automateWorkspaceCreation = useEffectEvent(async () => {
110+
if (autoCreationStartedRef.current) {
111+
return;
112+
}
113+
109114
try {
115+
autoCreationStartedRef.current = true;
110116
const newWorkspace = await autoCreateWorkspaceMutation.mutateAsync({
111117
templateName,
112118
organizationId,
@@ -122,11 +128,13 @@ const CreateWorkspacePage: FC = () => {
122128
}
123129
});
124130

131+
const autoStartReady =
132+
mode === "auto" && (!autofillEnabled || userParametersQuery.isSuccess);
125133
useEffect(() => {
126-
if (mode === "auto") {
134+
if (autoStartReady) {
127135
void automateWorkspaceCreation();
128136
}
129-
}, [automateWorkspaceCreation, mode]);
137+
}, [automateWorkspaceCreation, autoStartReady]);
130138

131139
return (
132140
<>

0 commit comments

Comments
 (0)