import { useTranslations } from 'next-intl'; import * as React from 'react'; import { AlertCircle } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { cn } from '@/lib/utils'; /** * 错误展示组件属性 * 用于统一显示加载失败、请求失败等错误状态 */ export interface ErrorDisplayProps { title?: string; message?: string; error?: Error | null; onRetry?: () => void; retryText?: string; icon?: React.ComponentType<{ className?: string }>; className?: string; showStack?: boolean; } /** * 错误展示组件 * 用于统一显示加载失败、请求失败等错误状态 * * @example * ```tsx * // 基础用法 * * * // 带重试按钮 * refetch()} * /> * * // 自定义样式 * * ``` * @param {ErrorDisplayProps} props - 错误展示组件属性 * @returns {React.ReactNode} 错误展示组件 */ export function ErrorDisplay({ title = undefined, message, error, onRetry, retryText = undefined, icon: Icon = AlertCircle, className, showStack = false, }: ErrorDisplayProps) { const rt = useTranslations('runtime'); title ??= rt('load_failed_d1d04482'); retryText ??= rt('retry_b8784c8d'); const errorMessage = message || error?.message || rt('an_unknown_error_occurred_please_try_again_79a9f98a'); return ( {title} {errorMessage} {onRetry && ( {retryText} )} {showStack && error?.stack && process.env.NODE_ENV === 'development' && ( {rt('view_error_details_9e7e4e37')} {error.stack} )} ); } /** * 区域级错误组件 * 适合在较小的区域显示错误 * * @example * ```tsx * refetch()} * /> * ``` */ export function ErrorInline({ message, error, onRetry, className, }: Omit) { const rt = useTranslations('runtime'); const errorMessage = message || error?.message || rt('unknown_error_e83a8ca8'); return ( {errorMessage} {onRetry && ( {rt('retry_b8784c8d')} )} ); } /** * 页面级错误组件 * 适合作为页面主要内容的错误状态 * * @example * ```tsx * refetch()} * /> * ``` */ export function ErrorPage(props: ErrorDisplayProps) { return ( ); }
{title}
{errorMessage}
{error.stack}