Skip to content

Commit aad4ae0

Browse files
authored
Support custom routing (#18)
* Add nested routes sample. * Fix identifying layout routes. * fix: don't relay on route ordering, that index route comes first.
1 parent d250771 commit aad4ae0

6 files changed

Lines changed: 53 additions & 17 deletions

File tree

src/RemixDevTools/tabs/PageTab.tsx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { useGetSocket } from "../hooks/useGetSocket";
66
import { Tag } from "../components/Tag";
77
import { VsCodeButton } from "../components/VScodeButton";
88
import { useMemo } from "react";
9+
import { isLayoutRoute } from "../utils/misc";
910

1011
export const ROUTE_COLORS: Record<string, string> = {
1112
ROUTE: "rdt-bg-green-500 rdt-text-white",
@@ -67,12 +68,9 @@ const PageTab = () => {
6768
const originalData = getOriginalData(route.data);
6869

6970
const isRoot = route.id === "root";
70-
const lastPart = route.id.split("/").pop();
71-
const isLayout =
72-
lastPart?.split(".")?.length === 1 &&
73-
(lastPart?.startsWith("_") || lastPart?.startsWith("__")) &&
74-
lastPart !== "_index" &&
75-
"index";
71+
72+
const entryRoute = __remixManifest.routes[route.id];
73+
const isLayout = isLayoutRoute(entryRoute);
7674

7775
return (
7876
<li key={route.id} className="rdt-mb-8 rdt-ml-8">

src/RemixDevTools/tabs/RoutesTab.tsx

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,7 @@ import { useRDTContext } from "../context/useRDTContext";
1313
import { Input } from "../components/Input";
1414
import { NewRouteForm } from "../components/NewRouteForm";
1515
import { useGetSocket } from "../hooks/useGetSocket";
16-
17-
const isLayout = (route: EntryRoute & { route: string }) => {
18-
const rId = route.id.replace("routes/", "");
19-
const v2Layout =
20-
rId.startsWith("_") &&
21-
!rId.split(".")[0].endsWith("index") &&
22-
rId.split(".").length === 1;
23-
const v1Layout = rId.startsWith("__");
24-
return v1Layout || v2Layout;
25-
};
16+
import { isLeafRoute } from "../utils/misc";
2617

2718
const RoutesTab = () => {
2819
const { routeWildcards, setRouteWildcards } = useRDTContext();
@@ -35,7 +26,7 @@ const RoutesTab = () => {
3526
route: convertRemixPathToUrl(window.__remixManifest.routes, route),
3627
};
3728
})
38-
.filter((route) => !isLayout(route) && route.id !== "root")
29+
.filter((route) => isLeafRoute(route))
3930
);
4031
const navigate = useNavigate();
4132

src/RemixDevTools/utils/misc.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { EntryRoute } from "@remix-run/react/dist/routes";
2+
3+
type RouteType = "ROOT" | "LAYOUT" | "ROUTE";
4+
5+
export function getRouteType(route: EntryRoute) {
6+
let routeType: RouteType = "ROUTE";
7+
8+
if (route.id === "root") {
9+
routeType = "ROOT";
10+
} else if (route.index) {
11+
routeType = "ROUTE";
12+
} else if (!route.path) {
13+
// Pathless layout route
14+
routeType = "LAYOUT";
15+
} else {
16+
// Find an index route with parentId set to this route
17+
const childIndexRoute = Object.values(window.__remixManifest.routes).find(
18+
(r) => r.parentId === route.id && r.index
19+
);
20+
21+
routeType = childIndexRoute ? "LAYOUT" : "ROUTE";
22+
}
23+
24+
return routeType;
25+
}
26+
27+
export function isLayoutRoute(route: EntryRoute) {
28+
return getRouteType(route) === "LAYOUT";
29+
}
30+
31+
export function isLeafRoute(route: EntryRoute) {
32+
return getRouteType(route) === "ROUTE";
33+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function OtherIndexRoute() {
2+
return <div>Other Home</div>
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function OtherPageRoute() {
2+
return <div>Other Page</div>
3+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { Outlet } from "@remix-run/react";
2+
3+
export default function OtherLayout() {
4+
return <div>
5+
<h2>Other Layout</h2>
6+
<Outlet />
7+
</div>
8+
}

0 commit comments

Comments
 (0)