/* 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) => ` `;
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 = `${c} `)
.join("")}