ComponentError.tsx 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import { Alert, Button } from 'antd';
  2. import { FC } from 'react';
  3. export type ComponentErrorProps = {
  4. message?: string;
  5. componentName: string;
  6. details?: string;
  7. retryFunction?: () => void;
  8. };
  9. const openBugReport = () => {
  10. window.open(
  11. 'https://github.com/owncast/owncast/issues/new?assignees=&labels=&template=bug-report-feature-request.yml',
  12. '_blank',
  13. );
  14. };
  15. const ErrorContent = ({
  16. message,
  17. componentName,
  18. details,
  19. canRetry,
  20. }: {
  21. message: string;
  22. componentName: string;
  23. details: string;
  24. canRetry: boolean;
  25. }) => (
  26. <div>
  27. <p>
  28. There was an unexpected error. It would be appreciated if you would report this so it can be
  29. fixed in the future.
  30. </p>
  31. {!!canRetry && (
  32. <p>You may optionally retry, however functionality might not work as expected.</p>
  33. )}
  34. <code>
  35. <div>{message && `Error: ${message}`}</div>
  36. <div>Component: {componentName}</div>
  37. <div>{details && details}</div>
  38. </code>
  39. </div>
  40. );
  41. export const ComponentError: FC<ComponentErrorProps> = ({
  42. message,
  43. componentName,
  44. details,
  45. retryFunction,
  46. }: ComponentErrorProps) => (
  47. <Alert
  48. message="Error"
  49. showIcon
  50. description=<ErrorContent
  51. message={message}
  52. details={details}
  53. componentName={componentName}
  54. canRetry={!!retryFunction}
  55. />
  56. type="error"
  57. action={
  58. <>
  59. {retryFunction && (
  60. <Button ghost size="small" onClick={retryFunction}>
  61. Retry
  62. </Button>
  63. )}
  64. <Button ghost size="small" danger onClick={openBugReport}>
  65. Report Error
  66. </Button>
  67. </>
  68. }
  69. />
  70. );