Skip to content

fix: ensure auto-workspace creation waits until all parameters are ready #12419

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions site/src/pages/CreateWorkspacePage/CreateWorkspacePage.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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,
Expand All @@ -122,11 +128,13 @@ const CreateWorkspacePage: FC = () => {
}
});

const autoStartReady =
mode === "auto" && (!autofillEnabled || userParametersQuery.isSuccess);
Comment on lines +131 to +132
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Defining this outside seemed like the more manageable thing to do, since this needs to evaluate twelve combinations:

  • The value of mode (form, auto, duplicate)
  • The value of autofillEnabled (true or false)
  • The value of userParametersQuery.isSuccess (true or false)

Combining them into a single boolean makes sure the effect only has to worry about three situations:

  1. Mounting render
  2. Value flips from false to true
  3. Value flips from true to false

useEffect(() => {
if (mode === "auto") {
if (autoStartReady) {
void automateWorkspaceCreation();
}
}, [automateWorkspaceCreation, mode]);
}, [automateWorkspaceCreation, autoStartReady]);

return (
<>
Expand Down