import React from 'react'; interface SlateLeaf { text: string; bold?: boolean; italic?: boolean; color?: 'primary' | 'success' | 'warning' | 'destructive'; } interface SlateNode { type?: 'paragraph' | 'block-quote' | 'heading-one' | 'heading-two' | 'list-item'; children: (SlateLeaf | SlateNode)[]; } interface SlateRendererProps { nodes?: any[]; variables?: Record; } export default function SlateRenderer({ nodes, variables = {} }: SlateRendererProps) { if (!nodes || !Array.isArray(nodes)) { return null; } // Helper to replace variable strings like {target_price} or {partner_type} const replaceVariables = (text: string): string => { let result = text; Object.entries(variables).forEach(([key, val]) => { const placeholder = `{${key}}`; if (result.includes(placeholder)) { if (typeof val === 'number') { result = result.replace(new RegExp(placeholder, 'g'), val.toLocaleString('ko-KR')); } else { result = result.replace(new RegExp(placeholder, 'g'), String(val)); } } }); return result; }; const renderLeaf = (leaf: SlateLeaf, key: string) => { const text = replaceVariables(leaf.text); let classes = ''; if (leaf.bold) { classes += ' font-bold '; } if (leaf.italic) { classes += ' italic '; } if (leaf.color === 'primary') { return ( {text} ); } if (leaf.color === 'success') { return ( {text} ); } if (leaf.color === 'warning') { return ( {text} ); } if (leaf.color === 'destructive') { return ( {text} ); } return ( {text} ); }; const renderNode = (node: any, index: number): React.ReactNode => { const key = `node-${index}`; // If it's pure leaf structure (contains no children arrays, but has text attribute) if (node.text !== undefined) { return renderLeaf(node as SlateLeaf, key); } const childrenElements = (node.children || []).map((child: any, childIdx: number) => { if (child.text !== undefined) { return renderLeaf(child as SlateLeaf, `${key}-${childIdx}`); } return renderNode(child, childIdx); }); switch (node.type) { case 'heading-one': return

{childrenElements}

; case 'heading-two': return

{childrenElements}

; case 'block-quote': return (
{childrenElements}
); case 'list-item': return
  • {childrenElements}
  • ; case 'paragraph': default: return

    {childrenElements}

    ; } }; return (
    {nodes.map((node, i) => renderNode(node, i))}
    ); }