import { Editable, isInPageBuilderIframe } from '@components/common/page-builder/index.js'; import { ProductList } from '@components/frontStore/catalog/ProductList.js'; import React, { useEffect, useState } from 'react'; export interface RecommendationShelfProps { heading?: string | null; products?: React.ComponentProps['products']; /** Page-builder placeholder copy (the preview iframe has no product context). */ placeholderTitle: string; placeholderHint: string; } /** * Shared presentational shelf for both recommendation widgets * (specifications/05 ยง 9.4). Empty-state contract: zero products โ†’ render * NOTHING on the storefront (the heading never renders alone). Inside the * page-builder iframe an empty shelf renders a placeholder card instead, so * merchants can see and select the block โ€” product context only exists on * real product pages. */ export function RecommendationShelf({ heading, products, placeholderTitle, placeholderHint }: RecommendationShelfProps) { // Defer iframe detection until after hydration so the first render is // SSR-stable (matches the production output of `null`). const [isClient, setIsClient] = useState(false); useEffect(() => { setIsClient(true); }, []); const inBuilder = isClient && isInPageBuilderIframe(); const hasHeading = typeof heading === 'string' && heading.length > 0; if (!products || products.length === 0) { if (inBuilder) { return (
{placeholderTitle}
{placeholderHint}
); } return null; } return (
{/* Inside the builder the header stays mounted even when the heading is cleared โ€” unmounting would rip the contenteditable out from under the merchant's caret mid-edit, with no inline way to bring it back. */} {(hasHeading || inBuilder) && (
{hasHeading ? (heading as string) : ''}
)}
); }