|
| 1 | +import { MissingBuildParameters } from "api/api"; |
| 2 | +import { updateWorkspace } from "api/queries/workspaces"; |
| 3 | +import type { |
| 4 | + TemplateVersion, |
| 5 | + Workspace, |
| 6 | + WorkspaceBuild, |
| 7 | + WorkspaceBuildParameter, |
| 8 | +} from "api/typesGenerated"; |
| 9 | +import { ConfirmDialog } from "components/Dialogs/ConfirmDialog/ConfirmDialog"; |
| 10 | +import { MemoizedInlineMarkdown } from "components/Markdown/Markdown"; |
| 11 | +import { UpdateBuildParametersDialog } from "pages/WorkspacePage/UpdateBuildParametersDialog"; |
| 12 | +import { type FC, useState } from "react"; |
| 13 | +import { useMutation, useQueryClient } from "react-query"; |
| 14 | + |
| 15 | +type UseWorkspaceUpdateOptions = { |
| 16 | + workspace: Workspace; |
| 17 | + latestVersion: TemplateVersion | undefined; |
| 18 | + onSuccess?: (build: WorkspaceBuild) => void; |
| 19 | + onError?: (error: unknown) => void; |
| 20 | +}; |
| 21 | + |
| 22 | +type UseWorkspaceUpdateResult = { |
| 23 | + update: () => void; |
| 24 | + isUpdating: boolean; |
| 25 | + dialogs: { |
| 26 | + updateConfirmation: UpdateConfirmationDialogProps; |
| 27 | + missingBuildParameters: MissingBuildParametersDialogProps; |
| 28 | + }; |
| 29 | +}; |
| 30 | + |
| 31 | +export const useWorkspaceUpdate = ({ |
| 32 | + workspace, |
| 33 | + latestVersion, |
| 34 | + onSuccess, |
| 35 | + onError, |
| 36 | +}: UseWorkspaceUpdateOptions): UseWorkspaceUpdateResult => { |
| 37 | + const queryClient = useQueryClient(); |
| 38 | + const [isConfirmingUpdate, setIsConfirmingUpdate] = useState(false); |
| 39 | + |
| 40 | + const updateWorkspaceOptions = updateWorkspace(workspace, queryClient); |
| 41 | + const updateWorkspaceMutation = useMutation({ |
| 42 | + ...updateWorkspaceOptions, |
| 43 | + onSuccess: (build: WorkspaceBuild) => { |
| 44 | + updateWorkspaceOptions.onSuccess(build); |
| 45 | + onSuccess?.(build); |
| 46 | + }, |
| 47 | + onError, |
| 48 | + }); |
| 49 | + |
| 50 | + const update = () => { |
| 51 | + setIsConfirmingUpdate(true); |
| 52 | + }; |
| 53 | + |
| 54 | + const confirmUpdate = (buildParameters: WorkspaceBuildParameter[] = []) => { |
| 55 | + updateWorkspaceMutation.mutate(buildParameters); |
| 56 | + setIsConfirmingUpdate(false); |
| 57 | + }; |
| 58 | + |
| 59 | + return { |
| 60 | + update, |
| 61 | + isUpdating: updateWorkspaceMutation.isLoading, |
| 62 | + dialogs: { |
| 63 | + updateConfirmation: { |
| 64 | + open: isConfirmingUpdate, |
| 65 | + onClose: () => setIsConfirmingUpdate(false), |
| 66 | + onConfirm: () => confirmUpdate(), |
| 67 | + latestVersion, |
| 68 | + }, |
| 69 | + missingBuildParameters: { |
| 70 | + error: updateWorkspaceMutation.error, |
| 71 | + onClose: () => { |
| 72 | + updateWorkspaceMutation.reset(); |
| 73 | + }, |
| 74 | + onUpdate: (buildParameters: WorkspaceBuildParameter[]) => { |
| 75 | + if (updateWorkspaceMutation.error instanceof MissingBuildParameters) { |
| 76 | + confirmUpdate(buildParameters); |
| 77 | + } |
| 78 | + }, |
| 79 | + }, |
| 80 | + }, |
| 81 | + }; |
| 82 | +}; |
| 83 | + |
| 84 | +type WorkspaceUpdateDialogsProps = { |
| 85 | + updateConfirmation: UpdateConfirmationDialogProps; |
| 86 | + missingBuildParameters: MissingBuildParametersDialogProps; |
| 87 | +}; |
| 88 | + |
| 89 | +export const WorkspaceUpdateDialogs: FC<WorkspaceUpdateDialogsProps> = ({ |
| 90 | + updateConfirmation, |
| 91 | + missingBuildParameters, |
| 92 | +}) => { |
| 93 | + return ( |
| 94 | + <> |
| 95 | + <UpdateConfirmationDialog {...updateConfirmation} /> |
| 96 | + <MissingBuildParametersDialog {...missingBuildParameters} /> |
| 97 | + </> |
| 98 | + ); |
| 99 | +}; |
| 100 | + |
| 101 | +type UpdateConfirmationDialogProps = { |
| 102 | + open: boolean; |
| 103 | + onClose: () => void; |
| 104 | + onConfirm: () => void; |
| 105 | + latestVersion?: TemplateVersion; |
| 106 | +}; |
| 107 | + |
| 108 | +const UpdateConfirmationDialog: FC<UpdateConfirmationDialogProps> = ({ |
| 109 | + latestVersion, |
| 110 | + ...dialogProps |
| 111 | +}) => { |
| 112 | + return ( |
| 113 | + <ConfirmDialog |
| 114 | + {...dialogProps} |
| 115 | + hideCancel={false} |
| 116 | + title="Update workspace?" |
| 117 | + confirmText="Update" |
| 118 | + description={ |
| 119 | + <div className="flex flex-col gap-2"> |
| 120 | + <p> |
| 121 | + Updating your workspace will start the workspace on the latest |
| 122 | + template version. This can{" "} |
| 123 | + <strong>delete non-persistent data</strong>. |
| 124 | + </p> |
| 125 | + {latestVersion?.message && ( |
| 126 | + <MemoizedInlineMarkdown allowedElements={["ol", "ul", "li"]}> |
| 127 | + {latestVersion.message} |
| 128 | + </MemoizedInlineMarkdown> |
| 129 | + )} |
| 130 | + </div> |
| 131 | + } |
| 132 | + /> |
| 133 | + ); |
| 134 | +}; |
| 135 | + |
| 136 | +type MissingBuildParametersDialogProps = { |
| 137 | + error: unknown; |
| 138 | + onClose: () => void; |
| 139 | + onUpdate: (buildParameters: WorkspaceBuildParameter[]) => void; |
| 140 | +}; |
| 141 | + |
| 142 | +const MissingBuildParametersDialog: FC<MissingBuildParametersDialogProps> = ({ |
| 143 | + error, |
| 144 | + ...dialogProps |
| 145 | +}) => { |
| 146 | + return ( |
| 147 | + <UpdateBuildParametersDialog |
| 148 | + missedParameters={ |
| 149 | + error instanceof MissingBuildParameters ? error.parameters : [] |
| 150 | + } |
| 151 | + open={error instanceof MissingBuildParameters} |
| 152 | + {...dialogProps} |
| 153 | + /> |
| 154 | + ); |
| 155 | +}; |
0 commit comments