import { asArray, drawerInputClass, Field, ImagePickerField, RepeatableAccordion, Section, Segmented, Toggle, useArraySetting, useScopedFormContext } from '@components/common/page-builder/index.js'; import { LinkPicker } from '@components/common/page-builder/pickers/LinkPicker.js'; import { _ } from '@evershop/evershop/lib/locale/translate/_'; import React from 'react'; import type { MosaicAspect, MosaicLabelPosition, MosaicLayout, MosaicTile } from './CategoryMosaic.js'; interface CategoryMosaicSettingProps { categoryMosaicWidget?: { heading?: string | null; tiles?: MosaicTile[]; columns?: number | null; aspect?: MosaicAspect; layout?: MosaicLayout; labelPosition?: MosaicLabelPosition; }; } const ASPECT_OPTIONS: ReadonlyArray<{ value: MosaicAspect; label: string; }> = [ { value: 'square', label: _('Square') }, { value: 'landscape', label: _('Landscape') }, { value: 'portrait', label: _('Portrait') } ]; const LABEL_POSITION_OPTIONS: ReadonlyArray<{ value: MosaicLabelPosition; label: string; }> = [ { value: 'overlay', label: _('Overlay') }, { value: 'below', label: _('Below') } ]; function makeBlankTile(): MosaicTile { return { id: typeof crypto !== 'undefined' && 'randomUUID' in crypto ? crypto.randomUUID() : `t-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`, image: '', imageAlt: '', imageWidth: null, imageHeight: null, label: _('New category'), link: '/', newTab: false }; } export default function CategoryMosaicSetting({ categoryMosaicWidget }: CategoryMosaicSettingProps) { const { heading, tiles: initialTiles = [], columns, aspect, layout, labelPosition } = categoryMosaicWidget ?? {}; const { register, setValue, watch, getValues } = useScopedFormContext(); const headingV = (watch('settings.heading') as string) ?? heading ?? ''; // `watch` drives re-render; reads inside mutation helpers go through // `getValues` so back-to-back updates (image URL + onLoadDimensions) read // each other's freshly-set state instead of the same stale closure value. const tiles = useArraySetting('settings.tiles', initialTiles); const colsV = (watch('settings.columns') as number | null) ?? columns ?? null; const aspectV = ((watch('settings.aspect') as string) ?? aspect ?? 'square') as MosaicAspect; const layoutV = ((watch('settings.layout') as string) ?? layout ?? 'uniform') as MosaicLayout; const labelPositionV = ((watch('settings.labelPosition') as string) ?? labelPosition ?? 'overlay') as MosaicLabelPosition; const readTiles = (): MosaicTile[] => asArray(getValues('settings.tiles'), initialTiles); const updateTile = (i: number, patch: Partial) => { const current = readTiles(); const next = current.map((t, idx) => (idx === i ? { ...t, ...patch } : t)); setValue('settings.tiles', next, { shouldDirty: true }); }; const moveTile = (from: number, to: number) => { const current = readTiles(); if (to < 0 || to >= current.length) return; const next = current.slice(); const [moved] = next.splice(from, 1); next.splice(to, 0, moved); setValue('settings.tiles', next, { shouldDirty: true }); }; const removeTile = (i: number) => { const current = readTiles(); if (current.length <= 2) return; setValue( 'settings.tiles', current.filter((_, idx) => idx !== i), { shouldDirty: true } ); }; const addTile = () => { const current = readTiles(); if (current.length >= 6) return; setValue('settings.tiles', [...current, makeBlankTile()], { shouldDirty: true }); }; return (
setValue('settings.heading', e.target.value || null, { shouldDirty: true }) } placeholder={_('Shop by category')} className={drawerInputClass} />
items={tiles} onAdd={addTile} onRemove={removeTile} onMove={moveTile} addLabel={_('Add tile')} minItems={2} maxItems={6} initiallyOpenFirst renderHeader={({ item }) => item.label || _('Untitled')} renderItem={({ item, index }) => ( <> updateTile(index, { label: e.target.value }) } placeholder={_('Women')} className={drawerInputClass} /> updateTile(index, { image: v, ...(v ? null : { imageWidth: null, imageHeight: null }) }) } onLoadDimensions={({ width: w, height: h }) => updateTile(index, { imageWidth: w, imageHeight: h }) } /> updateTile(index, { imageAlt: e.target.value }) } placeholder={_('Describe the image')} className={drawerInputClass} /> updateTile(index, { link: url })} /> updateTile(index, { newTab: v })} /> )} />
value={colsV ?? 0} options={[ { value: 0, label: _('Auto') }, { value: 3, label: '3' }, { value: 4, label: '4' }, { value: 5, label: '5' }, { value: 6, label: '6' } ]} onChange={(v) => setValue('settings.columns', v === 0 ? null : v, { shouldDirty: true }) } /> {(colsV === 3 || colsV === 4) && ( value={layoutV} options={[ { value: 'uniform', label: _('Uniform') }, { value: 'asymmetric', label: _('Asymmetric') } ]} onChange={(v) => setValue('settings.layout', v, { shouldDirty: true }) } /> )} value={aspectV} options={ASPECT_OPTIONS} onChange={(v) => setValue('settings.aspect', v, { shouldDirty: true }) } /> value={labelPositionV} options={LABEL_POSITION_OPTIONS} onChange={(v) => setValue('settings.labelPosition', v, { shouldDirty: true }) } />
); } export const query = ` query Query( $heading: String $tiles: JSON $columns: Int $aspect: String $layout: String $labelPosition: String ) { categoryMosaicWidget( heading: $heading tiles: $tiles columns: $columns aspect: $aspect layout: $layout labelPosition: $labelPosition ) { heading tiles columns aspect layout labelPosition } } `; export const variables = `{ heading: getWidgetSetting("heading"), tiles: getWidgetSetting("tiles", []), columns: getWidgetSetting("columns"), aspect: getWidgetSetting("aspect", "square"), layout: getWidgetSetting("layout", "uniform"), labelPosition: getWidgetSetting("labelPosition", "overlay") }`;