2021-09-06 20:49:13 +00:00
|
|
|
import { Suspense, useEffect } from "react";
|
2021-07-31 14:33:18 +00:00
|
|
|
import {
|
|
|
|
AppProps,
|
|
|
|
ErrorBoundary,
|
|
|
|
AuthenticationError,
|
|
|
|
AuthorizationError,
|
|
|
|
ErrorFallbackProps,
|
2021-09-25 12:58:28 +00:00
|
|
|
RedirectError,
|
|
|
|
Routes,
|
2021-07-31 14:33:18 +00:00
|
|
|
useQueryErrorResetBoundary,
|
2021-08-28 22:14:18 +00:00
|
|
|
getConfig,
|
2021-09-06 20:49:13 +00:00
|
|
|
useSession,
|
2021-07-31 15:57:43 +00:00
|
|
|
} from "blitz";
|
2021-07-31 14:33:18 +00:00
|
|
|
|
2021-09-06 20:49:13 +00:00
|
|
|
import Sentry from "../../integrations/sentry";
|
|
|
|
import ErrorComponent from "../core/components/error-component";
|
2021-08-28 22:14:18 +00:00
|
|
|
import { usePanelbear } from "../core/hooks/use-panelbear";
|
2021-07-31 14:33:18 +00:00
|
|
|
|
2021-07-31 15:57:43 +00:00
|
|
|
import "app/core/styles/index.css";
|
2021-07-31 14:33:18 +00:00
|
|
|
|
2021-08-28 22:14:18 +00:00
|
|
|
const { publicRuntimeConfig } = getConfig();
|
|
|
|
|
2021-07-31 14:33:18 +00:00
|
|
|
export default function App({ Component, pageProps }: AppProps) {
|
2021-10-17 22:06:45 +00:00
|
|
|
const session = useSession({ suspense: false });
|
2021-08-28 22:14:18 +00:00
|
|
|
usePanelbear(publicRuntimeConfig.panelBear.siteId);
|
2021-09-06 20:49:13 +00:00
|
|
|
useEffect(() => {
|
|
|
|
if (session.userId) {
|
|
|
|
Sentry.setUser({
|
2021-10-01 21:05:07 +00:00
|
|
|
id: session.userId,
|
2021-09-06 20:49:13 +00:00
|
|
|
orgId: session.orgId,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}, [session]);
|
2021-08-28 22:14:18 +00:00
|
|
|
|
2021-07-31 15:57:43 +00:00
|
|
|
const getLayout = Component.getLayout || ((page) => page);
|
2021-07-31 14:33:18 +00:00
|
|
|
|
|
|
|
return (
|
2021-09-06 20:49:13 +00:00
|
|
|
<ErrorBoundary
|
|
|
|
onError={(error, componentStack) =>
|
|
|
|
Sentry.captureException(error, { contexts: { react: { componentStack } } })
|
|
|
|
}
|
|
|
|
FallbackComponent={RootErrorFallback}
|
|
|
|
onReset={useQueryErrorResetBoundary().reset}
|
|
|
|
>
|
2021-10-17 22:06:45 +00:00
|
|
|
{/* TODO: better default fallback */}
|
|
|
|
<Suspense fallback={null}>{getLayout(<Component {...pageProps} />)}</Suspense>
|
2021-07-31 14:33:18 +00:00
|
|
|
</ErrorBoundary>
|
2021-07-31 15:57:43 +00:00
|
|
|
);
|
2021-07-31 14:33:18 +00:00
|
|
|
}
|
|
|
|
|
2021-09-25 12:58:28 +00:00
|
|
|
function RootErrorFallback({ error }: ErrorFallbackProps) {
|
2021-07-31 14:33:18 +00:00
|
|
|
if (error instanceof AuthenticationError) {
|
2021-09-25 12:58:28 +00:00
|
|
|
throw new RedirectError(Routes.SignIn());
|
2021-07-31 14:33:18 +00:00
|
|
|
} else if (error instanceof AuthorizationError) {
|
2021-08-01 14:03:49 +00:00
|
|
|
return <ErrorComponent statusCode={error.statusCode} title="Sorry, you are not authorized to access this" />;
|
2021-07-31 14:33:18 +00:00
|
|
|
} else {
|
2021-08-01 14:03:49 +00:00
|
|
|
return <ErrorComponent statusCode={error.statusCode || 400} title={error.message || error.name} />;
|
2021-07-31 14:33:18 +00:00
|
|
|
}
|
|
|
|
}
|