import type { Metadata } from "next"; import { notFound, redirect } from "next/navigation"; import { PageHeader } from "@/components/PageHeader"; import { Gallery } from "@/components/catalog/Gallery"; import { Tabs } from "@/components/catalog/Tabs"; import { TechnicalDataTable } from "@/components/catalog/TechnicalDataTable"; import { CommentsPanel } from "@/components/catalog/CommentsPanel"; import { EntityCard } from "@/components/catalog/EntityCard"; import { SimilarRow } from "@/components/catalog/SimilarRow"; import { AddToCartBar } from "@/components/catalog/AddToCartBar"; import { getSession } from "@/lib/auth"; import { safe } from "@/lib/meshkee/client"; import { getComments } from "@/lib/meshkee/comments"; import { normalizeGallery, normalizeImage, pickText, slugify } from "@/lib/meshkee/format"; import { getProductById, getProductTechnicalInfo } from "@/lib/meshkee/products"; import { getStoreItemByProduct } from "@/lib/meshkee/store-items"; type Params = { id: string; slug: string }; export async function generateMetadata({ params }: { params: Promise }): Promise { const { id } = await params; try { const { product } = await getProductById(id); const name = pickText(product.nameFa, product.name, product.title); return { title: product.seoMetaTitle || name, description: product.seoMetaDescription || product.summary || undefined }; } catch { return { title: "فروشگاه" }; } } export default async function StoreItemDetailPage({ params }: { params: Promise }) { const { id, slug } = await params; let detail; try { detail = await getProductById(id); } catch { notFound(); } const { product, relatedProducts = [] } = detail; const name = pickText(product.nameFa, product.name, product.title); const canonicalSlug = slugify(name); if (canonicalSlug && canonicalSlug !== slug) { redirect(`/store/${id}/${canonicalSlug}`); } const [technicalInfo, comments, session, storeItemResult] = await Promise.all([ safe(getProductTechnicalInfo(id), null), safe(getComments("store_item", id), { items: [] }), getSession(), safe(getStoreItemByProduct(id), { storeItem: null }), ]); const images = normalizeGallery(product.gallery ?? product.images ?? []); const mainImage = normalizeImage(product.titleImageUrl ?? product.imageUrl); const gallery = mainImage && images.length === 0 ? [mainImage] : images; const variants = storeItemResult.storeItem?.variants ?? []; return ( <>

{name}

{product.summary &&

{product.summary}

}
) : (

{product.description || product.summary || "توضیحاتی ثبت نشده است."}

), }, { key: "technical", label: "مشخصات فنی", content: }, { key: "comments", label: `نظرات${comments.items.length ? ` (${comments.items.length.toLocaleString("fa-IR")})` : ""}`, content: ( ), }, ]} />
{relatedProducts.length > 0 && ( {relatedProducts.map((related) => { const relatedName = pickText(related.nameFa, related.name, related.title); return ( ); })} )} ); }