ErrorPage.tsx 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import React from 'react';
  2. import { useRouteError, useNavigate } from 'react-router';
  3. export const ErrorPage = () => {
  4. const navigate = useNavigate();
  5. const error = useRouteError() as any;
  6. const errorMessage = error?.statusText || error?.message || '未知错误';
  7. return (
  8. <div className="flex flex-col items-center justify-center min-h-screen bg-gradient-to-br from-gray-50 to-gray-100 dark:from-gray-900 dark:to-gray-800 p-4">
  9. <div className="w-full max-w-md bg-white dark:bg-gray-800 rounded-xl shadow-lg overflow-hidden transition-all duration-300 hover:shadow-xl">
  10. <div className="bg-red-50 dark:bg-red-900/30 px-6 py-4 border-b border-red-100 dark:border-red-800">
  11. <h1 className="text-2xl font-bold text-red-600 dark:text-red-400">发生错误</h1>
  12. </div>
  13. <div className="p-6">
  14. <div className="flex items-start mb-4">
  15. <div className="flex-shrink-0 bg-red-100 dark:bg-red-900/50 p-3 rounded-full">
  16. <svg className="w-8 h-8 text-red-500 dark:text-red-400" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
  17. <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M12 8v4m0 4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"></path>
  18. </svg>
  19. </div>
  20. <div className="ml-4">
  21. <h3 className="text-lg font-medium text-gray-900 dark:text-white">{error?.message || '未知错误'}</h3>
  22. {error?.stack && (
  23. <pre className="mt-2 text-xs text-gray-600 dark:text-gray-300 bg-gray-50 dark:bg-gray-700 p-3 rounded overflow-x-auto max-h-40">
  24. {error.stack}
  25. </pre>
  26. )}
  27. </div>
  28. </div>
  29. <div className="flex gap-4">
  30. <button
  31. onClick={() => navigate(0)}
  32. className="inline-flex items-center px-4 py-2 border border-transparent text-sm font-medium rounded-md shadow-sm text-white bg-red-600 hover:bg-red-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-red-500 transition-colors duration-200"
  33. >
  34. 重新加载
  35. </button>
  36. <button
  37. onClick={() => navigate('/')}
  38. className="inline-flex items-center px-4 py-2 border border-gray-300 dark:border-gray-600 shadow-sm text-sm font-medium rounded-md text-gray-700 dark:text-gray-300 bg-white dark:bg-gray-700 hover:bg-gray-50 dark:hover:bg-gray-600 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-red-500 transition-colors duration-200"
  39. >
  40. 返回首页
  41. </button>
  42. </div>
  43. </div>
  44. </div>
  45. </div>
  46. );
  47. };