// src/lib/slugMap.ts import { categories } from "@/data/categories"; export type PageType = | "article_home" | "article_category" | "article_detail" | "product_category" | "product_detail"; export type SlugResult = | { type: "article_home" } | { type: "article_category"; slug: string } | { type: "article_detail"; slug: string } | { type: "product_category"; slug: string } | { type: "product_detail"; slug: string }; export function findBySlug(rawSlug: string): SlugResult { const slug = normalizeSlug(rawSlug); const url = "/" + slug; /* 1. ARTICLE HOME */ if (url === "/tin-tuc") { return { type: "article_home" }; } /* 2. ARTICLE CATEGORY */ const articleCats = categories.article.all_category.article; for (const parent of articleCats) { if (parent.url === url) { return { type: "article_category", slug }; } for (const child of parent.children ?? []) { if (child.url === url) { return { type: "article_category", slug }; } } } /* 3. PRODUCT CATEGORY */ const productCats = categories.product.all_category; for (const cat of productCats) { if (cat.url === url) { return { type: "product_category", slug }; } for (const child of cat.children ?? []) { if (child.url === url) { return { type: "product_category", slug }; } } } /* 4. PRODUCT DETAIL */ if (isProductDetailSlug(slug)) { return { type: "product_detail", slug }; } /* 5. ARTICLE DETAIL (fallback) */ return { type: "article_detail", slug }; } /* =============================== * HELPERS * =============================== */ function normalizeSlug(slug: string) { return slug.replace(/^\/+/, "").trim(); } function isProductDetailSlug(slug: string) { return ( slug.startsWith("hhpc-") || slug.startsWith("pc-") || slug.startsWith("man-hinh-") ); }