import { Check, ChevronDown, ChevronUp, Copy, CornerUpLeft } from "lucide-react" import React, { useState } from "react" import ReactMarkdown from "react-markdown" import { Prism as SyntaxHighlighter } from "react-syntax-highlighter" import { vscDarkPlus } from "react-syntax-highlighter/dist/esm/styles/prism" import remarkGfm from "remark-gfm" import { Button } from "../../ui/button" import { copyToClipboard, stringifyContent } from "./lib/chatUtils" import ContextTabs from "./ContextTabs" import { Socket } from "socket.io-client" interface MessageProps { message: { role: "user" | "assistant" content: string context?: string } setContext: (context: string | null) => void setIsContextExpanded: (isExpanded: boolean) => void socket: Socket | null } export default function ChatMessage({ message, setContext, setIsContextExpanded, socket, }: MessageProps) { const [expandedMessageIndex, setExpandedMessageIndex] = useState< number | null >(null) const [copiedText, setCopiedText] = useState(null) const renderCopyButton = (text: any) => ( ) const askAboutCode = (code: any) => { const contextString = stringifyContent(code) setContext(`Regarding this code:\n${contextString}`) setIsContextExpanded(false) } const renderMarkdownElement = (props: any) => { const { node, children } = props const content = stringifyContent(children) return (
{renderCopyButton(content)}
{React.createElement( node.tagName, { ...props, className: `${ props.className || "" } hover:bg-transparent rounded p-1 transition-colors`, }, children )}
) } return (
{message.role === "user" && message.context && (
{}} contextTabs={parseContextToTabs(message.context)} onRemoveTab={() => {}} isExpanded={expandedMessageIndex === 0} onToggleExpand={() => setExpandedMessageIndex(expandedMessageIndex === 0 ? null : 0)} className="[&_div:first-child>div:first-child>div]:bg-[#0D0D0D] [&_button:first-child]:hidden [&_button:last-child]:hidden" /> {expandedMessageIndex === 0 && (
{renderCopyButton( message.context.replace(/^Regarding this code:\n/, "") )}
{(() => { const code = message.context.replace( /^Regarding this code:\n/, "" ) const match = /language-(\w+)/.exec(code) const language = match ? match[1] : "typescript" return (