Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 25 additions & 16 deletions web/src/routes/__root.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
import { Header } from '@/components/layout/header.js';
import { Sidebar } from '@/components/layout/sidebar.js';
import { OrgProvider } from '@/lib/org-context.js';
import { queryClient } from '@/lib/query-client.js';
import { trpc } from '@/lib/trpc.js';
import { useQuery } from '@tanstack/react-query';
import { Outlet, createRootRoute, useNavigate } from '@tanstack/react-router';
import { useEffect } from 'react';
import { Outlet, createRootRoute, redirect, useRouterState } from '@tanstack/react-router';

function RootLayout() {
const navigate = useNavigate();
const meQuery = useQuery(trpc.auth.me.queryOptions());

const isLoginPage = window.location.pathname === '/login';

useEffect(() => {
if (meQuery.isError && !isLoginPage) {
navigate({ to: '/login' });
}
}, [meQuery.isError, isLoginPage, navigate]);
const routerState = useRouterState();
const isLoginPage = routerState.location.pathname === '/login';
const meQuery = useQuery({ ...trpc.auth.me.queryOptions(), retry: false });

if (isLoginPage) {
return <Outlet />;
Expand All @@ -30,10 +23,6 @@ function RootLayout() {
);
}

if (meQuery.isError) {
return null;
}

return (
<OrgProvider me={meQuery.data}>
<div className="flex h-screen">
Expand All @@ -49,6 +38,26 @@ function RootLayout() {
);
}

function PendingComponent() {
return (
<div className="flex h-screen items-center justify-center">
<div className="text-muted-foreground">Loading...</div>
</div>
);
}

export const rootRoute = createRootRoute({
component: RootLayout,
pendingComponent: PendingComponent,
beforeLoad: async ({ location }) => {
if (location.pathname === '/login') return;
try {
await queryClient.ensureQueryData({
...trpc.auth.me.queryOptions(),
retry: false,
});
} catch {
throw redirect({ to: '/login' });
}
},
});
4 changes: 2 additions & 2 deletions web/src/routes/login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ function LoginPage() {
return;
}

await queryClient.refetchQueries({ queryKey: trpc.auth.me.queryOptions().queryKey });
navigate({ to: '/' });
await queryClient.invalidateQueries({ queryKey: trpc.auth.me.queryOptions().queryKey });
navigate({ to: '/', replace: true });
} catch {
setError('Network error');
} finally {
Expand Down