Files
react-editor-shopify/lib/schema-resolver.ts
2026-05-02 10:01:13 -04:00

59 lines
1.8 KiB
TypeScript

import type { Schema } from "@/lib/schema.server";
export type ResolvedEntry = {
key: string;
data: { root: any; content: any[] };
params: Record<string, string>;
};
const TEMPLATE_PATTERNS: { key: string; prefix: string; param: string }[] = [
{ key: "/products/*", prefix: "/products/", param: "handle" },
{ key: "/collections/*", prefix: "/collections/", param: "handle" },
];
export function resolveSchemaEntry(
schema: Schema,
route: string,
): ResolvedEntry | null {
if (schema[route]) {
return { key: route, data: schema[route], params: {} };
}
for (const { key, prefix, param } of TEMPLATE_PATTERNS) {
if (route.startsWith(prefix) && route.length > prefix.length && schema[key]) {
const value = route.slice(prefix.length);
return {
key,
data: applyParams(schema[key], { [param]: value }),
params: { [param]: value },
};
}
}
return null;
}
export function templateKeyForRoute(route: string): string | null {
for (const { key, prefix } of TEMPLATE_PATTERNS) {
if (route.startsWith(prefix) && route.length > prefix.length) return key;
}
return null;
}
function applyParams(
data: { root: any; content: any[] },
params: Record<string, string>,
): { root: any; content: any[] } {
const handle = params.handle;
if (!handle) return data;
const content = data.content.map((block) => {
const props = block?.props ?? {};
if ("product" in props && (props.product == null || !props.product?.handle)) {
return { ...block, props: { ...props, product: { handle } } };
}
if ("collection" in props && (props.collection == null || !props.collection?.handle)) {
return { ...block, props: { ...props, collection: { handle } } };
}
return block;
});
return { ...data, content };
}