import { useEffect } from "react";
import { setFrontendErrorContext } from "@openhandoff/frontend-errors/client";
import {
Navigate,
Outlet,
createRootRoute,
createRoute,
createRouter,
useRouterState,
} from "@tanstack/react-router";
import { MockLayout } from "../components/mock-layout";
import { defaultWorkspaceId } from "../lib/env";
import { handoffWorkbenchClient } from "../lib/workbench";
const rootRoute = createRootRoute({
component: RootLayout,
});
const indexRoute = createRoute({
getParentRoute: () => rootRoute,
path: "/",
component: () => (
),
});
const workspaceRoute = createRoute({
getParentRoute: () => rootRoute,
path: "/workspaces/$workspaceId",
component: WorkspaceLayoutRoute,
});
const workspaceIndexRoute = createRoute({
getParentRoute: () => workspaceRoute,
path: "/",
component: WorkspaceRoute,
});
const handoffRoute = createRoute({
getParentRoute: () => workspaceRoute,
path: "handoffs/$handoffId",
validateSearch: (search: Record) => ({
sessionId: typeof search.sessionId === "string" && search.sessionId.trim().length > 0 ? search.sessionId : undefined,
}),
component: HandoffRoute,
});
const repoRoute = createRoute({
getParentRoute: () => workspaceRoute,
path: "repos/$repoId",
component: RepoRoute,
});
const routeTree = rootRoute.addChildren([
indexRoute,
workspaceRoute.addChildren([workspaceIndexRoute, handoffRoute, repoRoute]),
]);
export const router = createRouter({ routeTree });
declare module "@tanstack/react-router" {
interface Register {
router: typeof router;
}
}
function WorkspaceLayoutRoute() {
return ;
}
function WorkspaceRoute() {
const { workspaceId } = workspaceRoute.useParams();
useEffect(() => {
setFrontendErrorContext({
workspaceId,
handoffId: undefined,
});
}, [workspaceId]);
return ;
}
function HandoffRoute() {
const { workspaceId, handoffId } = handoffRoute.useParams();
const { sessionId } = handoffRoute.useSearch();
useEffect(() => {
setFrontendErrorContext({
workspaceId,
handoffId,
repoId: undefined,
});
}, [handoffId, workspaceId]);
return ;
}
function RepoRoute() {
const { workspaceId, repoId } = repoRoute.useParams();
useEffect(() => {
setFrontendErrorContext({
workspaceId,
handoffId: undefined,
repoId,
});
}, [repoId, workspaceId]);
const activeHandoffId = handoffWorkbenchClient.getSnapshot().handoffs.find(
(handoff) => handoff.repoId === repoId,
)?.id;
if (!activeHandoffId) {
return (
);
}
return (
);
}
function RootLayout() {
return (
<>
>
);
}
function RouteContextSync() {
const location = useRouterState({
select: (state) => state.location,
});
useEffect(() => {
setFrontendErrorContext({
route: `${location.pathname}${location.search}${location.hash}`,
});
}, [location.hash, location.pathname, location.search]);
return null;
}