add nextjs style routes
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user