"use client"

import { useEffect } from "react"
import { Button } from "@/components/ui/button"
import { AlertCircle } from "lucide-react"

export default function ErrorBoundary({
  error,
  reset,
}: {
  error: Error & { digest?: string }
  reset: () => void
}) {
  useEffect(() => {
    // 可以在这里添加错误日志上报
    console.error("Error:", error)
  }, [error])

  // 确保 error 存在且是一个对象
  const errorMessage = error?.message || "未知错误"
  const errorDigest = error?.digest || null

  return (
    <div className="flex min-h-[400px] flex-col items-center justify-center rounded-md border border-red-100 bg-red-50 p-8">
      <div className="flex h-12 w-12 items-center justify-center rounded-full bg-red-100">
        <AlertCircle className="h-6 w-6 text-red-600" />
      </div>
      <h2 className="mt-4 text-lg font-semibold text-red-600">出错了</h2>
      <p className="mt-2 text-sm text-slate-600">抱歉，加载过程中发生了错误</p>
      <p className="mt-1 text-sm text-slate-500">{errorMessage}</p>
      {errorDigest && <p className="mt-2 text-xs text-slate-500">错误代码: {errorDigest}</p>}
      <Button onClick={reset} className="mt-4 bg-red-600 text-white hover:bg-red-700">
        重试
      </Button>
    </div>
  )
}
