fix: ensure auto-workspace creation waits until all parameters are ready (#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
This commit is contained in:
Michael Smith 2024-03-05 18:42:50 -05:00 committed by GitHub
parent 0fe109d517
commit a92853c72d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 13 additions and 5 deletions

View File

@ -1,4 +1,4 @@
import { type FC, useCallback, useEffect, useState } from "react";
import { type FC, useCallback, useEffect, useState, useRef } from "react";
import { Helmet } from "react-helmet-async";
import { useMutation, useQuery, useQueryClient } from "react-query";
import { useNavigate, useParams, useSearchParams } from "react-router-dom";
@ -94,19 +94,25 @@ const CreateWorkspacePage: FC = () => {
);
// Auto fill parameters
const autofillEnabled = experiments.includes("auto-fill-parameters");
const userParametersQuery = useQuery({
queryKey: ["userParameters"],
queryFn: () => getUserParameters(templateQuery.data!.id),
enabled:
experiments.includes("auto-fill-parameters") && templateQuery.isSuccess,
enabled: autofillEnabled && templateQuery.isSuccess,
});
const autofillParameters = getAutofillParameters(
searchParams,
userParametersQuery.data ? userParametersQuery.data : [],
);
const autoCreationStartedRef = useRef(false);
const automateWorkspaceCreation = useEffectEvent(async () => {
if (autoCreationStartedRef.current) {
return;
}
try {
autoCreationStartedRef.current = true;
const newWorkspace = await autoCreateWorkspaceMutation.mutateAsync({
templateName,
organizationId,
@ -122,11 +128,13 @@ const CreateWorkspacePage: FC = () => {
}
});
const autoStartReady =
mode === "auto" && (!autofillEnabled || userParametersQuery.isSuccess);
useEffect(() => {
if (mode === "auto") {
if (autoStartReady) {
void automateWorkspaceCreation();
}
}, [automateWorkspaceCreation, mode]);
}, [automateWorkspaceCreation, autoStartReady]);
return (
<>