Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/dashboard/src/start/start-workspace-options.ts
2500 views
1
/**
2
* Copyright (c) 2022 Gitpod GmbH. All rights reserved.
3
* Licensed under the GNU Affero General Public License (AGPL).
4
* See License.AGPL.txt in the project root for license information.
5
*/
6
7
import { IDESettings } from "@gitpod/gitpod-protocol";
8
9
export interface StartWorkspaceOptions {
10
workspaceClass?: string;
11
ideSettings?: IDESettings;
12
autostart?: boolean;
13
showExamples?: boolean;
14
}
15
export namespace StartWorkspaceOptions {
16
// The workspace class to use for the workspace. If not specified, the default workspace class is used.
17
export const WORKSPACE_CLASS = "workspaceClass";
18
19
// The editor to use for the workspace. If not specified, the default editor is used.
20
export const EDITOR = "editor";
21
22
// whether the workspace should automatically start
23
export const AUTOSTART = "autostart";
24
25
// whether to show example repositories
26
export const SHOW_EXAMPLES = "showExamples";
27
28
export function parseSearchParams(search: string): StartWorkspaceOptions {
29
const params = new URLSearchParams(search);
30
const options: StartWorkspaceOptions = {};
31
const workspaceClass = params.get(StartWorkspaceOptions.WORKSPACE_CLASS);
32
if (workspaceClass) {
33
options.workspaceClass = workspaceClass;
34
}
35
const editorParam = params.get(StartWorkspaceOptions.EDITOR);
36
if (editorParam) {
37
if (editorParam?.endsWith("-latest")) {
38
options.ideSettings = {
39
defaultIde: editorParam.slice(0, -7),
40
useLatestVersion: true,
41
};
42
} else {
43
options.ideSettings = {
44
defaultIde: editorParam,
45
useLatestVersion: false,
46
};
47
}
48
}
49
if (params.get(StartWorkspaceOptions.AUTOSTART)) {
50
options.autostart = params.get(StartWorkspaceOptions.AUTOSTART) === "true";
51
}
52
53
if (params.get(StartWorkspaceOptions.SHOW_EXAMPLES)) {
54
options.showExamples = params.get(StartWorkspaceOptions.SHOW_EXAMPLES) === "true";
55
}
56
57
return options;
58
}
59
60
export function toSearchParams(options: StartWorkspaceOptions): string {
61
const params = new URLSearchParams();
62
if (options.workspaceClass) {
63
params.set(StartWorkspaceOptions.WORKSPACE_CLASS, options.workspaceClass);
64
}
65
if (options.ideSettings && options.ideSettings.defaultIde) {
66
const ide = options.ideSettings.defaultIde;
67
const latest = options.ideSettings.useLatestVersion;
68
params.set(StartWorkspaceOptions.EDITOR, latest ? ide + "-latest" : ide);
69
}
70
if (options.autostart) {
71
params.set(StartWorkspaceOptions.AUTOSTART, "true");
72
}
73
if (options.showExamples) {
74
params.set(StartWorkspaceOptions.SHOW_EXAMPLES, "true");
75
}
76
return params.toString();
77
}
78
79
export function parseContextUrl(locationHash: string): string {
80
let result = locationHash.replace(/^[#/]+/, "").trim();
81
return result;
82
}
83
}
84
85