Path: blob/main/components/dashboard/src/workspaces/RenameWorkspaceModal.tsx
2500 views
/**1* Copyright (c) 2023 Gitpod GmbH. All rights reserved.2* Licensed under the GNU Affero General Public License (AGPL).3* See License.AGPL.txt in the project root for license information.4*/56import { FunctionComponent, useCallback, useState } from "react";7import Modal, { ModalBody, ModalFooter, ModalHeader } from "../components/Modal";8import { Button } from "@podkit/buttons/Button";9import { Workspace } from "@gitpod/public-api/lib/gitpod/v1/workspace_pb";10import { useUpdateWorkspaceMutation } from "../data/workspaces/update-workspace-mutation";11import { LoadingButton } from "@podkit/buttons/LoadingButton";12import { Workspace as WorkspaceProtocol } from "@gitpod/gitpod-protocol";1314export function toWorkspaceName(name: string): string {15return WorkspaceProtocol.toWorkspaceName(name);16}1718export function fromWorkspaceName(workspace?: Workspace): string | undefined {19return WorkspaceProtocol.fromWorkspaceName(workspace?.metadata?.name);20}2122type Props = {23workspace: Workspace;24onClose(): void;25};26export const RenameWorkspaceModal: FunctionComponent<Props> = ({ workspace, onClose }) => {27const [errorMessage, setErrorMessage] = useState("");28const [name, setName] = useState(fromWorkspaceName(workspace) || "");29const updateWorkspace = useUpdateWorkspaceMutation();3031const updateWorkspaceDescription = useCallback(async () => {32try {33if (name.length > 250) {34setErrorMessage("Name is too long for readability.");35return;36}3738setErrorMessage("");3940// Using mutateAsync here so we can close the modal after it completes successfully41await updateWorkspace.mutateAsync({ workspaceId: workspace.id, metadata: { name: toWorkspaceName(name) } });4243// Close the modal44onClose();45} catch (error) {46setErrorMessage("Something went wrong. Please try renaming again.");47}48}, [name, onClose, updateWorkspace, workspace.id]);4950return (51<Modal visible onClose={onClose} onSubmit={updateWorkspaceDescription}>52<ModalHeader>Rename Workspace</ModalHeader>53<ModalBody>54{errorMessage.length > 0 ? (55<div className="bg-kumquat-light rounded-md p-3 text-gitpod-red text-sm mb-2">{errorMessage}</div>56) : null}57<input58autoFocus59className="w-full truncate"60type="text"61value={name}62disabled={updateWorkspace.isLoading}63onChange={(e) => setName(e.target.value)}64/>65<div className="mt-1">66<p className="text-gray-500">Change the name to better identify the workspace.</p>67<p className="text-gray-500">Workspace URLs and endpoints will remain the same.</p>68</div>69</ModalBody>70<ModalFooter>71<Button variant="secondary" disabled={updateWorkspace.isLoading} onClick={onClose}>72Cancel73</Button>74<LoadingButton type="submit" loading={updateWorkspace.isLoading}>75Update Name76</LoadingButton>77</ModalFooter>78</Modal>79);80};818283