|
| 1 | +import TextField from "@mui/material/TextField"; |
| 2 | +import { Box } from "@mui/system"; |
| 3 | +import { deleteWorkspace, startWorkspace, stopWorkspace } from "api/api"; |
| 4 | +import { Workspace } from "api/typesGenerated"; |
| 5 | +import { ConfirmDialog } from "components/Dialogs/ConfirmDialog/ConfirmDialog"; |
| 6 | +import { displayError } from "components/GlobalSnackbar/utils"; |
| 7 | +import { useState } from "react"; |
| 8 | +import { useMutation } from "react-query"; |
| 9 | +import { MONOSPACE_FONT_FAMILY } from "theme/constants"; |
| 10 | + |
| 11 | +export const useBatchActions = (options: { |
| 12 | + onSuccess: () => Promise<void>; |
| 13 | +}) => { |
| 14 | + const { onSuccess } = options; |
| 15 | + |
| 16 | + const startAllMutation = useMutation({ |
| 17 | + mutationFn: async (workspaces: Workspace[]) => { |
| 18 | + return Promise.all( |
| 19 | + workspaces.map((w) => |
| 20 | + startWorkspace(w.id, w.latest_build.template_version_id), |
| 21 | + ), |
| 22 | + ); |
| 23 | + }, |
| 24 | + onSuccess, |
| 25 | + onError: () => { |
| 26 | + displayError("Failed to start workspaces"); |
| 27 | + }, |
| 28 | + }); |
| 29 | + |
| 30 | + const stopAllMutation = useMutation({ |
| 31 | + mutationFn: async (workspaces: Workspace[]) => { |
| 32 | + return Promise.all(workspaces.map((w) => stopWorkspace(w.id))); |
| 33 | + }, |
| 34 | + onSuccess, |
| 35 | + onError: () => { |
| 36 | + displayError("Failed to stop workspaces"); |
| 37 | + }, |
| 38 | + }); |
| 39 | + |
| 40 | + const deleteAllMutation = useMutation({ |
| 41 | + mutationFn: async (workspaces: Workspace[]) => { |
| 42 | + return Promise.all(workspaces.map((w) => deleteWorkspace(w.id))); |
| 43 | + }, |
| 44 | + onSuccess, |
| 45 | + onError: () => { |
| 46 | + displayError("Failed to delete workspaces"); |
| 47 | + }, |
| 48 | + }); |
| 49 | + |
| 50 | + return { |
| 51 | + startAll: startAllMutation.mutateAsync, |
| 52 | + stopAll: stopAllMutation.mutateAsync, |
| 53 | + deleteAll: deleteAllMutation.mutateAsync, |
| 54 | + isLoading: |
| 55 | + startAllMutation.isLoading || |
| 56 | + stopAllMutation.isLoading || |
| 57 | + deleteAllMutation.isLoading, |
| 58 | + }; |
| 59 | +}; |
| 60 | + |
| 61 | +type BatchDeleteConfirmationProps = { |
| 62 | + checkedWorkspaces: Workspace[]; |
| 63 | + open: boolean; |
| 64 | + isLoading: boolean; |
| 65 | + onClose: () => void; |
| 66 | + onConfirm: () => void; |
| 67 | +}; |
| 68 | + |
| 69 | +export const BatchDeleteConfirmation = ( |
| 70 | + props: BatchDeleteConfirmationProps, |
| 71 | +) => { |
| 72 | + const { checkedWorkspaces, open, onClose, onConfirm, isLoading } = props; |
| 73 | + const [confirmation, setConfirmation] = useState({ value: "", error: false }); |
| 74 | + |
| 75 | + const confirmDeletion = () => { |
| 76 | + setConfirmation((c) => ({ ...c, error: false })); |
| 77 | + |
| 78 | + if (confirmation.value !== "DELETE") { |
| 79 | + setConfirmation((c) => ({ ...c, error: true })); |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + onConfirm(); |
| 84 | + }; |
| 85 | + |
| 86 | + return ( |
| 87 | + <ConfirmDialog |
| 88 | + type="delete" |
| 89 | + open={open} |
| 90 | + confirmLoading={isLoading} |
| 91 | + onConfirm={confirmDeletion} |
| 92 | + onClose={() => { |
| 93 | + onClose(); |
| 94 | + setConfirmation({ value: "", error: false }); |
| 95 | + }} |
| 96 | + title={`Delete ${checkedWorkspaces?.length} ${ |
| 97 | + checkedWorkspaces.length === 1 ? "workspace" : "workspaces" |
| 98 | + }`} |
| 99 | + description={ |
| 100 | + <form |
| 101 | + onSubmit={async (e) => { |
| 102 | + e.preventDefault(); |
| 103 | + confirmDeletion(); |
| 104 | + }} |
| 105 | + > |
| 106 | + <Box> |
| 107 | + Deleting these workspaces is irreversible! Are you sure you want to |
| 108 | + proceed? Type{" "} |
| 109 | + <Box |
| 110 | + component="code" |
| 111 | + sx={{ |
| 112 | + fontFamily: MONOSPACE_FONT_FAMILY, |
| 113 | + color: (theme) => theme.palette.text.primary, |
| 114 | + fontWeight: 600, |
| 115 | + }} |
| 116 | + > |
| 117 | + `DELETE` |
| 118 | + </Box>{" "} |
| 119 | + to confirm. |
| 120 | + </Box> |
| 121 | + <TextField |
| 122 | + value={confirmation.value} |
| 123 | + required |
| 124 | + autoFocus |
| 125 | + fullWidth |
| 126 | + inputProps={{ |
| 127 | + "aria-label": "Type DELETE to confirm", |
| 128 | + }} |
| 129 | + placeholder="Type DELETE to confirm" |
| 130 | + sx={{ mt: 2 }} |
| 131 | + onChange={(e) => { |
| 132 | + const value = e.currentTarget?.value; |
| 133 | + setConfirmation((c) => ({ ...c, value })); |
| 134 | + }} |
| 135 | + error={confirmation.error} |
| 136 | + helperText={confirmation.error && "Please type DELETE to confirm"} |
| 137 | + /> |
| 138 | + </form> |
| 139 | + } |
| 140 | + /> |
| 141 | + ); |
| 142 | +}; |
0 commit comments