( options: UseBatchActionsOptions, )
| 26 | }>; |
| 27 | |
| 28 | export function useBatchActions( |
| 29 | options: UseBatchActionsOptions, |
| 30 | ): UseBatchActionsResult { |
| 31 | const { onSuccess } = options; |
| 32 | |
| 33 | const startAllMutation = useMutation({ |
| 34 | mutationFn: (workspaces: readonly Workspace[]) => { |
| 35 | return Promise.all( |
| 36 | workspaces.map((w) => |
| 37 | API.startWorkspace(w.id, w.latest_build.template_version_id), |
| 38 | ), |
| 39 | ); |
| 40 | }, |
| 41 | onSuccess, |
| 42 | onError: (error) => { |
| 43 | toast.error("Failed to start workspaces.", { |
| 44 | description: getErrorDetail(error), |
| 45 | }); |
| 46 | }, |
| 47 | }); |
| 48 | |
| 49 | const stopAllMutation = useMutation({ |
| 50 | mutationFn: (workspaces: readonly Workspace[]) => { |
| 51 | return Promise.all(workspaces.map((w) => API.stopWorkspace(w.id))); |
| 52 | }, |
| 53 | onSuccess, |
| 54 | onError: (error) => { |
| 55 | toast.error("Failed to stop workspaces.", { |
| 56 | description: getErrorDetail(error), |
| 57 | }); |
| 58 | }, |
| 59 | }); |
| 60 | |
| 61 | const deleteAllMutation = useMutation({ |
| 62 | mutationFn: (workspaces: readonly Workspace[]) => { |
| 63 | return Promise.all(workspaces.map((w) => API.deleteWorkspace(w.id))); |
| 64 | }, |
| 65 | onSuccess, |
| 66 | onError: (error) => { |
| 67 | toast.error("Failed to delete some workspaces.", { |
| 68 | description: getErrorDetail(error), |
| 69 | }); |
| 70 | }, |
| 71 | }); |
| 72 | |
| 73 | const updateAllMutation = useMutation({ |
| 74 | mutationFn: (payload: UpdateAllPayload) => { |
| 75 | const { workspaces, isDynamicParametersEnabled } = payload; |
| 76 | return Promise.all( |
| 77 | workspaces |
| 78 | .filter((w) => w.outdated && !w.dormant_at) |
| 79 | .map((w) => API.updateWorkspace(w, [], isDynamicParametersEnabled)), |
| 80 | ); |
| 81 | }, |
| 82 | onSuccess, |
| 83 | onError: (error) => { |
| 84 | toast.error("Failed to update some workspaces.", { |
| 85 | description: getErrorDetail(error), |
no test coverage detected