From 957926f7f1ec06d8abf3b5337bcabb8d9a3adce5 Mon Sep 17 00:00:00 2001 From: Indrajeet Patil Date: Fri, 11 Oct 2024 22:06:43 +0200 Subject: [PATCH] Revert "Update AssistantMessage.tsx" This reverts commit fa1364178ce372b20563aac452d52982b816372e. --- .../components/messages/AssistantMessage.tsx | 130 +++++++++--------- 1 file changed, 64 insertions(+), 66 deletions(-) diff --git a/frontend/components/messages/AssistantMessage.tsx b/frontend/components/messages/AssistantMessage.tsx index 49f6a28..b385593 100644 --- a/frontend/components/messages/AssistantMessage.tsx +++ b/frontend/components/messages/AssistantMessage.tsx @@ -5,6 +5,7 @@ import { vs, vscDarkPlus, } from "react-syntax-highlighter/dist/esm/styles/prism"; + import FileCopyIcon from "@mui/icons-material/FileCopy"; import SmartToyIcon from "@mui/icons-material/SmartToy"; import { @@ -16,71 +17,6 @@ import { useTheme, } from "@mui/material"; -const CodeBlock: React.FC<{ - inline?: boolean; - className?: string; - children?: React.ReactNode; -}> = ({ inline, className, children }) => { - const [codeCopied, setCodeCopied] = useState(null); - const theme = useTheme(); - - const match = /language-(\w+)/.exec(className ?? ""); - const codeContent = String(children).replace(/\n$/, ""); - - const handleCodeCopy = (code: string) => { - navigator.clipboard.writeText(code); - setCodeCopied(code); - setTimeout(() => setCodeCopied(null), 2000); - }; - - if (!inline && match) { - return ( - - - {codeContent} - - - handleCodeCopy(codeContent)} - sx={{ - position: "absolute", - top: 4, - right: 4, - backgroundColor: - theme.palette.mode === "dark" ? "#4caf50" : "#ff9800", - color: theme.palette.common.white, - fontSize: 18, - "&:hover": { - backgroundColor: - theme.palette.mode === "dark" ? "#45a049" : "#e65100", - }, - }} - > - - - - - ); - } - - return ( - - {children} - - ); -}; - interface AssistantMessageProps { content: string; isFirstMessage: boolean; @@ -91,6 +27,7 @@ const AssistantMessage: React.FC = ({ isFirstMessage, }) => { const [copied, setCopied] = useState(false); + const [codeCopied, setCodeCopied] = useState(null); const theme = useTheme(); const handleCopy = (text: string) => { @@ -99,8 +36,69 @@ const AssistantMessage: React.FC = ({ setTimeout(() => setCopied(false), 2000); }; + const handleCodeCopy = (code: string) => { + navigator.clipboard.writeText(code); + setCodeCopied(code); + setTimeout(() => setCodeCopied(null), 2000); + }; + const renderers = { - code: CodeBlock, + code({ + inline, + className, + children, + }: { + inline?: boolean; + className?: string; + children?: React.ReactNode; + }): React.JSX.Element { + const match = /language-(\w+)/.exec(className ?? ""); + const codeContent = String(children).replace(/\n$/, ""); + + return !inline && match ? ( + + + {codeContent} + + + handleCodeCopy(codeContent)} + sx={{ + position: "absolute", + top: 4, + right: 4, + backgroundColor: + theme.palette.mode === "dark" ? "#4caf50" : "#ff9800", + color: theme.palette.common.white, + fontSize: 18, + "&:hover": { + backgroundColor: + theme.palette.mode === "dark" ? "#45a049" : "#e65100", + }, + }} + > + + + + + ) : ( + + {children} + + ); + }, }; return (