|
| 1 | +import { Workspace, CreateWorkspaceBuildRequest } from "api/typesGenerated"; |
| 2 | +import { useId, useState, FormEvent } from "react"; |
| 3 | +import { ConfirmDialog } from "components/Dialogs/ConfirmDialog/ConfirmDialog"; |
| 4 | +import { type Interpolation, type Theme } from "@emotion/react"; |
| 5 | +import { colors } from "theme/colors"; |
| 6 | +import TextField from "@mui/material/TextField"; |
| 7 | +import { docs } from "utils/docs"; |
| 8 | +import Link from "@mui/material/Link"; |
| 9 | +import Checkbox from "@mui/material/Checkbox"; |
| 10 | + |
| 11 | +const styles = { |
| 12 | + workspaceInfo: (theme) => ({ |
| 13 | + display: "flex", |
| 14 | + justifyContent: "space-between", |
| 15 | + backgroundColor: colors.gray[14], |
| 16 | + border: `1px solid ${theme.palette.divider}`, |
| 17 | + borderRadius: 8, |
| 18 | + padding: 12, |
| 19 | + marginBottom: 20, |
| 20 | + lineHeight: "1.3em", |
| 21 | + |
| 22 | + "& .name": { |
| 23 | + fontSize: 18, |
| 24 | + fontWeight: 800, |
| 25 | + color: theme.palette.text.primary, |
| 26 | + }, |
| 27 | + |
| 28 | + "& .label": { |
| 29 | + fontSize: 11, |
| 30 | + color: theme.palette.text.secondary, |
| 31 | + }, |
| 32 | + |
| 33 | + "& .info": { |
| 34 | + fontSize: 14, |
| 35 | + fontWeight: 500, |
| 36 | + color: theme.palette.text.primary, |
| 37 | + }, |
| 38 | + }), |
| 39 | + orphanContainer: () => ({ |
| 40 | + marginTop: 24, |
| 41 | + display: "flex", |
| 42 | + backgroundColor: colors.orange[15], |
| 43 | + justifyContent: "space-between", |
| 44 | + border: `1px solid ${colors.orange[11]}`, |
| 45 | + borderRadius: 8, |
| 46 | + padding: 12, |
| 47 | + lineHeight: "18px", |
| 48 | + |
| 49 | + "& .option": { |
| 50 | + color: colors.orange[11], |
| 51 | + "&.Mui-checked": { |
| 52 | + color: colors.orange[11], |
| 53 | + }, |
| 54 | + }, |
| 55 | + |
| 56 | + "& .info": { |
| 57 | + fontSize: "14px", |
| 58 | + color: colors.orange[10], |
| 59 | + fontWeight: 500, |
| 60 | + }, |
| 61 | + }), |
| 62 | +} satisfies Record<string, Interpolation<Theme>>; |
| 63 | + |
| 64 | +interface WorkspaceDeleteDialogProps { |
| 65 | + workspace: Workspace; |
| 66 | + canUpdateTemplate: boolean; |
| 67 | + isOpen: boolean; |
| 68 | + onCancel: () => void; |
| 69 | + onConfirm: (arg: CreateWorkspaceBuildRequest["orphan"]) => void; |
| 70 | + workspaceBuildDateStr: string; |
| 71 | +} |
| 72 | +export const WorkspaceDeleteDialog = (props: WorkspaceDeleteDialogProps) => { |
| 73 | + const { |
| 74 | + workspace, |
| 75 | + canUpdateTemplate, |
| 76 | + isOpen, |
| 77 | + onCancel, |
| 78 | + onConfirm, |
| 79 | + workspaceBuildDateStr, |
| 80 | + } = props; |
| 81 | + const hookId = useId(); |
| 82 | + const [userConfirmationText, setUserConfirmationText] = useState(""); |
| 83 | + const [orphanWorkspace, setOrphanWorkspace] = |
| 84 | + useState<CreateWorkspaceBuildRequest["orphan"]>(false); |
| 85 | + const [isFocused, setIsFocused] = useState(false); |
| 86 | + |
| 87 | + const deletionConfirmed = workspace.name === userConfirmationText; |
| 88 | + const onSubmit = (event: FormEvent) => { |
| 89 | + event.preventDefault(); |
| 90 | + if (deletionConfirmed) { |
| 91 | + onConfirm(orphanWorkspace); |
| 92 | + } |
| 93 | + }; |
| 94 | + |
| 95 | + const hasError = !deletionConfirmed && userConfirmationText.length > 0; |
| 96 | + const displayErrorMessage = hasError && !isFocused; |
| 97 | + const inputColor = hasError ? "error" : "primary"; |
| 98 | + |
| 99 | + return ( |
| 100 | + <ConfirmDialog |
| 101 | + type="delete" |
| 102 | + hideCancel={false} |
| 103 | + open={isOpen} |
| 104 | + title="Delete Workspace" |
| 105 | + onConfirm={() => onConfirm(orphanWorkspace)} |
| 106 | + onClose={onCancel} |
| 107 | + disabled={!deletionConfirmed} |
| 108 | + description={ |
| 109 | + <> |
| 110 | + <div css={styles.workspaceInfo}> |
| 111 | + <div> |
| 112 | + <p className="name">{workspace.name}</p> |
| 113 | + <p className="label">workspace</p> |
| 114 | + </div> |
| 115 | + <div> |
| 116 | + <p className="info">{workspaceBuildDateStr}</p> |
| 117 | + <p className="label">created</p> |
| 118 | + </div> |
| 119 | + </div> |
| 120 | + |
| 121 | + <p>Deleting this workspace is irreversible!</p> |
| 122 | + <p> |
| 123 | + Type “<strong>{workspace.name}</strong>“ below to |
| 124 | + confirm: |
| 125 | + </p> |
| 126 | + |
| 127 | + <form onSubmit={onSubmit}> |
| 128 | + <TextField |
| 129 | + fullWidth |
| 130 | + autoFocus |
| 131 | + css={{ marginTop: 32 }} |
| 132 | + name="confirmation" |
| 133 | + autoComplete="off" |
| 134 | + id={`${hookId}-confirm`} |
| 135 | + placeholder={workspace.name} |
| 136 | + value={userConfirmationText} |
| 137 | + onChange={(event) => setUserConfirmationText(event.target.value)} |
| 138 | + onFocus={() => setIsFocused(true)} |
| 139 | + onBlur={() => setIsFocused(false)} |
| 140 | + label="Workspace name" |
| 141 | + color={inputColor} |
| 142 | + error={displayErrorMessage} |
| 143 | + helperText={ |
| 144 | + displayErrorMessage && |
| 145 | + `${userConfirmationText} does not match the name of this workspace` |
| 146 | + } |
| 147 | + InputProps={{ color: inputColor }} |
| 148 | + inputProps={{ |
| 149 | + "data-testid": "delete-dialog-name-confirmation", |
| 150 | + }} |
| 151 | + /> |
| 152 | + {canUpdateTemplate && ( |
| 153 | + <div css={styles.orphanContainer}> |
| 154 | + <div css={{ flexDirection: "column" }}> |
| 155 | + <Checkbox |
| 156 | + id="orphan_resources" |
| 157 | + size="small" |
| 158 | + color="warning" |
| 159 | + onChange={() => { |
| 160 | + setOrphanWorkspace(!orphanWorkspace); |
| 161 | + }} |
| 162 | + className="option" |
| 163 | + name="orphan_resources" |
| 164 | + checked={orphanWorkspace} |
| 165 | + data-testid="orphan-checkbox" |
| 166 | + /> |
| 167 | + </div> |
| 168 | + <div css={{ flexDirection: "column" }}> |
| 169 | + <p className="info">Orphan resources</p> |
| 170 | + <span css={{ fontSize: "11px" }}> |
| 171 | + Skip resource cleanup. Resources such as volumes and virtual |
| 172 | + machines will not be destroyed. |
| 173 | + <Link |
| 174 | + href={docs("/workspaces#workspace-resources")} |
| 175 | + target="_blank" |
| 176 | + rel="noreferrer" |
| 177 | + > |
| 178 | + Learn more... |
| 179 | + </Link> |
| 180 | + </span> |
| 181 | + </div> |
| 182 | + </div> |
| 183 | + )} |
| 184 | + </form> |
| 185 | + </> |
| 186 | + } |
| 187 | + /> |
| 188 | + ); |
| 189 | +}; |
0 commit comments