import { Image } from '@components/common/Image.js'; import { Editable, EditableImage, EditableMarkdown, isPageBuilderActive } from '@components/common/page-builder/index.js'; import { ProductList } from '@components/frontStore/catalog/ProductList.js'; import { ImagePlus, Sparkles } from 'lucide-react'; import React, { useEffect, useState } from 'react'; /** * Collection spotlight — a big cover image on one side, an editorial copy * panel + 2x2 (or 1x2) product preview grid on the other. Bridges * brand storytelling and commerce in one block. */ export interface CollectionSpotlightProps { collectionSpotlightWidget: { collection: string | null; image: string | null; imageAlt: string; imagePosition: 'left' | 'right'; /** Natural intrinsic width of the cover image, captured at pick time. */ imageWidth: number | null; /** Natural intrinsic height. */ imageHeight: number | null; eyebrow: string | null; heading: string; body: string | null; previewCount: 2 | 4; previewProducts: any[]; totalProducts: number; collectionName: string | null; /** Resolved storefront URL for the "View all" CTA (null → CTA hidden). */ viewAllLink?: string | null; viewAllLabel?: string | null; }; } export default function CollectionSpotlight({ collectionSpotlightWidget }: CollectionSpotlightProps) { const { image, imageAlt, imagePosition, imageWidth, imageHeight, eyebrow, heading, body, previewCount, previewProducts = [], totalProducts, collectionName, collection, viewAllLink, viewAllLabel } = collectionSpotlightWidget; const intrinsicWidth = imageWidth && imageWidth > 0 ? imageWidth : 1200; const intrinsicHeight = imageHeight && imageHeight > 0 ? imageHeight : 1500; const [inPb, setInPb] = useState(false); useEffect(() => { setInPb(isPageBuilderActive()); }, []); if (!heading && !collectionName) { if (inPb) { const reverseP = imagePosition === 'right'; const cols = previewCount === 2 ? 2 : 2; const rows = previewCount === 2 ? 1 : 2; const cellCount = cols * rows; const ph = (
{!reverseP ? ( <>
Collection
{Array.from({ length: cellCount }, (_, i) => (
))}
) : ( <>
Collection
{Array.from({ length: cellCount }, (_, i) => (
))}
)}
); return ph; } return null; } const reverse = imagePosition === 'right'; const cols = previewCount === 2 ? 2 : 2; // 2 cols for both — 2 = 1x2, 4 = 2x2 // Collections have no public page, so the CTA points at a merchant-chosen // link (resolved server-side), NOT a synthetic `/collections/` route // that always 404'd. Hidden entirely when no link is set. const resolvedViewAllLabel = viewAllLabel || (totalProducts > 0 ? `View all ${totalProducts} →` : 'View all →'); const viewAllUrl = viewAllLink || null; // The spotlight stacks (image on top of copy) below `lg`, and only splits // side-by-side on desktop — so the product grid gets full width and shows a // comfortable 2-up on tablet instead of being crushed into a ~40% panel. // Image aspect per surface: // - mobile ('s inline `aspect-ratio` + `height: auto` defeat classNames, // so the cover overrides go through `style` (`aspectRatio: 'auto'` clears // the inline ratio, `height: 100%` fills the box). const imagePanel = (
{/* className h-full w-full so EditableImage fills the relative wrapper in the canvas and stays the positioning context for the absolute (production is display:contents — zero layout impact). `empty` shows an inline "add image" affordance before a cover is set. */} {image ? ( {imageAlt ) : null}
); const copyPanel = (
{/* Stacked (1-col) layout: drop the side/bottom padding so the copy and product grid align edge-to-edge with the full-width image above; keep a small top gap. Full padding returns only in the side-by-side (lg) split. */} {eyebrow && ( {eyebrow} )} {heading || collectionName || ''} {body && ( {body} )} {viewAllUrl && ( {resolvedViewAllLabel} )}
); return (
{!reverse && ( <>
{imagePanel}
{copyPanel}
)} {reverse && ( <>
{copyPanel}
{imagePanel}
)}
); } export const query = ` query Query( $collection: String $image: String $imageAlt: String $imagePosition: String $imageWidth: Float $imageHeight: Float $eyebrow: String $heading: String $body: String $previewCount: Int $viewAllLink: String $viewAllLabel: String ) { collectionSpotlightWidget( collection: $collection image: $image imageAlt: $imageAlt imagePosition: $imagePosition imageWidth: $imageWidth imageHeight: $imageHeight eyebrow: $eyebrow heading: $heading body: $body previewCount: $previewCount viewAllLink: $viewAllLink viewAllLabel: $viewAllLabel ) { collection image imageAlt imagePosition imageWidth imageHeight eyebrow heading body previewCount totalProducts collectionName viewAllLink viewAllLabel previewProducts { ...Product } } } `; export const fragments = ` fragment Product on Product { productId name sku variantGroupId price { regular { value text } special { value text } } inventory { isInStock } image { alt url } url } `; export const variables = `{ collection: getWidgetSetting("collection"), image: getWidgetSetting("image"), imageAlt: getWidgetSetting("imageAlt"), imagePosition: getWidgetSetting("imagePosition", "left"), imageWidth: getWidgetSetting("imageWidth"), imageHeight: getWidgetSetting("imageHeight"), eyebrow: getWidgetSetting("eyebrow", "COLLECTION"), heading: getWidgetSetting("heading"), body: getWidgetSetting("body"), previewCount: getWidgetSetting("previewCount", 4), viewAllLink: getWidgetSetting("viewAllLink"), viewAllLabel: getWidgetSetting("viewAllLabel") }`;