Files
AFFiNE-Mirror/packages/frontend/core/src/router.tsx
forehalo bffc294620 fix(core): oauth popup blocked in safari (#8144)
fix(core): oauth popup blocked in safari

fix(core): standarize auth routes

fix AF-1352
2024-09-06 08:26:27 +00:00

193 lines
4.8 KiB
TypeScript

import { wrapCreateBrowserRouter } from '@sentry/react';
import { useEffect, useState } from 'react';
import type { RouteObject } from 'react-router-dom';
import {
createBrowserRouter as reactRouterCreateBrowserRouter,
redirect,
// eslint-disable-next-line @typescript-eslint/no-restricted-imports
useNavigate,
} from 'react-router-dom';
import { NavigateContext } from './hooks/use-navigate-helper';
import { RootWrapper } from './pages/root';
export function RootRouter() {
const navigate = useNavigate();
const [ready, setReady] = useState(false);
useEffect(() => {
// a hack to make sure router is ready
setReady(true);
}, []);
return (
ready && (
<NavigateContext.Provider value={navigate}>
<RootWrapper />
</NavigateContext.Provider>
)
);
}
export const topLevelRoutes = [
{
element: <RootRouter />,
children: [
{
path: '/',
lazy: () => import('./pages/index'),
},
{
path: '/workspace/:workspaceId/*',
lazy: () => import('./pages/workspace/index'),
},
{
path: '/share/:workspaceId/:pageId',
loader: ({ params }) => {
return redirect(`/workspace/${params.workspaceId}/${params.pageId}`);
},
},
{
path: '/404',
lazy: () => import('./pages/404'),
},
{
path: '/expired',
lazy: () => import('./pages/expired'),
},
{
path: '/invite/:inviteId',
lazy: () => import('./pages/invite'),
},
{
path: '/upgrade-success',
lazy: () => import('./pages/upgrade-success'),
},
{
path: '/ai-upgrade-success',
lazy: () => import('./pages/ai-upgrade-success'),
},
{
path: '/onboarding',
lazy: () => import('./pages/onboarding'),
},
{
path: '/redirect-proxy',
lazy: () => import('./pages/redirect'),
},
{
path: '/subscribe',
lazy: () => import('./pages/subscribe'),
},
{
path: '/try-cloud',
loader: () => {
return redirect(
`/sign-in?redirect_uri=${encodeURIComponent('/?initCloud=true')}`
);
},
},
{
path: '/theme-editor',
lazy: () => import('./pages/theme-editor'),
},
{
path: '/template/import',
lazy: () => import('./pages/import-template'),
},
{
path: '/auth/:authType',
lazy: () => import(/* webpackChunkName: "auth" */ './pages/auth/auth'),
},
{
path: '/sign-In',
lazy: () =>
import(/* webpackChunkName: "auth" */ './pages/auth/sign-in'),
},
{
path: '/magic-link',
lazy: () =>
import(/* webpackChunkName: "auth" */ './pages/auth/magic-link'),
},
{
path: '/oauth/login',
lazy: () =>
import(/* webpackChunkName: "auth" */ './pages/auth/oauth-login'),
},
{
path: '/oauth/callback',
lazy: () =>
import(/* webpackChunkName: "auth" */ './pages/auth/oauth-callback'),
},
// deprecated, keep for old client compatibility
// TODO(@forehalo): remove
{
path: '/desktop-signin',
lazy: () =>
import(/* webpackChunkName: "auth" */ './pages/auth/oauth-login'),
},
// deprecated, keep for old client compatibility
// use '/sign-in'
// TODO(@forehalo): remove
{
path: '/signIn',
lazy: () =>
import(/* webpackChunkName: "auth" */ './pages/auth/sign-in'),
},
{
path: '/open-app/:action',
lazy: () => import('./pages/open-app'),
},
{
path: '*',
lazy: () => import('./pages/404'),
},
],
},
] satisfies [RouteObject, ...RouteObject[]];
export const viewRoutes = [
{
path: '/all',
lazy: () => import('./pages/workspace/all-page/all-page'),
},
{
path: '/collection',
lazy: () => import('./pages/workspace/all-collection'),
},
{
path: '/collection/:collectionId',
lazy: () => import('./pages/workspace/collection/index'),
},
{
path: '/tag',
lazy: () => import('./pages/workspace/all-tag'),
},
{
path: '/tag/:tagId',
lazy: () => import('./pages/workspace/tag'),
},
{
path: '/trash',
lazy: () => import('./pages/workspace/trash-page'),
},
{
path: '/:pageId',
lazy: () => import('./pages/workspace/detail-page/detail-page'),
},
{
path: '*',
lazy: () => import('./pages/404'),
},
] satisfies [RouteObject, ...RouteObject[]];
const createBrowserRouter = wrapCreateBrowserRouter(
reactRouterCreateBrowserRouter
);
export const router = (
window.SENTRY_RELEASE ? createBrowserRouter : reactRouterCreateBrowserRouter
)(topLevelRoutes, {
future: {
v7_normalizeFormMethod: true,
},
});