Files
react-editor-shopify-nextjs/hooks/use-route-segment.ts
2026-06-06 11:45:24 -04:00

18 lines
714 B
TypeScript

"use client";
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.
*/
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];
}