diff --git a/webui2/src/routes/$repo/_code.tsx b/webui2/src/routes/$repo/_code.tsx index e11826b182fea902fe68c3fbca31ee0c32a74fa5..9ebc43fa06ec2d9f8c462c52965202ba5ba48451 100644 --- a/webui2/src/routes/$repo/_code.tsx +++ b/webui2/src/routes/$repo/_code.tsx @@ -6,9 +6,9 @@ import { useReadQuery } from "@apollo/client/react"; import { createFileRoute, Outlet, - useMatchRoute, useNavigate, useParams, + useRouterState, } from "@tanstack/react-router"; import { GitCommit } from "lucide-react"; @@ -18,6 +18,8 @@ import { RefSelector } from "@/components/code/RefSelector"; import { ButtonLink } from "@/components/ui/button-link"; import { Skeleton } from "@/components/ui/skeleton"; +export type CodeViewMode = "tree" | "blob" | "commits"; + export const Route = createFileRoute("/$repo/_code")({ component: CodeLayout, pendingComponent: CodeLayoutSkeleton, @@ -30,7 +32,7 @@ function CodeLayout() { const refs: GitRef[] = refsData?.repository?.refs?.nodes ?? []; const repoName = refsData?.repository?.name ?? repoRef ?? "default-repo"; - // Read child route params (ref and splat path) via loose useParams + // Read child route params (ref and splat path) const allParams = useParams({ strict: false }) as { ref?: string; _splat?: string; @@ -38,35 +40,33 @@ function CodeLayout() { const currentRef = allParams.ref ?? ""; const currentPath = allParams._splat ?? ""; - const matchRoute = useMatchRoute(); - const isBlobView = !!matchRoute({ - to: "/$repo/blob/$ref/$", - params: { repo, ref: currentRef, _splat: currentPath }, - fuzzy: true, - }); - const isCommitsView = !!matchRoute({ - to: "/$repo/commits/$ref", - params: { repo, ref: currentRef }, - fuzzy: true, + // Read viewMode from the deepest child route's context. + // Each child (tree, blob, commits) sets viewMode in its beforeLoad. + const viewMode: CodeViewMode = useRouterState({ + select: (s) => { + const ctx = s.matches.at(-1)?.context; + if (ctx && typeof ctx === "object" && "viewMode" in ctx) { + return ctx.viewMode as CodeViewMode; + } + return "tree"; + }, }); const navigate = useNavigate(); function handleRefSelect(newRef: GitRef) { - if (isCommitsView) { - void navigate({ - to: "/$repo/commits/$ref", - params: { repo, ref: newRef.shortName }, - }); - } else if (isBlobView) { + const refName = newRef.shortName; + if (viewMode === "commits") { + void navigate({ to: "/$repo/commits/$ref", params: { repo, ref: refName } }); + } else if (viewMode === "blob") { void navigate({ to: "/$repo/blob/$ref/$", - params: { repo, ref: newRef.shortName, _splat: currentPath }, + params: { repo, ref: refName, _splat: currentPath }, }); } else { void navigate({ to: "/$repo/tree/$ref/$", - params: { repo, ref: newRef.shortName, _splat: currentPath }, + params: { repo, ref: refName, _splat: currentPath }, }); } } @@ -81,7 +81,7 @@ function CodeLayout() { repo={repo} />