35 lines
1.0 KiB
TypeScript
35 lines
1.0 KiB
TypeScript
const TEMPLATE_PATTERNS: { key: string; prefix: string; param: string }[] = [
|
|
{ key: "/products/*", prefix: "/products/", param: "handle" },
|
|
{ key: "/collections/*", prefix: "/collections/", param: "handle" },
|
|
];
|
|
|
|
export type ResolvedEditorPath = {
|
|
isEdit: boolean;
|
|
path: string;
|
|
templateKey: string | null;
|
|
params: Record<string, string>;
|
|
};
|
|
|
|
const resolveEditorPath = (editorPath: string[] = []): ResolvedEditorPath => {
|
|
const isEdit =
|
|
editorPath.length > 0 && editorPath[editorPath.length - 1] === "edit";
|
|
|
|
const segments = isEdit ? editorPath.slice(0, -1) : editorPath;
|
|
const path = segments.length === 0 ? "/" : `/${segments.join("/")}`;
|
|
|
|
for (const { key, prefix, param } of TEMPLATE_PATTERNS) {
|
|
if (path.startsWith(prefix) && path.length > prefix.length) {
|
|
return {
|
|
isEdit,
|
|
path,
|
|
templateKey: key,
|
|
params: { [param]: path.slice(prefix.length) },
|
|
};
|
|
}
|
|
}
|
|
|
|
return { isEdit, path, templateKey: null, params: {} };
|
|
};
|
|
|
|
export default resolveEditorPath;
|