'use client' import { useState } from 'react' import Script from 'next/script' import { cn } from '@/lib/utils' import { RichTextRenderer } from './RichTextRenderer' import type { FAQBlock as FAQBlockType, FAQ } from '@/lib/types' type FAQBlockProps = Omit & { faqs?: FAQ[] } export function FAQBlock({ title, subtitle, displayMode, selectedFaqs, filterCategory, layout = 'accordion', expandFirst = false, showSchema = true, faqs: externalFaqs, }: FAQBlockProps) { // Use selectedFaqs if displayMode is 'selected', otherwise use externalFaqs const items = displayMode === 'selected' ? selectedFaqs : externalFaqs if (!items || items.length === 0) return null // Generate JSON-LD schema data const schemaData = showSchema ? { '@context': 'https://schema.org', '@type': 'FAQPage', mainEntity: items.map((faq) => ({ '@type': 'Question', name: faq.question, acceptedAnswer: { '@type': 'Answer', text: extractTextFromRichText(faq.answer), }, })), } : null return (
{/* Section Header */} {(title || subtitle) && (
{title &&

{title}

} {subtitle && (

{subtitle}

)}
)} {/* FAQ Items */}
{layout === 'accordion' ? ( ) : layout === 'grid' ? ( ) : ( )}
{/* JSON-LD Schema using Next.js Script component for safety */} {schemaData && ( )}
) } function AccordionFAQ({ items, expandFirst, }: { items: FAQ[] expandFirst: boolean }) { const [openIndex, setOpenIndex] = useState( expandFirst ? 0 : null ) return (
{items.map((faq, index) => (
))}
) } function ListFAQ({ items }: { items: FAQ[] }) { return (
{items.map((faq) => (

{faq.question}

))}
) } function GridFAQ({ items }: { items: FAQ[] }) { return (
{items.map((faq) => (

{faq.question}

))}
) } // Helper to extract plain text from RichText for schema function extractTextFromRichText(richText: FAQ['answer']): string { if (!richText?.root?.children) return '' function extractFromNode(node: Record): string { if (node.text) return node.text as string const children = node.children as Record[] | undefined if (children) { return children.map(extractFromNode).join('') } return '' } return richText.root.children .map((node) => extractFromNode(node as Record)) .join(' ') .trim() }