import * as React from 'react'; import { AlertCircle } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { cn } from '@/lib/utils'; import { useTranslations } from 'next-intl'; /** * 错误展示组件属性 * 用于统一显示加载失败、请求失败等错误状态 */ 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, message, error, onRetry, retryText, icon: Icon = AlertCircle, className, showStack = false, }: ErrorDisplayProps) { const t = useTranslations('layout.error'); const displayTitle = title ?? t('loadFailed'); const displayRetryText = retryText ?? t('retry'); const errorMessage = message || error?.message || t('unknownError'); return ( {displayTitle} {errorMessage} {onRetry && ( {displayRetryText} )} {showStack && error?.stack && process.env.NODE_ENV === 'development' && ( {t('viewDetails')} {error.stack} )} ); } /** * 区域级错误组件 * 适合在较小的区域显示错误 * * @example * ```tsx * refetch()} * /> * ``` */ export function ErrorInline({ message, error, onRetry, className, }: Omit) { const t = useTranslations('layout.error'); const errorMessage = message || error?.message || t('unknownErrorShort'); return ( {errorMessage} {onRetry && ( {t('retry')} )} ); } /** * 页面级错误组件 * 适合作为页面主要内容的错误状态 * * @example * ```tsx * refetch()} * /> * ``` */ export function ErrorPage(props: ErrorDisplayProps) { return ( ); }
{displayTitle}
{errorMessage}
{error.stack}