SuspenseQueryComponent.tsx 1009 B

1234567891011121314151617181920212223242526272829
  1. import React, { PropsWithChildren } from 'react';
  2. import { ErrorBoundary } from 'react-error-boundary';
  3. import { Navigate } from 'react-router-dom';
  4. const ErrorComponent: React.FC<{ error: Error }> = ({ error }) => {
  5. const errorStatus = (error as unknown as Response)?.status
  6. ? (error as unknown as Response).status
  7. : '404';
  8. return <Navigate to={`/${errorStatus}`} />;
  9. };
  10. /**
  11. * @description
  12. * basic idea that you can not choose a wrong url, that is why you are safe, but when
  13. * the user tries to manipulate some url to get the the desired result and the BE returns 404
  14. * it will be propagated to this component and redirected
  15. *
  16. * !!NOTE!! But only use this Component for GET query Throw error cause maybe in the future inner functionality may change
  17. * */
  18. const SuspenseQueryComponent: React.FC<PropsWithChildren<unknown>> = ({
  19. children,
  20. }) => {
  21. return (
  22. <ErrorBoundary FallbackComponent={ErrorComponent}>{children}</ErrorBoundary>
  23. );
  24. };
  25. export default SuspenseQueryComponent;