/* eslint-disable @typescript-eslint/no-explicit-any */ import React, { ReactNode, useMemo } from "react"; import { ScrollArea, ScrollBar } from "@sparkle/components"; import { ContentBlockWrapper } from "@sparkle/components/markdown/ContentBlockWrapper"; const getNodeText = (node: ReactNode): string => { if (["string", "number"].includes(typeof node)) { return node as string; } if (node instanceof Array) { return node.map(getNodeText).join(""); } if (node && typeof node === "object" && "props" in node) { return getNodeText(node.props.children); } return ""; }; export function TableBlock({ children }: { children: React.ReactNode }) { const tableData = useMemo(() => { const [headNode, bodyNode] = Array.from(children as [any, any]); if ( !headNode || !bodyNode || !("props" in headNode) || !("props" in bodyNode) ) { return; } const headCells = headNode.props.children[0].props.children.map((c: any) => getNodeText(c.props.children) ); const headHtml = `${headCells .map((c: any) => `${c}`) .join("")}`; const headPlain = headCells.join("\t"); const bodyRows = bodyNode.props.children.map((row: any) => row.props.children.map((cell: any) => { const children = cell.props.children; return (Array.isArray(children) ? children : [children]) .map((child: any) => child?.type?.name === "CiteBlock" ? "" : getNodeText(child) ) .join(""); }) ); const bodyHtml = `${bodyRows .map((row: any) => { return `${row .map((cell: any) => `${cell}`) .join("")}`; }) .join("")}`; const bodyPlain = bodyRows.map((row: any) => row.join("\t")).join("\n"); return { "text/html": `${headHtml}${bodyHtml}
`, "text/plain": headPlain + "\n" + bodyPlain, }; }, [children]); return ( {children}
); } export function TableHeadBlock({ children }: { children: React.ReactNode }) { return ( {children} ); } export function TableBodyBlock({ children }: { children: React.ReactNode }) { return ( {children} ); } export function TableHeaderBlock({ children }: { children: React.ReactNode }) { return ( {children} ); } export function TableDataBlock({ children }: { children: React.ReactNode }) { return ( {Array.isArray(children) ? ( children.map((child: any, i) => { if (child === "
") { return
; } return {child}; }) ) : ( <>{children} )} ); }