|
5 | 5 | */
|
6 | 6 |
|
7 | 7 | import { StartOptions } from "@gitpod/gitpod-protocol/lib/util/gitpod-host-url";
|
8 |
| -import { useCallback, useContext, useEffect, useState } from "react"; |
9 |
| -import { useLocation } from "react-router"; |
| 8 | +import { useCallback, useContext, useMemo, useState } from "react"; |
10 | 9 | import Modal from "../components/Modal";
|
11 | 10 | import RepositoryFinder from "../components/RepositoryFinder";
|
12 | 11 | import SelectIDEComponent from "../components/SelectIDEComponent";
|
13 | 12 | import SelectWorkspaceClassComponent from "../components/SelectWorkspaceClassComponent";
|
14 | 13 | import { UserContext } from "../user-context";
|
15 |
| -import { StartWorkspaceModalContext } from "./start-workspace-modal-context"; |
16 | 14 |
|
17 |
| -export function StartWorkspaceModal() { |
| 15 | +export interface StartWorkspaceModalProps { |
| 16 | + uselatestIde?: boolean; |
| 17 | + ide?: string; |
| 18 | + workspaceClass?: string; |
| 19 | + contextUrl?: string; |
| 20 | + onClose?: () => void; |
| 21 | +} |
| 22 | + |
| 23 | +export function StartWorkspaceModal(props: StartWorkspaceModalProps) { |
18 | 24 | const { user } = useContext(UserContext);
|
19 |
| - const { isStartWorkspaceModalVisible, setIsStartWorkspaceModalVisible } = useContext(StartWorkspaceModalContext); |
20 |
| - const location = useLocation(); |
21 | 25 | const [useLatestIde, setUseLatestIde] = useState<boolean | undefined>(
|
22 |
| - !!user?.additionalData?.ideSettings?.useLatestVersion, |
| 26 | + props.uselatestIde || !!user?.additionalData?.ideSettings?.useLatestVersion, |
23 | 27 | );
|
24 |
| - const [selectedIde, setSelectedIde] = useState(user?.additionalData?.ideSettings?.defaultIde); |
25 |
| - const [selectedWsClass, setSelectedWsClass] = useState<string | undefined>(); |
26 |
| - const [repo, setRepo] = useState<string | undefined>(undefined); |
| 28 | + const [selectedIde, setSelectedIde] = useState(props.ide || user?.additionalData?.ideSettings?.defaultIde); |
| 29 | + const [selectedWsClass, setSelectedWsClass] = useState<string | undefined>(props.workspaceClass); |
| 30 | + const [repo, setRepo] = useState<string | undefined>(props.contextUrl); |
27 | 31 | const onSelectEditorChange = useCallback(
|
28 | 32 | (ide: string, useLatest: boolean) => {
|
29 | 33 | setSelectedIde(ide);
|
@@ -52,37 +56,34 @@ export function StartWorkspaceModal() {
|
52 | 56 | return true;
|
53 | 57 | }, [repo, selectedIde, selectedWsClass, useLatestIde]);
|
54 | 58 |
|
55 |
| - // Close the modal on navigation events. |
56 |
| - useEffect(() => { |
57 |
| - setIsStartWorkspaceModalVisible(false); |
58 |
| - }, [location, setIsStartWorkspaceModalVisible]); |
59 |
| - |
60 |
| - useEffect(() => { |
61 |
| - // reset state when visibility changes. |
62 |
| - setSelectedIde(user?.additionalData?.ideSettings?.defaultIde); |
63 |
| - setUseLatestIde(!!user?.additionalData?.ideSettings?.useLatestVersion); |
64 |
| - setRepo(undefined); |
65 |
| - }, [user, setSelectedIde, setUseLatestIde, isStartWorkspaceModalVisible]); |
| 59 | + const buttons = useMemo(() => { |
| 60 | + const result = [ |
| 61 | + <button key="cancel" className="secondary" onClick={props.onClose}> |
| 62 | + Cancel |
| 63 | + </button>, |
| 64 | + <button key="start" className="" onClick={startWorkspace} disabled={!repo || repo.length === 0}> |
| 65 | + New Workspace |
| 66 | + </button>, |
| 67 | + ]; |
| 68 | + if (!props.onClose) { |
| 69 | + return result.slice(1, 2); |
| 70 | + } |
| 71 | + return result; |
| 72 | + }, [props.onClose, repo, startWorkspace]); |
66 | 73 |
|
67 | 74 | return (
|
68 | 75 | <Modal
|
69 |
| - onClose={() => setIsStartWorkspaceModalVisible(false)} |
| 76 | + onClose={props.onClose || (() => {})} |
| 77 | + closeable={!!props.onClose} |
70 | 78 | onEnter={startWorkspace}
|
71 |
| - visible={!!isStartWorkspaceModalVisible} |
| 79 | + visible={true} |
72 | 80 | title="Open in Gitpod"
|
73 |
| - buttons={[ |
74 |
| - <button key="cancel" className="secondary" onClick={() => setIsStartWorkspaceModalVisible(false)}> |
75 |
| - Cancel |
76 |
| - </button>, |
77 |
| - <button key="start" className="" onClick={startWorkspace} disabled={!repo || repo.length === 0}> |
78 |
| - New Workspace |
79 |
| - </button>, |
80 |
| - ]} |
| 81 | + buttons={buttons} |
81 | 82 | >
|
82 | 83 | <div className="-mx-6 px-6">
|
83 |
| - <div className="text-xs text-gray-500">Select a repository and configure workspace options.</div> |
| 84 | + <div className="text-xs text-gray-500">Start a new workspace with the following options.</div> |
84 | 85 | <div className="pt-3">
|
85 |
| - <RepositoryFinder setSelection={setRepo} initialValue={repo} /> |
| 86 | + <RepositoryFinder setSelection={props.contextUrl ? undefined : setRepo} initialValue={repo} /> |
86 | 87 | </div>
|
87 | 88 | <div className="pt-3">
|
88 | 89 | <SelectIDEComponent
|
|
0 commit comments