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} />
- {isCommitsView ? ( + {viewMode === "commits" ? ( ({ viewMode: "blob" as const }), }); function BlobView() { diff --git a/webui2/src/routes/$repo/_code/commits/$ref.tsx b/webui2/src/routes/$repo/_code/commits/$ref.tsx index 4f94db6613a053e76b4bbe680e7ca64192c80077..f1ea10949ca827b23a8069bf26e435d19c5200dc 100644 --- a/webui2/src/routes/$repo/_code/commits/$ref.tsx +++ b/webui2/src/routes/$repo/_code/commits/$ref.tsx @@ -6,6 +6,7 @@ import { CommitList } from "@/components/code/CommitList"; export const Route = createFileRoute("/$repo/_code/commits/$ref")({ component: CommitsView, + beforeLoad: () => ({ viewMode: "commits" as const }), }); function CommitsView() { diff --git a/webui2/src/routes/$repo/_code/tree/$ref/$.tsx b/webui2/src/routes/$repo/_code/tree/$ref/$.tsx index ec8e00eeb10cd5f5c68b2e3c2251ec7356ed89e9..2059fa890661d189bcffbdd8ca78bee8671224b3 100644 --- a/webui2/src/routes/$repo/_code/tree/$ref/$.tsx +++ b/webui2/src/routes/$repo/_code/tree/$ref/$.tsx @@ -64,6 +64,7 @@ interface ReadmeQueryData { export const Route = createFileRoute("/$repo/_code/tree/$ref/$")({ component: TreeView, + beforeLoad: () => ({ viewMode: "tree" as const }), }); function TreeView() {