import { asArray, drawerInputClass, drawerTextareaClass, Field, RepeatableAccordion, Section, Segmented, Toggle, useArraySetting, useScopedFormContext } from '@components/common/page-builder/index.js'; import { CollectionPicker } from '@components/common/page-builder/pickers/CollectionPicker.js'; import { LinkPicker } from '@components/common/page-builder/pickers/LinkPicker.js'; import { _ } from '@evershop/evershop/lib/locale/translate/_'; import React from 'react'; interface CollectionRow { id: string; title: string; subText: string; source: string; viewAllLink: string | null; viewAllLabel: string; } interface CollectionStackSettingProps { collectionStackWidget?: { collections?: CollectionRow[]; productCount?: number; countPerRow?: number; divider?: boolean; }; } function makeBlankRow(): CollectionRow { return { id: typeof crypto !== 'undefined' && 'randomUUID' in crypto ? crypto.randomUUID() : `r-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`, title: 'New collection', subText: '', source: '', viewAllLink: null, viewAllLabel: 'View all →' }; } export default function CollectionStackSetting({ collectionStackWidget }: CollectionStackSettingProps) { const { collections: initialCollections = [], productCount, countPerRow, divider } = collectionStackWidget ?? {}; const { register, setValue, watch, getValues } = useScopedFormContext(); const rows = useArraySetting( 'settings.collections', initialCollections ); const productCountV = (watch('settings.productCount') as number) ?? productCount ?? 4; const countPerRowV = ((watch('settings.countPerRow') as number) ?? countPerRow ?? 4) as | 2 | 3 | 4 | 5 | 6; const dividerV = (watch('settings.divider') as boolean | null) ?? divider ?? true; // Read live form state inside mutation helpers to dodge the stale-closure // race that bites when two updates fire in quick succession. const readRows = (): CollectionRow[] => asArray(getValues('settings.collections'), initialCollections); const updateRow = (i: number, patch: Partial) => { const current = readRows(); const next = current.map((r, idx) => (idx === i ? { ...r, ...patch } : r)); setValue('settings.collections', next, { shouldDirty: true }); }; const moveRow = (from: number, to: number) => { const current = readRows(); if (to < 0 || to >= current.length) return; const next = current.slice(); const [moved] = next.splice(from, 1); next.splice(to, 0, moved); setValue('settings.collections', next, { shouldDirty: true }); }; const removeRow = (i: number) => { const current = readRows(); if (current.length <= 1) return; setValue( 'settings.collections', current.filter((_, idx) => idx !== i), { shouldDirty: true } ); }; const addRow = () => { const current = readRows(); if (current.length >= 3) return; setValue('settings.collections', [...current, makeBlankRow()], { shouldDirty: true }); }; return (
items={rows} onAdd={addRow} onRemove={removeRow} onMove={moveRow} addLabel={_('Add collection')} minItems={1} maxItems={3} initiallyOpenFirst renderHeader={({ item }) => item.title || _('Untitled')} renderItem={({ item, index }) => ( <> updateRow(index, { title: e.target.value }) } placeholder={_('The Linen Edit')} className={drawerInputClass} />