import { useSocket } from "@/context/SocketContext" import { TTab } from "@/lib/types" import { Check, CornerUpLeft, FileText, X } from "lucide-react" import monaco from "monaco-editor" import { Components } from "react-markdown" import { Prism as SyntaxHighlighter } from "react-syntax-highlighter" import { vscDarkPlus } from "react-syntax-highlighter/dist/esm/styles/prism" import { Button } from "../../../ui/button" import ApplyButton from "../ApplyButton" import { isFilePath, stringifyContent } from "./chatUtils" // Create markdown components for chat message component export const createMarkdownComponents = ( renderCopyButton: (text: any) => JSX.Element, renderMarkdownElement: (props: any) => JSX.Element, askAboutCode: (code: any) => void, activeFileName: string, activeFileContent: string, editorRef: any, handleApplyCode: (mergedCode: string, originalCode: string) => void, selectFile: (tab: TTab) => void, mergeDecorationsCollection?: monaco.editor.IEditorDecorationsCollection, setMergeDecorationsCollection?: (collection: undefined) => void ): Components => ({ code: ({ node, className, children, ...props }: { node?: import("hast").Element className?: string children?: React.ReactNode [key: string]: any }) => { const match = /language-(\w+)/.exec(className || "") return match ? (
{match[1]}
{renderCopyButton(children)}
{!mergeDecorationsCollection ? ( ) : ( <>
)}
{stringifyContent(children)}
) : ( {children} ) }, // Render markdown elements p: ({ node, children, ...props }) => { const content = stringifyContent(children) const { socket } = useSocket() if (isFilePath(content)) { const isNewFile = content.endsWith("(new file)") const filePath = ( isNewFile ? content.replace(" (new file)", "") : content ) .split("/") .filter((part, index) => index !== 0) .join("/") const handleFileClick = () => { if (isNewFile) { socket?.emit( "createFile", { name: filePath, }, (response: any) => { if (response.success) { const tab: TTab = { id: filePath, name: filePath.split("/").pop() || "", saved: true, type: "file", } selectFile(tab) } } ) } else { const tab: TTab = { id: filePath, name: filePath.split("/").pop() || "", saved: true, type: "file", } selectFile(tab) } } return (
{content}
) } return renderMarkdownElement({ node, children, ...props }) }, h1: ({ node, children, ...props }) => renderMarkdownElement({ node, children, ...props }), h2: ({ node, children, ...props }) => renderMarkdownElement({ node, children, ...props }), h3: ({ node, children, ...props }) => renderMarkdownElement({ node, children, ...props }), h4: ({ node, children, ...props }) => renderMarkdownElement({ node, children, ...props }), h5: ({ node, children, ...props }) => renderMarkdownElement({ node, children, ...props }), h6: ({ node, children, ...props }) => renderMarkdownElement({ node, children, ...props }), ul: (props) => ( ), ol: (props) => (
    {props.children}
), })