import React, { useEffect, useState } from "react"; import { highlightSql } from "./highlight"; export interface SqlBlock { name: string; fields?: number; testStatus?: "passed" | "failed" | "untested"; sql: string; comments?: Record; } type Layout = "vertical" | "horizontal"; const STATUS_LABELS: Record = { passed: "passed", failed: "failed", untested: "untested", }; const STATUS_CLASSES: Record = { passed: "bg-green-100 text-green-800", failed: "bg-red-100 text-red-800", untested: "bg-gray-100 text-gray-600", }; function Block({ block }: { block: SqlBlock }) { const [open, setOpen] = useState(true); const [html, setHtml] = useState(""); useEffect(() => { let cancelled = false; highlightSql(block.sql).then((result) => { if (!cancelled) setHtml(result); }); return () => { cancelled = true; }; }, [block.sql]); const badge = block.testStatus ? STATUS_LABELS[block.testStatus] : null; const badgeClass = block.testStatus ? STATUS_CLASSES[block.testStatus] : ""; return (
{open && (
{html ? ( // eslint-disable-next-line react/no-danger
) : (
{block.sql}
)} {block.comments && Object.keys(block.comments).length > 0 && (
{Object.entries(block.comments).map(([field, comment]) => (
{field}
{comment}
))}
)}
)}
); } export function SqlViewer({ blocks }: { blocks: SqlBlock[] }) { const [layout, setLayout] = useState("vertical"); return (
{blocks.map((block) => (
))}
); }