Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/dashboard/src/workspaces/RenameWorkspaceModal.tsx
2500 views
1
/**
2
* Copyright (c) 2023 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 { FunctionComponent, useCallback, useState } from "react";
8
import Modal, { ModalBody, ModalFooter, ModalHeader } from "../components/Modal";
9
import { Button } from "@podkit/buttons/Button";
10
import { Workspace } from "@gitpod/public-api/lib/gitpod/v1/workspace_pb";
11
import { useUpdateWorkspaceMutation } from "../data/workspaces/update-workspace-mutation";
12
import { LoadingButton } from "@podkit/buttons/LoadingButton";
13
import { Workspace as WorkspaceProtocol } from "@gitpod/gitpod-protocol";
14
15
export function toWorkspaceName(name: string): string {
16
return WorkspaceProtocol.toWorkspaceName(name);
17
}
18
19
export function fromWorkspaceName(workspace?: Workspace): string | undefined {
20
return WorkspaceProtocol.fromWorkspaceName(workspace?.metadata?.name);
21
}
22
23
type Props = {
24
workspace: Workspace;
25
onClose(): void;
26
};
27
export const RenameWorkspaceModal: FunctionComponent<Props> = ({ workspace, onClose }) => {
28
const [errorMessage, setErrorMessage] = useState("");
29
const [name, setName] = useState(fromWorkspaceName(workspace) || "");
30
const updateWorkspace = useUpdateWorkspaceMutation();
31
32
const updateWorkspaceDescription = useCallback(async () => {
33
try {
34
if (name.length > 250) {
35
setErrorMessage("Name is too long for readability.");
36
return;
37
}
38
39
setErrorMessage("");
40
41
// Using mutateAsync here so we can close the modal after it completes successfully
42
await updateWorkspace.mutateAsync({ workspaceId: workspace.id, metadata: { name: toWorkspaceName(name) } });
43
44
// Close the modal
45
onClose();
46
} catch (error) {
47
setErrorMessage("Something went wrong. Please try renaming again.");
48
}
49
}, [name, onClose, updateWorkspace, workspace.id]);
50
51
return (
52
<Modal visible onClose={onClose} onSubmit={updateWorkspaceDescription}>
53
<ModalHeader>Rename Workspace</ModalHeader>
54
<ModalBody>
55
{errorMessage.length > 0 ? (
56
<div className="bg-kumquat-light rounded-md p-3 text-gitpod-red text-sm mb-2">{errorMessage}</div>
57
) : null}
58
<input
59
autoFocus
60
className="w-full truncate"
61
type="text"
62
value={name}
63
disabled={updateWorkspace.isLoading}
64
onChange={(e) => setName(e.target.value)}
65
/>
66
<div className="mt-1">
67
<p className="text-gray-500">Change the name to better identify the workspace.</p>
68
<p className="text-gray-500">Workspace URLs and endpoints will remain the same.</p>
69
</div>
70
</ModalBody>
71
<ModalFooter>
72
<Button variant="secondary" disabled={updateWorkspace.isLoading} onClick={onClose}>
73
Cancel
74
</Button>
75
<LoadingButton type="submit" loading={updateWorkspace.isLoading}>
76
Update Name
77
</LoadingButton>
78
</ModalFooter>
79
</Modal>
80
);
81
};
82
83