diff --git a/apps/website/src/app/products/[id]/page.tsx b/apps/website/src/app/products/[id]/page.tsx index e29f879..1333758 100644 --- a/apps/website/src/app/products/[id]/page.tsx +++ b/apps/website/src/app/products/[id]/page.tsx @@ -6,10 +6,18 @@ import AddToCartSection from '../../../components/AddToCartSection' import ProductCard from '../../../components/ProductCard' import { API_BASE } from '../../../lib/api' +import type { TranslatableString } from '@homaroma/types' + +/** Resolve TranslatableString → plain string (SSR: always defaults to 'th') */ +function resolveStr(v: TranslatableString | null | undefined, lang = 'th'): string { + if (!v) return '' + if (typeof v === 'string') return v + return v[lang] ?? v['th'] ?? v['en'] ?? Object.values(v)[0] ?? '' +} async function getProduct(id: string): Promise { try { - const res = await fetch(`${API_BASE}/products/${id}`, { next: { revalidate: 60 } }) + const res = await fetch(`${API_BASE}/products/${id}?lang=th`, { next: { revalidate: 60 } }) return res.ok ? res.json() : null } catch { return null } } @@ -29,10 +37,10 @@ export async function generateMetadata({ params }: { params: Promise<{ id: strin if (!product) return { title: 'Product Not Found | HomAroma' } return { title: `${product.scent.name} ${product.category.name} | HomAroma`, - description: product.description ?? `Premium ${product.category.name} — ${product.scent.name}. Shop HomAroma for high-quality botanical scents.`, + description: resolveStr(product.description) ?? `Premium ${product.category.name} — ${product.scent.name}. Shop HomAroma for high-quality botanical scents.`, openGraph: { title: product.scent.name, - description: product.description ?? '', + description: resolveStr(product.description) ?? '', images: product.images?.[0]?.url ? [product.images[0].url] : [], }, } @@ -85,12 +93,12 @@ export default async function ProductDetailPage({ params }: { params: Promise<{ {/* Full Details Section */} - {product.details && ( -
+ {resolveStr(product.details) && ( +

Product Details

-
)} diff --git a/apps/website/src/components/AddToCartSection.tsx b/apps/website/src/components/AddToCartSection.tsx index df30b0e..10f022e 100644 --- a/apps/website/src/components/AddToCartSection.tsx +++ b/apps/website/src/components/AddToCartSection.tsx @@ -1,9 +1,16 @@ "use client"; import { useState } from 'react' import Link from 'next/link' -import { getCategoryImage, formatPrice, type Product } from '@homaroma/types' +import { getCategoryImage, formatPrice, type Product, type TranslatableString } from '@homaroma/types' import { useCartStore } from '../store/cartStore' +/** Resolve TranslatableString → plain string */ +function resolveStr(v: TranslatableString | null | undefined, lang = 'th'): string { + if (!v) return '' + if (typeof v === 'string') return v + return v[lang] ?? v['th'] ?? v['en'] ?? Object.values(v)[0] ?? '' +} + interface Props { product: Product } @@ -40,8 +47,8 @@ export default function AddToCartSection({ product }: Props) {

Premium Collection

- {product.description && ( -

{product.description}

+ {resolveStr(product.description) && ( +

{resolveStr(product.description)}

)} {/* Price */}