|
| 1 | +import { Button } from "@/app/components/ui/button"; |
| 2 | +import { cn } from "@/app/lib/utils"; |
| 3 | +import type { ErrorReason } from "@/server/db/schemas/chat"; |
| 4 | +import { ProviderIcon } from "@lobehub/icons"; |
| 5 | +import { useNavigate } from "@tanstack/react-router"; |
| 6 | +import { RefreshCw, Settings } from "lucide-react"; |
| 7 | +import { useTranslation } from "react-i18next"; |
| 8 | + |
| 9 | +interface GenerationErrorItemProps { |
| 10 | + errorReason: ErrorReason; |
| 11 | + provider?: string; |
| 12 | + onRetry?: () => void; |
| 13 | + className?: string; |
| 14 | +} |
| 15 | + |
| 16 | +export function GenerationErrorItem({ errorReason, provider, onRetry, className }: GenerationErrorItemProps) { |
| 17 | + const { t } = useTranslation(); |
| 18 | + const navigate = useNavigate(); |
| 19 | + |
| 20 | + // Determine error type and corresponding messages |
| 21 | + const getErrorInfo = (reason: ErrorReason) => { |
| 22 | + switch (reason) { |
| 23 | + case "CONFIG_INVALID": |
| 24 | + case "CONFIG_ERROR": |
| 25 | + return { |
| 26 | + title: t("chat.generation.configError"), |
| 27 | + description: t("chat.generation.configErrorDesc"), |
| 28 | + buttonText: t("chat.generation.goToSettings"), |
| 29 | + buttonIcon: Settings, |
| 30 | + buttonAction: "config" as const, |
| 31 | + }; |
| 32 | + case "API_ERROR": |
| 33 | + return { |
| 34 | + title: t("chat.generation.apiError"), |
| 35 | + description: t("chat.generation.apiErrorDesc"), |
| 36 | + buttonText: t("chat.generation.retry"), |
| 37 | + buttonIcon: RefreshCw, |
| 38 | + buttonAction: "retry" as const, |
| 39 | + }; |
| 40 | + case "TIMEOUT": |
| 41 | + return { |
| 42 | + title: t("chat.generation.timeoutError"), |
| 43 | + description: t("chat.generation.timeoutErrorDesc"), |
| 44 | + buttonText: t("chat.generation.retry"), |
| 45 | + buttonIcon: RefreshCw, |
| 46 | + buttonAction: "retry" as const, |
| 47 | + }; |
| 48 | + default: |
| 49 | + return { |
| 50 | + title: t("chat.generation.unknownError"), |
| 51 | + description: t("chat.generation.unknownErrorDesc"), |
| 52 | + buttonText: t("chat.generation.retry"), |
| 53 | + buttonIcon: RefreshCw, |
| 54 | + buttonAction: "retry" as const, |
| 55 | + }; |
| 56 | + } |
| 57 | + }; |
| 58 | + |
| 59 | + const errorInfo = getErrorInfo(errorReason); |
| 60 | + |
| 61 | + const handleButtonClick = () => { |
| 62 | + if (errorInfo.buttonAction === "config") { |
| 63 | + // Navigate to provider settings |
| 64 | + if (provider) { |
| 65 | + navigate({ |
| 66 | + to: "/settings/provider/$providerId", |
| 67 | + params: { providerId: provider }, |
| 68 | + }); |
| 69 | + } else { |
| 70 | + navigate({ |
| 71 | + to: "/settings/provider", |
| 72 | + }); |
| 73 | + } |
| 74 | + } else { |
| 75 | + // Retry action |
| 76 | + onRetry?.(); |
| 77 | + } |
| 78 | + }; |
| 79 | + |
| 80 | + const ButtonIcon = errorInfo.buttonIcon; |
| 81 | + |
| 82 | + return ( |
| 83 | + <div className={cn("flex h-48 w-80 flex-col px-2 text-center", className)}> |
| 84 | + {/* Provider Icon Block */} |
| 85 | + {provider && ( |
| 86 | + <div className="mb-6 flex flex-1 items-center justify-center"> |
| 87 | + <ProviderIcon provider={provider} size={44} type="avatar" /> |
| 88 | + </div> |
| 89 | + )} |
| 90 | + |
| 91 | + {/* Error Content Block */} |
| 92 | + <div className="mb-6 flex flex-1 flex-col items-center justify-center space-y-2 px-2"> |
| 93 | + <h3 className="font-semibold text-foreground text-lg leading-tight">{errorInfo.title}</h3> |
| 94 | + <p className="text-muted-foreground text-sm leading-relaxed">{errorInfo.description}</p> |
| 95 | + </div> |
| 96 | + |
| 97 | + {/* Action Button Block */} |
| 98 | + <div className="flex flex-1 items-center justify-center"> |
| 99 | + <Button onClick={handleButtonClick} className="h-10 gap-2 px-6 font-medium text-sm"> |
| 100 | + <ButtonIcon className="h-4 w-4" /> |
| 101 | + {errorInfo.buttonText} |
| 102 | + </Button> |
| 103 | + </div> |
| 104 | + </div> |
| 105 | + ); |
| 106 | +} |
0 commit comments