add nextjs style routes

This commit is contained in:
Rami Bitar
2026-06-06 13:46:56 -04:00
parent ef550e9b55
commit aa58af410d
23 changed files with 1183 additions and 855 deletions

View File

@@ -3,15 +3,13 @@
import { useParams } from "next/navigation";
/**
* Returns the last segment of the current catch-all slug route, e.g. the
* `cool-shirt` in `/products/cool-shirt`. Components use this to derive the
* resource they should load from the Next.js route segments directly.
* Returns the dynamic handle of the current route, e.g. the `cool-shirt` in
* `/products/cool-shirt` (or its `/products/cool-shirt/editor` editor view).
* Both the public and editor routes share the same `[handle]` segment, so the
* value is identical in either context.
*/
export function useRouteSegment(): string | undefined {
const params = useParams();
const segments = Array.isArray(params?.slug) ? (params.slug as string[]) : [];
// Editor routes are served under `/editor/*`; ignore that prefix so the
// resolved segment matches the public route.
const routeSegments = segments[0] === "editor" ? segments.slice(1) : segments;
return routeSegments[routeSegments.length - 1];
const handle = params?.handle;
return typeof handle === "string" ? handle : undefined;
}