From f96c0ab92d0f1a2dee6b1f4a3020569a68eee408 Mon Sep 17 00:00:00 2001 From: Kenneth Date: Tue, 3 Dec 2024 22:40:59 +0000 Subject: [PATCH] fix: workspace conflict error not showing --- web/src/workspaces/api.ts | 4 +- web/src/workspaces/new-workspace-dialog.tsx | 43 +++++++-------------- 2 files changed, 17 insertions(+), 30 deletions(-) diff --git a/web/src/workspaces/api.ts b/web/src/workspaces/api.ts index 6160be6..3fdd387 100644 --- a/web/src/workspaces/api.ts +++ b/web/src/workspaces/api.ts @@ -1,4 +1,4 @@ -import { fetchApi } from "@/api"; +import { fetchApi, type ApiError } from "@/api"; import type { QueryStatus } from "@/lib/query"; import { useCallback, useState } from "react"; import useSWR, { useSWRConfig } from "swr"; @@ -24,7 +24,7 @@ function useWorkspaces() { } function useCreateWorkspace() { - const [status, setStatus] = useState({ type: "idle" }); + const [status, setStatus] = useState>({ type: "idle" }); const { mutate } = useSWRConfig(); const createWorkspace = useCallback( diff --git a/web/src/workspaces/new-workspace-dialog.tsx b/web/src/workspaces/new-workspace-dialog.tsx index 79e33c5..86ef2f1 100644 --- a/web/src/workspaces/new-workspace-dialog.tsx +++ b/web/src/workspaces/new-workspace-dialog.tsx @@ -1,4 +1,4 @@ -import { API_ERROR_WORKSPACE_EXISTS, isApiErrorResponse } from "@/api"; +import { API_ERROR_WORKSPACE_EXISTS } from "@/api"; import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert"; import { Button } from "@/components/ui/button"; import { DialogFooter, DialogHeader } from "@/components/ui/dialog"; @@ -21,7 +21,6 @@ import { SelectTrigger, SelectValue, } from "@/components/ui/select"; -import { ToastAction } from "@/components/ui/toast"; import { useToast } from "@/hooks/use-toast"; import { useTemplateImages } from "@/templates/api"; import type { TemplateImage } from "@/templates/types"; @@ -136,36 +135,24 @@ function NewWorkspaceForm({ useEffect(() => { switch (status.type) { case "error": - if (isApiErrorResponse(status.error)) { - let toastTitle = ""; - switch (status.error.code) { - case API_ERROR_WORKSPACE_EXISTS: - toastTitle = "Workspace already exists."; - break; - default: - toastTitle = "Failed to create the workspace."; - break; + { + let toastTitle: string; + let toastDescription: string; + if ( + status.error.type === "BAD_REQUEST" && + status.error.details.code === API_ERROR_WORKSPACE_EXISTS + ) { + toastTitle = "Workspace already exists."; + toastDescription = status.error.details.error; + } else { + toastTitle = "Failed to create the workspace."; + toastDescription = "Unknown error"; } + toast({ variant: "destructive", title: toastTitle, - description: status.error.error, - }); - } else { - toast({ - variant: "destructive", - title: "Failed to create the workspace.", - description: "Unknown error", - action: ( - { - formRef.current?.requestSubmit(); - }} - altText="Try again" - > - Try again - - ), + description: toastDescription, }); } break;