import { Image } from '@components/common/Image.js'; import { Editable, EditableImage, EditableMarkdown, isPageBuilderActive } from '@components/common/page-builder/index.js'; import { buttonVariants } from '@components/common/ui/Button.js'; import { ImagePlus, ShoppingBag } from 'lucide-react'; import React, { useEffect, useState } from 'react'; /** * Product hero — single-product spotlight. A mini-PDP embedded on the * homepage: image, eyebrow, name, price, optional editorial copy, and a * "View details" link to the real product page. * * Variant picking + add-to-cart deliberately defer to the theme's normal * PDP affordances — exposing them from a homepage widget would require * wiring up the cart drawer machinery, which is theme-owned. The hero * is read-only: it surfaces the product and links into its full PDP. */ interface HeroProduct { productId: number; uuid: string; name: string; sku?: string | null; price?: { regular?: { value: number; text: string } | null; special?: { value: number; text: string } | null; } | null; image?: { alt: string; url: string } | null; url: string; inventory?: { isInStock?: boolean } | null; } export interface ProductHeroProps { productHeroWidget: { productUuid: string | null; image: string | null; imageAlt: string; /** Natural intrinsic width of the override image, captured at pick * time. Falls back to a hero-scale default. The product's own image * doesn't carry dimensions, so we always need this fallback when * the override is empty. */ imageWidth: number | null; /** Natural intrinsic height. */ imageHeight: number | null; eyebrow: string | null; copy: string | null; imagePosition: 'left' | 'right'; product: HeroProduct | null; }; } export default function ProductHero({ productHeroWidget }: ProductHeroProps) { const { image, imageAlt, imageWidth, imageHeight, eyebrow, copy, imagePosition, product } = productHeroWidget; const [inPb, setInPb] = useState(false); useEffect(() => { setInPb(isPageBuilderActive()); }, []); if (!product) { if (inPb) { const reverseP = imagePosition === 'right'; const imageBlock = (
); const copyBlock = (
Featured
); return (
{!reverseP ? ( <> {imageBlock} {copyBlock} ) : ( <>
{copyBlock}
{imageBlock}
)}
); } return null; } const reverse = imagePosition === 'right'; const displayImage = image || product.image?.url || null; const displayAlt = imageAlt || product.image?.alt || product.name; // Override dimensions when present; otherwise the product image has no // stored dimensions of its own — fall back to a hero-scale 4:5 portrait. const intrinsicWidth = image && imageWidth && imageWidth > 0 ? imageWidth : 1200; const intrinsicHeight = image && imageHeight && imageHeight > 0 ? imageHeight : 1500; const price = product.price?.special?.text || product.price?.regular?.text || null; const imagePanel = (
{/* `empty` (no override and no product image) shows an inline "add image" affordance; picking writes the override `settings.image`. */} {displayImage ? ( {displayAlt} ) : null}
); const copyPanel = (
{eyebrow && ( {eyebrow} )}

{product.name}

{price &&
{price}
} {copy && ( {copy} )}
); return (
{!reverse && ( <> {imagePanel} {copyPanel} )} {reverse && ( <>
{copyPanel}
{imagePanel}
)}
); } export const query = ` query Query( $productUuid: String $image: String $imageAlt: String $imageWidth: Float $imageHeight: Float $eyebrow: String $copy: String $imagePosition: String ) { productHeroWidget( productUuid: $productUuid image: $image imageAlt: $imageAlt imageWidth: $imageWidth imageHeight: $imageHeight eyebrow: $eyebrow copy: $copy imagePosition: $imagePosition ) { productUuid image imageAlt imageWidth imageHeight eyebrow copy imagePosition product { productId uuid name sku price { regular { value text } special { value text } } image { alt url } url inventory { isInStock } } } } `; export const variables = `{ productUuid: getWidgetSetting("productUuid"), image: getWidgetSetting("image"), imageAlt: getWidgetSetting("imageAlt"), imageWidth: getWidgetSetting("imageWidth"), imageHeight: getWidgetSetting("imageHeight"), eyebrow: getWidgetSetting("eyebrow"), copy: getWidgetSetting("copy"), imagePosition: getWidgetSetting("imagePosition", "left") }`;