Skip to content

Commit 66b5e67

Browse files
committed
[dashboard] simplify autostart
1 parent 8bf5797 commit 66b5e67

File tree

3 files changed

+53
-79
lines changed

3 files changed

+53
-79
lines changed

components/dashboard/src/data/workspaces/create-workspace-mutation.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,6 @@ export const useCreateWorkspaceMutation = () => {
2222
onError: (error) => {
2323
setIsStarting(false);
2424
},
25-
onSuccess: (result) => {
26-
if (result && result.createdWorkspaceId) {
27-
// successfully started a workspace, wait a bit before we allow to start another one
28-
setTimeout(() => {
29-
setIsStarting(false);
30-
}, 4000);
31-
} else {
32-
setIsStarting(false);
33-
}
34-
},
3525
});
3626
return {
3727
createWorkspace: (options: GitpodServer.CreateWorkspaceOptions) => {

components/dashboard/src/start/start-workspace-options.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export interface StartWorkspaceOptions {
1313
}
1414
export namespace StartWorkspaceOptions {
1515
// The workspace class to use for the workspace. If not specified, the default workspace class is used.
16-
export const WORKSPACE_CLASS = "workspaceClass";
16+
export const WORKSPACE_CLASS = "workspaceclass";
1717

1818
// The editor to use for the workspace. If not specified, the default editor is used.
1919
export const EDITOR = "editor";
@@ -22,7 +22,12 @@ export namespace StartWorkspaceOptions {
2222
export const AUTOSTART = "autostart";
2323

2424
export function parseSearchParams(search: string): StartWorkspaceOptions {
25-
const params = new URLSearchParams(search);
25+
const original = new URLSearchParams(search);
26+
const params = new URLSearchParams();
27+
// translate to lower case
28+
for (const [key, value] of original.entries()) {
29+
params.set(key.toLowerCase(), value);
30+
}
2631
const options: StartWorkspaceOptions = {};
2732
const workspaceClass = params.get(StartWorkspaceOptions.WORKSPACE_CLASS);
2833
if (workspaceClass) {
@@ -43,7 +48,7 @@ export namespace StartWorkspaceOptions {
4348
}
4449
}
4550
if (params.get(StartWorkspaceOptions.AUTOSTART)) {
46-
options.autostart = params.get(StartWorkspaceOptions.AUTOSTART) === "true";
51+
options.autostart = params.get(StartWorkspaceOptions.AUTOSTART) !== "false";
4752
}
4853
return options;
4954
}
@@ -58,8 +63,8 @@ export namespace StartWorkspaceOptions {
5863
const latest = options.ideSettings.useLatestVersion;
5964
params.set(StartWorkspaceOptions.EDITOR, latest ? ide + "-latest" : ide);
6065
}
61-
if (options.autostart) {
62-
params.set(StartWorkspaceOptions.AUTOSTART, "true");
66+
if (options.autostart !== undefined) {
67+
params.set(StartWorkspaceOptions.AUTOSTART, options.autostart.toString());
6368
}
6469
return params.toString();
6570
}

components/dashboard/src/workspaces/CreateWorkspacePage.tsx

Lines changed: 43 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,33 @@ import { ErrorCodes } from "@gitpod/gitpod-protocol/lib/messaging/error";
1010
import { Deferred } from "@gitpod/gitpod-protocol/lib/util/deferred";
1111
import { FC, FunctionComponent, useCallback, useContext, useEffect, useMemo, useState } from "react";
1212
import { useHistory, useLocation } from "react-router";
13-
import { Link } from "react-router-dom";
13+
import Alert from "../components/Alert";
14+
import { AuthorizeGit, useNeedsGitAuthorization } from "../components/AuthorizeGit";
1415
import { Button } from "../components/Button";
16+
import { LinkButton } from "../components/LinkButton";
1517
import Modal from "../components/Modal";
1618
import RepositoryFinder from "../components/RepositoryFinder";
1719
import SelectIDEComponent from "../components/SelectIDEComponent";
1820
import SelectWorkspaceClassComponent from "../components/SelectWorkspaceClassComponent";
1921
import { UsageLimitReachedModal } from "../components/UsageLimitReachedModal";
20-
import { CheckboxInputField } from "../components/forms/CheckboxInputField";
22+
import { InputField } from "../components/forms/InputField";
2123
import { Heading1 } from "../components/typography/headings";
2224
import { useAuthProviders } from "../data/auth-providers/auth-provider-query";
2325
import { useCurrentOrg } from "../data/organizations/orgs-query";
2426
import { useListProjectsQuery } from "../data/projects/list-projects-query";
2527
import { useCreateWorkspaceMutation } from "../data/workspaces/create-workspace-mutation";
2628
import { useListWorkspacesQuery } from "../data/workspaces/list-workspaces-query";
2729
import { useWorkspaceContext } from "../data/workspaces/resolve-context-query";
30+
import { useDirtyState } from "../hooks/use-dirty-state";
2831
import { openAuthorizeWindow } from "../provider-utils";
2932
import { getGitpodService, gitpodHostUrl } from "../service/service";
3033
import { StartWorkspaceError } from "../start/StartPage";
3134
import { VerifyModal } from "../start/VerifyModal";
3235
import { StartWorkspaceOptions } from "../start/start-workspace-options";
3336
import { UserContext, useCurrentUser } from "../user-context";
3437
import { SelectAccountModal } from "../user-settings/SelectAccountModal";
35-
import { settingsPathPreferences } from "../user-settings/settings.routes";
36-
import { WorkspaceEntry } from "./WorkspaceEntry";
37-
import { AuthorizeGit, useNeedsGitAuthorization } from "../components/AuthorizeGit";
3838
import { settingsPathIntegrations } from "../user-settings/settings.routes";
39-
import { useDirtyState } from "../hooks/use-dirty-state";
40-
import { LinkButton } from "../components/LinkButton";
41-
import { InputField } from "../components/forms/InputField";
42-
import Alert from "../components/Alert";
39+
import { WorkspaceEntry } from "./WorkspaceEntry";
4340

4441
export function CreateWorkspacePage() {
4542
const { user, setUser } = useContext(UserContext);
@@ -48,8 +45,14 @@ export function CreateWorkspacePage() {
4845
const workspaces = useListWorkspacesQuery({ limit: 50 });
4946
const location = useLocation();
5047
const history = useHistory();
48+
const [contextURL, setContextURL] = useState<string | undefined>(
49+
StartWorkspaceOptions.parseContextUrl(location.hash),
50+
);
5151
const props = StartWorkspaceOptions.parseSearchParams(location.search);
52-
const [autostart, setAutostart] = useState<boolean | undefined>(props.autostart);
52+
// we autostart when a contextURL is provided via the URL hash and autostart is not explicitly set to false
53+
const [autostart, setAutostart] = useState<boolean | undefined>(
54+
contextURL !== undefined && props.autostart !== false,
55+
);
5356
const createWorkspaceMutation = useCreateWorkspaceMutation();
5457

5558
const defaultLatestIde =
@@ -65,11 +68,7 @@ export function CreateWorkspacePage() {
6568
const defaultWorkspaceClass = props.workspaceClass;
6669
const [selectedWsClass, setSelectedWsClass, selectedWsClassIsDirty] = useDirtyState(defaultWorkspaceClass);
6770
const [errorWsClass, setErrorWsClass] = useState<string | undefined>(undefined);
68-
const [contextURL, setContextURL] = useState<string | undefined>(
69-
StartWorkspaceOptions.parseContextUrl(location.hash),
70-
);
7171
const workspaceContext = useWorkspaceContext(contextURL);
72-
const [rememberOptions, setRememberOptions] = useState(false);
7372
const needsGitAuthorization = useNeedsGitAuthorization();
7473

7574
const storeAutoStartOptions = useCallback(async () => {
@@ -87,24 +86,22 @@ export function CreateWorkspacePage() {
8786
// we only keep the last 20 options
8887
workspaceAutoStartOptions = workspaceAutoStartOptions.slice(-40);
8988

90-
if (rememberOptions) {
91-
workspaceAutoStartOptions.push({
92-
cloneURL,
93-
organizationId: currentOrg.id,
94-
ideSettings: {
95-
defaultIde: selectedIde,
96-
useLatestVersion: useLatestIde,
97-
},
98-
workspaceClass: selectedWsClass,
99-
});
100-
}
89+
workspaceAutoStartOptions.push({
90+
cloneURL,
91+
organizationId: currentOrg.id,
92+
ideSettings: {
93+
defaultIde: selectedIde,
94+
useLatestVersion: useLatestIde,
95+
},
96+
workspaceClass: selectedWsClass,
97+
});
10198
AdditionalUserData.set(user, {
10299
workspaceAutostartOptions: workspaceAutoStartOptions,
103100
});
104101
setUser(user);
105102
await getGitpodService().server.updateLoggedInUser(user);
106103
console.log("Stored autostart options", workspaceAutoStartOptions);
107-
}, [currentOrg, rememberOptions, selectedIde, selectedWsClass, setUser, useLatestIde, user, workspaceContext.data]);
104+
}, [currentOrg, selectedIde, selectedWsClass, setUser, useLatestIde, user, workspaceContext.data]);
108105

109106
// see if we have a matching project based on context url and project's repo url
110107
const project = useMemo(() => {
@@ -274,7 +271,6 @@ export function CreateWorkspacePage() {
274271
(e) => e.cloneURL === cloneURL && e.organizationId === currentOrg?.id,
275272
);
276273
if (rememberedOptions) {
277-
setRememberOptions(true);
278274
if (!selectedIdeIsDirty) {
279275
setSelectedIde(rememberedOptions.ideSettings?.defaultIde, false);
280276
setUseLatestIde(!!rememberedOptions.ideSettings?.useLatestVersion);
@@ -287,12 +283,15 @@ export function CreateWorkspacePage() {
287283
setAutostart(true);
288284
}
289285
} else {
290-
setRememberOptions(false);
291286
// reset the ide settings to the user's default IF they haven't changed it manually
292287
if (!selectedIdeIsDirty) {
293288
setSelectedIde(defaultIde, false);
294289
setUseLatestIde(defaultLatestIde);
295290
}
291+
292+
if (!selectedWsClassIsDirty) {
293+
setSelectedWsClass(defaultWorkspaceClass, false);
294+
}
296295
}
297296
// we only update the remembered options when the workspaceContext changes
298297
// eslint-disable-next-line react-hooks/exhaustive-deps
@@ -313,7 +312,14 @@ export function CreateWorkspacePage() {
313312

314313
// Derive if the continue button is disabled based on current state
315314
const continueButtonDisabled = useMemo(() => {
316-
if (workspaceContext.isLoading || !contextURL || contextURL.length === 0 || !!errorIde || !!errorWsClass) {
315+
if (
316+
createWorkspaceMutation.isStarting ||
317+
workspaceContext.isLoading ||
318+
!contextURL ||
319+
contextURL.length === 0 ||
320+
!!errorIde ||
321+
!!errorWsClass
322+
) {
317323
return true;
318324
}
319325
if (workspaceContext.error) {
@@ -326,7 +332,14 @@ export function CreateWorkspacePage() {
326332
}
327333

328334
return false;
329-
}, [contextURL, errorIde, errorWsClass, workspaceContext.error, workspaceContext.isLoading]);
335+
}, [
336+
contextURL,
337+
createWorkspaceMutation.isStarting,
338+
errorIde,
339+
errorWsClass,
340+
workspaceContext.error,
341+
workspaceContext.isLoading,
342+
]);
330343

331344
if (SelectAccountPayload.is(selectAccountError)) {
332345
return (
@@ -418,13 +431,6 @@ export function CreateWorkspacePage() {
418431
</Button>
419432
</div>
420433

421-
{workspaceContext.data && (
422-
<RememberOptions
423-
disabled={workspaceContext.isLoading || createWorkspaceMutation.isStarting}
424-
checked={rememberOptions}
425-
onChange={setRememberOptions}
426-
/>
427-
)}
428434
{existingWorkspaces.length > 0 && !createWorkspaceMutation.isStarting && (
429435
<div className="w-full flex flex-col justify-end px-6">
430436
<p className="mt-6 text-center text-base">Running workspaces on this revision</p>
@@ -446,33 +452,6 @@ export function CreateWorkspacePage() {
446452
);
447453
}
448454

449-
function RememberOptions(params: { disabled?: boolean; checked: boolean; onChange: (checked: boolean) => void }) {
450-
const { disabled, checked, onChange } = params;
451-
452-
return (
453-
<>
454-
<div className={"w-full flex justify-center mt-3 px-8 mx-2"}>
455-
<CheckboxInputField
456-
label="Autostart with these options for this repository."
457-
checked={checked}
458-
disabled={disabled}
459-
topMargin={false}
460-
onChange={onChange}
461-
/>
462-
</div>
463-
<div className={"w-full flex justify-center px-8 mx-2"}>
464-
<p className="text-gray-400 dark:text-gray-500 text-sm">
465-
Don't worry, you can reset this anytime in your{" "}
466-
<Link to={settingsPathPreferences} className="gp-link">
467-
preferences
468-
</Link>
469-
.
470-
</p>
471-
</div>
472-
</>
473-
);
474-
}
475-
476455
function tryAuthorize(host: string, scopes?: string[]): Promise<SelectAccountPayload | undefined> {
477456
const result = new Deferred<SelectAccountPayload | undefined>();
478457
openAuthorizeWindow({

0 commit comments

Comments
 (0)