Skip to content

feat: add opt-out option to new parameters form #17456

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 11 commits into from
Apr 21, 2025
Merged
Prev Previous commit
Next Next commit
:)
  • Loading branch information
aslilac committed Apr 18, 2025
commit 210f80cb17c58a0bad9d4a148d7b05a2b6047ba8
4 changes: 3 additions & 1 deletion site/src/api/queries/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ export const templateByName = (
): QueryOptions<Template> => {
return {
queryKey: templateByNameKey(organization, name),
queryFn: async () => API.getTemplateByName(organization, name),
queryFn: async () => {
return API.getTemplateByName(organization, name);
},
};
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { templateByName } from "api/queries/templates";
import { ErrorAlert } from "components/Alert/ErrorAlert";
import { Loader } from "components/Loader/Loader";
import { useDashboard } from "modules/dashboard/useDashboard";
import { createContext, type FC, useState } from "react";
import { type FC, createContext } from "react";
import { useQuery } from "react-query";
import { useParams } from "react-router-dom";
import CreateWorkspacePage from "./CreateWorkspacePage";
import CreateWorkspacePageExperimental from "./CreateWorkspacePageExperimental";
import { useParams } from "react-router-dom";
import { useQuery } from "react-query";
import { templateByName } from "api/queries/templates";
import { Loader } from "components/Loader/Loader";
import { ErrorAlert } from "components/Alert/ErrorAlert";

const CreateWorkspaceExperimentRouter: FC = () => {
const { experiments } = useDashboard();
Expand All @@ -20,35 +20,47 @@ const CreateWorkspaceExperimentRouter: FC = () => {
: { enabled: false },
);

const optOutQuery = useQuery(
templateQuery.data
? {
queryKey: [
organizationName,
"template",
templateQuery.data.id,
"optOut",
],
queryFn: () => ({
templateId: templateQuery.data.id,
optedOut:
localStorage.getItem(optOutKey(templateQuery.data.id)) === "true",
}),
}
: { enabled: false },
);

if (dynamicParametersEnabled) {
if (templateQuery.isLoading) {
if (optOutQuery.isLoading) {
return <Loader />;
}
if (!templateQuery.data) {
return <ErrorAlert error={templateQuery.error} />;
if (!optOutQuery.data) {
return <ErrorAlert error={optOutQuery.error} />;
}

const optOut = `parameters.${templateQuery.data.id}.optOut`;
const [optedOut, setOptedOut] = useState(
localStorage.getItem(optOut) == "true",
);

const toggleOptedOut = () => {
setOptedOut((prev) => {
const next = !prev;
localStorage.setItem(optOut, next.toString());
return next;
});
const key = optOutKey(optOutQuery.data.templateId);
const current = localStorage.getItem(key) === "true";
localStorage.setItem(key, (!current).toString());
optOutQuery.refetch();
};

return (
<CreateWorkspaceContext.Provider value={{ toggleOptedOut }}>
{optedOut ? (
<NewFormContext.Provider value={{ toggleOptedOut }}>
{optOutQuery.data.optedOut ? (
<CreateWorkspacePage />
) : (
<CreateWorkspacePageExperimental />
)}
</CreateWorkspaceContext.Provider>
</NewFormContext.Provider>
);
}

Expand All @@ -57,6 +69,8 @@ const CreateWorkspaceExperimentRouter: FC = () => {

export default CreateWorkspaceExperimentRouter;

const CreateWorkspaceContext = createContext<
const optOutKey = (id: string) => `parameters.${id}.optOut`;

export const NewFormContext = createContext<
{ toggleOptedOut: () => void } | undefined
>(undefined);
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,11 @@ const CreateWorkspacePageExperimental: FC = () => {
const templateQuery = useQuery(
templateByName(organizationName, templateName),
);
const templateVersionPresetsQuery = useQuery({
...templateVersionPresets(templateQuery.data?.active_version_id ?? ""),
enabled: templateQuery.data !== undefined,
});
const templateVersionPresetsQuery = useQuery(
templateQuery.data
? templateVersionPresets(templateQuery.data.active_version_id)
: { enabled: false },
);
const permissionsQuery = useQuery(
templateQuery.data
? checkAuthorization({
Expand Down
19 changes: 17 additions & 2 deletions site/src/pages/CreateWorkspacePage/CreateWorkspacePageView.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { Interpolation, Theme } from "@emotion/react";
import FormControlLabel from "@mui/material/FormControlLabel";
import FormHelperText from "@mui/material/FormHelperText";
import TextField from "@mui/material/TextField";
import type * as TypesGen from "api/typesGenerated";
Expand Down Expand Up @@ -29,7 +28,14 @@ import { Switch } from "components/Switch/Switch";
import { UserAutocomplete } from "components/UserAutocomplete/UserAutocomplete";
import { type FormikContextType, useFormik } from "formik";
import { generateWorkspaceName } from "modules/workspaces/generateWorkspaceName";
import { type FC, useCallback, useEffect, useMemo, useState } from "react";
import {
type FC,
useCallback,
useContext,
useEffect,
useMemo,
useState,
} from "react";
import {
getFormHelpers,
nameValidator,
Expand All @@ -47,6 +53,8 @@ import type {
} from "./CreateWorkspacePage";
import { ExternalAuthButton } from "./ExternalAuthButton";
import type { CreateWorkspacePermissions } from "./permissions";
import { NewFormContext } from "./CreateWorkspaceExperimentRouter";

export const Language = {
duplicationWarning:
"Duplicating a workspace only copies its parameters. No state from the old workspace is copied over.",
Expand Down Expand Up @@ -98,6 +106,7 @@ export const CreateWorkspacePageView: FC<CreateWorkspacePageViewProps> = ({
onSubmit,
onCancel,
}) => {
const newFormContext = useContext(NewFormContext);
const [owner, setOwner] = useState(defaultOwner);
const [suggestedName, setSuggestedName] = useState(() =>
generateWorkspaceName(),
Expand Down Expand Up @@ -238,6 +247,12 @@ export const CreateWorkspacePageView: FC<CreateWorkspacePageViewProps> = ({
</Stack>
</PageHeader>

{newFormContext && (
<button type="button" onClick={newFormContext.toggleOptedOut}>
Try out the new workspace creation flow ✨
</button>
)}

<HorizontalForm
name="create-workspace-form"
onSubmit={form.handleSubmit}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,14 @@ import {
useValidationSchemaForDynamicParameters,
} from "modules/workspaces/DynamicParameter/DynamicParameter";
import { generateWorkspaceName } from "modules/workspaces/generateWorkspaceName";
import { type FC, useCallback, useEffect, useId, useState } from "react";
import {
type FC,
useCallback,
useContext,
useEffect,
useId,
useState,
} from "react";
import { getFormHelpers, nameValidator } from "utils/formUtils";
import type { AutofillBuildParameter } from "utils/richParameters";
import * as Yup from "yup";
Expand All @@ -32,6 +39,7 @@ import type {
} from "./CreateWorkspacePage";
import { ExternalAuthButton } from "./ExternalAuthButton";
import type { CreateWorkspacePermissions } from "./permissions";
import { NewFormContext } from "./CreateWorkspaceExperimentRouter";

export interface CreateWorkspacePageViewExperimentalProps {
autofillParameters: AutofillBuildParameter[];
Expand Down Expand Up @@ -89,6 +97,7 @@ export const CreateWorkspacePageViewExperimental: FC<
owner,
setOwner,
}) => {
const newFormContext = useContext(NewFormContext);
const [suggestedName, setSuggestedName] = useState(() =>
generateWorkspaceName(),
);
Expand Down Expand Up @@ -270,6 +279,12 @@ export const CreateWorkspacePageViewExperimental: FC<
{template.deprecated && <Pill type="warning">Deprecated</Pill>}
</header>

{newFormContext && (
<button type="button" onClick={newFormContext.toggleOptedOut}>
Go back to the classic workspace creation flow
</button>
)}

<form
onSubmit={form.handleSubmit}
aria-label="Create workspace form"
Expand Down
Loading