import { getAreaComponents } from '@components/common/Area.js'; import { useAppState } from '@components/common/context/app.js'; import { _ } from '@evershop/evershop/lib/locale/translate/_'; import { generateComponentKey } from '@evershop/evershop/lib/util/keyGenerator'; import React, { useMemo } from 'react'; interface PreviewableWidget { code: string; name: string; description?: string | null; category?: string | null; } interface WidgetPreviewCardProps { widget: PreviewableWidget; /** Bounding rect of the widget row that triggered the hover. */ rect: DOMRect; /** Right edge of the palette panel — the card sits just past it. */ anchorX: number; } const CARD_W = 280; const ESTIMATED_H = 220; /** * Hover preview card surfaced by the Widgets palette. Renders a fixed- * positioned card beside the panel containing the widget's stylized preview * (registered via `Widget.previewComponent`, bundled by AreaLoader under * key `admin_widget_preview_`) plus a caption with name/description. * * Doesn't go through `` because Area's lookup picks at most one * component per widget type (setting OR storefront) — adding a third * "preview" variant would invasively change Area's contract. Instead we * read the current route's wildcard map via `getAreaComponents(routeId)['*']` * and pull the preview component out of it. * * `pointer-events: none` so the card never blocks drag/click on the row. */ export function WidgetPreviewCard({ widget, rect, anchorX }: WidgetPreviewCardProps): React.ReactElement { const routeId = useAppState()?.config?.pageMeta?.route?.id; const PreviewComponent = useMemo(() => { const key = generateComponentKey(`admin_widget_preview_${widget.code}`); // Pull the preview component from the current route's wildcard ('*') map. // The page-builder editor route bundle registers every widget's preview there. const components = getAreaComponents(routeId); const wildcard = components?.['*'] ?? {}; const entry = wildcard?.[key]; const cmp = entry?.component?.default; return typeof cmp === 'function' || typeof cmp === 'object' ? cmp : null; }, [widget.code, routeId]); // Vertically center on the row, but clamp to viewport. const vh = typeof window !== 'undefined' ? window.innerHeight : 800; let top = rect.top + rect.height / 2 - ESTIMATED_H / 2; top = Math.max(12, Math.min(top, vh - ESTIMATED_H - 12)); return (
{PreviewComponent ? ( // Cast to a generic React element — the registry guarantees it's a // component-like value, but TS doesn't know the exact shape. React.createElement( PreviewComponent as React.ComponentType>, {} ) ) : (
{widget.code}
)}
{_(widget.name)}
{widget.description && (
{_(widget.description)}
)}
{_('Click or drag to add')}
); } /** * Inject the fade-in keyframes once. Idempotent — checks for an existing * `