import { useMemo } from "react"; import dynamic from "next/dynamic"; import { IS_DEMO } from "../studio-mode"; import WorkspaceMarkdown from "./WorkspaceMarkdown"; const DemoTags = IS_DEMO ? dynamic(() => import("../../demo/DemoTags"), { ssr: false }) : null; import { parseMarkdownFrontmatter } from "./markdown"; import styles from "./files-workspace.module.css"; export type PreviewType = "markdown" | "csv" | "text"; export function getPreviewType(filePath: string): PreviewType { const ext = filePath.split(".").pop()?.toLowerCase() ?? ""; if (ext === "md" || ext === "mdx") return "markdown"; if (ext === "csv") return "csv"; return "text"; } function parseCsv(raw: string): string[][] { return raw .trimEnd() .split(/\r?\n/) .map((line) => { const cells: string[] = []; let current = ""; let quoted = false; for (let index = 0; index < line.length; index += 1) { const char = line[index]; if (char === '"') { if (quoted && line[index + 1] === '"') { current += '"'; index += 1; } else quoted = !quoted; } else if (char === "," && !quoted) { cells.push(current); current = ""; } else current += char; } cells.push(current); return cells; }); } function MarkdownPreview({ content }: { content: string }) { const { body, entries } = useMemo( () => parseMarkdownFrontmatter(content), [content], ); return (
{entries.length > 0 && (
{entries.map(({ key, value }, index) => (
{key}
{key === "memory_tags" && DemoTags ? ( ) : ( value )}
))}
)}
); } function CsvPreview({ content }: { content: string }) { const rows = useMemo(() => parseCsv(content), [content]); const header = rows[0] ?? []; return (
{header.slice(0, 50).map((cell, index) => ( ))} {rows.slice(1, 501).map((row, rowIndex) => ( {row.slice(0, 50).map((cell, cellIndex) => ( ))} ))}
{cell}
{cell}
); } export default function FilePreview({ filePath, content, }: { filePath: string; content: string; }) { const type = getPreviewType(filePath); if (type === "markdown") return ; if (type === "csv") return ; return
{content}
; }