import { Editor, Row } from '@components/common/form/Editor.js'; import { drawerInputClass, drawerTextareaClass, Field, RepeatableAccordion, Section, Segmented, Toggle, useArraySetting, useScopedFormContext } from '@components/common/page-builder/index.js'; import { Button } from '@components/common/ui/Button.js'; import { _ } from '@evershop/evershop/lib/locale/translate/_'; import { FileText, Plus, Trash2 } from 'lucide-react'; import React from 'react'; import type { FaqMaxWidth, FaqSection_ } from './FaqBlock.js'; /** * FAQ block drawer form. Owns a `sections` array (discriminated by * `type: 'prose' | 'faq'`) plus a heading and width toggle. Prose sections * delegate authoring to the shared `Editor` (EditorJS Row[]). FAQ * sections are nested repeatable items (q+a). Both shapes coexist in * one list so the merchant can interleave prose and accordions. */ interface FaqBlockSettingProps { faqBlockWidget?: { heading?: string | null; sections?: FaqSection_[]; maxWidth?: FaqMaxWidth | null; allowMultipleOpen?: boolean | null; }; } const WIDTH_OPTIONS: ReadonlyArray<{ value: FaqMaxWidth; label: string }> = [ { value: 'narrow', label: _('Narrow') }, { value: 'normal', label: _('Normal') }, { value: 'wide', label: _('Wide') } ]; function makeId(): string { return typeof crypto !== 'undefined' && 'randomUUID' in crypto ? crypto.randomUUID() : `s-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`; } function makeBlankProse(): FaqSection_ { return { id: makeId(), type: 'prose', content: '[]' }; } function makeBlankFaqGroup(): FaqSection_ { return { id: makeId(), type: 'faq', heading: null, items: [ { id: makeId(), question: 'New question', answer: '' } ] }; } export default function FaqBlockSetting({ faqBlockWidget }: FaqBlockSettingProps) { const { heading = '', sections: initialSections = [], maxWidth, allowMultipleOpen } = faqBlockWidget ?? {}; const { register, setValue, watch } = useScopedFormContext(); const sections = useArraySetting( 'settings.sections', initialSections ); const widthState = ((watch('settings.maxWidth') as string | null) ?? maxWidth ?? 'normal') as FaqMaxWidth; const allowMultiple = (watch('settings.allowMultipleOpen') as boolean | null) ?? allowMultipleOpen ?? false; // EditorJS interaction: the Editor binds to `name`, writes to that path on // every change. We need one form path per prose section because two // mounted editors can't share a path. Use `temp_editor_` paths and // sync the value back to `settings.sections[N].content` via watch. const sectionWatches = sections.map((s, i) => s.type === 'prose' ? watch(`temp_editor_${s.id}`) : undefined ); React.useEffect(() => { let dirty = false; const next = sections.map((s, i) => { if (s.type !== 'prose') return s; const w = sectionWatches[i]; if (w !== undefined && JSON.stringify(w) !== s.content) { dirty = true; return { ...s, content: w as Row[] }; } return s; }); if (dirty) { setValue('settings.sections', next, { shouldDirty: true }); } }, [JSON.stringify(sectionWatches)]); const updateSection = (index: number, patch: Partial) => { const next = sections.map((s, i) => i === index ? ({ ...s, ...patch } as FaqSection_) : s ); setValue('settings.sections', next, { shouldDirty: true }); }; const moveSection = (from: number, to: number) => { if (to < 0 || to >= sections.length) return; const next = sections.slice(); const [moved] = next.splice(from, 1); next.splice(to, 0, moved); setValue('settings.sections', next, { shouldDirty: true }); }; const removeSection = (index: number) => { if (sections.length <= 1) return; setValue( 'settings.sections', sections.filter((_, i) => i !== index), { shouldDirty: true } ); }; const addProse = () => setValue('settings.sections', [...sections, makeBlankProse()], { shouldDirty: true }); const addFaq = () => setValue('settings.sections', [...sections, makeBlankFaqGroup()], { shouldDirty: true }); const updateFaqItem = ( sectionIndex: number, itemIndex: number, patch: Partial<{ question: string; answer: string }> ) => { const section = sections[sectionIndex]; if (section.type !== 'faq') return; const nextItems = section.items.map((it, i) => i === itemIndex ? { ...it, ...patch } : it ); updateSection(sectionIndex, { items: nextItems }); }; const moveFaqItem = ( sectionIndex: number, from: number, to: number ) => { const section = sections[sectionIndex]; if (section.type !== 'faq') return; if (to < 0 || to >= section.items.length) return; const nextItems = section.items.slice(); const [moved] = nextItems.splice(from, 1); nextItems.splice(to, 0, moved); updateSection(sectionIndex, { items: nextItems }); }; const removeFaqItem = (sectionIndex: number, itemIndex: number) => { const section = sections[sectionIndex]; if (section.type !== 'faq') return; if (section.items.length <= 1) return; updateSection(sectionIndex, { items: section.items.filter((_, i) => i !== itemIndex) }); }; const addFaqItem = (sectionIndex: number) => { const section = sections[sectionIndex]; if (section.type !== 'faq') return; updateSection(sectionIndex, { items: [ ...section.items, { id: makeId(), question: 'New question', answer: '' } ] }); }; return (
setValue('settings.heading', e.target.value, { shouldDirty: true }) } placeholder={_('e.g. Frequently asked')} className={drawerInputClass} />
items={sections} onRemove={removeSection} onMove={moveSection} minItems={1} renderHeader={({ item }) => { if (item.type === 'prose') { return ( {_('Prose')} ); } return _('FAQ group · ${count} items', { count: String(item.items?.length ?? 0) }); }} renderItem={({ item, index }) => { if (item.type === 'prose') { const rows: Row[] = (() => { if (Array.isArray(item.content)) return item.content as Row[]; if (typeof item.content !== 'string') return []; const trimmed = item.content.trim(); if (!trimmed.startsWith('[')) return []; try { const p = JSON.parse(trimmed); return Array.isArray(p) ? (p as Row[]) : []; } catch { return []; } })(); return ( ); } return ( <> updateSection(index, { heading: e.target.value || null }) } placeholder={_('e.g. Shipping')} className={drawerInputClass} /> removeFaqItem(index, i)} onMove={(f, t) => moveFaqItem(index, f, t)} onAdd={() => addFaqItem(index)} addLabel={_('Add question')} minItems={1} renderHeader={({ item: it }) => it.question || _('Untitled') } renderItem={({ item: it, index: itIdx }) => ( <> updateFaqItem(index, itIdx, { question: e.target.value }) } placeholder={_('e.g. How long does shipping take?')} className={drawerInputClass} />