"use client" import type { DocumentsWithMemoriesResponseSchema } from "@repo/validation/api" import type { z } from "zod" import { dmSansClassName } from "@/lib/fonts" import { cn } from "@lib/utils" import { useMemo } from "react" type DocumentsResponse = z.infer type DocumentWithMemories = DocumentsResponse["documents"][0] type TipTapNode = { type: string text?: string content?: TipTapNode[] attrs?: Record } function extractTextFromTipTapContent(content: string): string { try { const json = JSON.parse(content) as TipTapNode return extractTextFromNode(json) } catch { return content } } function extractTextFromNode(node: TipTapNode): string { if (node.type === "text" && node.text) { return node.text } if (!node.content) { return "" } const texts: string[] = [] for (const child of node.content) { const text = extractTextFromNode(child) if (text) { texts.push(text) } } const blockTypes = [ "paragraph", "heading", "listItem", "blockquote", "codeBlock", ] if (blockTypes.includes(node.type)) { return `${texts.join("")}\n` } return texts.join("") } function NoteIcon() { return ( Note Icon ) } export function NotePreview({ document }: { document: DocumentWithMemories }) { const previewText = useMemo(() => { if (!document.content) return "" return extractTextFromTipTapContent(document.content).trim() }, [document.content]) return (

Note

{document.title && (

{document.title}

)} {previewText && (

{previewText}

)}
) }