Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support custom routing #18

Merged
merged 3 commits into from
Jul 22, 2023
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
10 changes: 4 additions & 6 deletions src/RemixDevTools/tabs/PageTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useGetSocket } from "../hooks/useGetSocket";
import { Tag } from "../components/Tag";
import { VsCodeButton } from "../components/VScodeButton";
import { useMemo } from "react";
import { isLayoutRoute } from "../utils/misc";

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

const isRoot = route.id === "root";
const lastPart = route.id.split("/").pop();
const isLayout =
lastPart?.split(".")?.length === 1 &&
(lastPart?.startsWith("_") || lastPart?.startsWith("__")) &&
lastPart !== "_index" &&
"index";

const entryRoute = __remixManifest.routes[route.id];
const isLayout = isLayoutRoute(entryRoute);

return (
<li key={route.id} className="rdt-mb-8 rdt-ml-8">
Expand Down
13 changes: 2 additions & 11 deletions src/RemixDevTools/tabs/RoutesTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,7 @@ import { useRDTContext } from "../context/useRDTContext";
import { Input } from "../components/Input";
import { NewRouteForm } from "../components/NewRouteForm";
import { useGetSocket } from "../hooks/useGetSocket";

const isLayout = (route: EntryRoute & { route: string }) => {
const rId = route.id.replace("routes/", "");
const v2Layout =
rId.startsWith("_") &&
!rId.split(".")[0].endsWith("index") &&
rId.split(".").length === 1;
const v1Layout = rId.startsWith("__");
return v1Layout || v2Layout;
};
import { isLeafRoute } from "../utils/misc";

const RoutesTab = () => {
const { routeWildcards, setRouteWildcards } = useRDTContext();
Expand All @@ -35,7 +26,7 @@ const RoutesTab = () => {
route: convertRemixPathToUrl(window.__remixManifest.routes, route),
};
})
.filter((route) => !isLayout(route) && route.id !== "root")
.filter((route) => isLeafRoute(route))
);
const navigate = useNavigate();

Expand Down
33 changes: 33 additions & 0 deletions src/RemixDevTools/utils/misc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { EntryRoute } from "@remix-run/react/dist/routes";

type RouteType = "ROOT" | "LAYOUT" | "ROUTE";

export function getRouteType(route: EntryRoute) {
let routeType: RouteType = "ROUTE";

if (route.id === "root") {
routeType = "ROOT";
} else if (route.index) {
routeType = "ROUTE";
} else if (!route.path) {
// Pathless layout route
routeType = "LAYOUT";
} else {
// Find an index route with parentId set to this route
const childIndexRoute = Object.values(window.__remixManifest.routes).find(
(r) => r.parentId === route.id && r.index
);

routeType = childIndexRoute ? "LAYOUT" : "ROUTE";
}

return routeType;
}

export function isLayoutRoute(route: EntryRoute) {
return getRouteType(route) === "LAYOUT";
}

export function isLeafRoute(route: EntryRoute) {
return getRouteType(route) === "ROUTE";
}
3 changes: 3 additions & 0 deletions src/remix-app-for-testing/app/routes/other._index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function OtherIndexRoute() {
return <div>Other Home</div>
}
3 changes: 3 additions & 0 deletions src/remix-app-for-testing/app/routes/other.page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function OtherPageRoute() {
return <div>Other Page</div>
}
8 changes: 8 additions & 0 deletions src/remix-app-for-testing/app/routes/other.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Outlet } from "@remix-run/react";

export default function OtherLayout() {
return <div>
<h2>Other Layout</h2>
<Outlet />
</div>
}