pageError.tsx 936 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import {createContext, useContext, useState} from 'react';
  2. import Alert from 'sentry/components/alert';
  3. const pageErrorContext = createContext<{
  4. setPageError: (error: string | undefined) => void;
  5. pageError?: string;
  6. }>({
  7. pageError: undefined,
  8. setPageError: (_: string | undefined) => {},
  9. });
  10. export const PageErrorProvider = ({children}: {children: React.ReactNode}) => {
  11. const [pageError, setPageError] = useState<string | undefined>();
  12. return (
  13. <pageErrorContext.Provider
  14. value={{
  15. pageError,
  16. setPageError,
  17. }}
  18. >
  19. {children}
  20. </pageErrorContext.Provider>
  21. );
  22. };
  23. export const PageErrorAlert = () => {
  24. const {pageError} = useContext(pageErrorContext);
  25. if (!pageError) {
  26. return null;
  27. }
  28. return (
  29. <Alert type="error" data-test-id="page-error-alert" showIcon>
  30. {pageError}
  31. </Alert>
  32. );
  33. };
  34. export const usePageError = () => useContext(pageErrorContext);