diff --git a/app/components/ConversationMessages.tsx b/app/components/ConversationMessages.tsx index 7b2d4c5..c855db1 100644 --- a/app/components/ConversationMessages.tsx +++ b/app/components/ConversationMessages.tsx @@ -18,6 +18,7 @@ export interface Message { interface ConversationMessagesProps { messages: Message[]; handleHideMessage: (index: number) => Promise; + handleDeleteMessage: (index: number) => Promise; userName?: string; } @@ -30,13 +31,14 @@ type MathComponents = { const ConversationMessages: React.FC = ({ messages, handleHideMessage, + handleDeleteMessage, userName = "User", }) => { const messagesEndRef = useRef(null); useEffect(() => { messagesEndRef.current?.scrollIntoView({ behavior: "smooth" }); - }, [messages]); + }, [messages.length]); return ( @@ -45,7 +47,10 @@ const ConversationMessages: React.FC = ({ key={index} borderRadius="none" borderWidth="1px" - p={4} + pt={4} + pl={4} + pr={4} + pb={1} borderColor={message.hidden ? "gray.300" : "black"} alignSelf={message.role === "user" ? "flex-end" : "flex-start"} maxWidth="100%" @@ -61,6 +66,11 @@ const ConversationMessages: React.FC = ({ remarkPlugins={[remarkGfm, remarkMath]} components={ { + p: ({ children }) => ( + + {children} + + ), code({ node, className, children, ...props }) { const match = /language-(\w+)/.exec(className || ""); return match ? ( @@ -98,10 +108,22 @@ const ConversationMessages: React.FC = ({ size="xs" variant="ghost" onClick={() => handleHideMessage(index)} - ml="auto" - mt={2} + ml={-1} + mt={4} + px={1} + color="gray.600" > - hide + {message.hidden ? "show" : "hide"} + + ))} diff --git a/app/page.tsx b/app/page.tsx index 6be55f3..9bd8d24 100644 --- a/app/page.tsx +++ b/app/page.tsx @@ -24,6 +24,8 @@ import { handleModelChange, fetchConversations, fetchConversationById, + handleHideMessage, + handleDeleteMessage, } from "./utils"; import Link from "next/link"; @@ -48,27 +50,27 @@ export default function Home() { setTextValue(event.target.value); }; - const handleHideMessage = async (index: number) => { - try { - const updatedMessages = messages.map((msg, i) => - i === index ? { ...msg, hidden: !msg.hidden } : msg + const handleHideMessageWrapper = async (index: number) => { + if (conversationId) { + await handleHideMessage( + index, + messages, + conversationId, + setMessages, + cookies ); - await fetch( - `${process.env.BACKEND_URL}/conversations/${conversationId}/messages`, - { - method: "PUT", - headers: { - "Content-Type": "application/json", - Authorization: `Bearer ${cookies.token}`, - }, - body: JSON.stringify({ messages: updatedMessages }), - mode: "cors", - credentials: "include", - } + } + }; + + const handleDeleteMessageWrapper = async (index: number) => { + if (conversationId) { + await handleDeleteMessage( + index, + messages, + conversationId, + setMessages, + cookies ); - setMessages(updatedMessages); - } catch (error) { - console.error("Error toggling message visibility:", error); } }; @@ -165,7 +167,8 @@ export default function Home() { diff --git a/app/utils.ts b/app/utils.ts index 2136a52..d2fb6e8 100644 --- a/app/utils.ts +++ b/app/utils.ts @@ -206,3 +206,62 @@ export const handleModelChange = async ( }); } }; + + +export const handleHideMessage = async ( + index: number, + messages: Message[], + conversationId: string, + setMessages: React.Dispatch>, + cookies: { [key: string]: string } +) => { + try { + const updatedMessages = messages.map((msg, i) => + i === index ? { ...msg, hidden: !msg.hidden } : msg + ); + await fetch( + `${process.env.BACKEND_URL}/conversations/${conversationId}/messages`, + { + method: "PUT", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${cookies.token}`, + }, + body: JSON.stringify(updatedMessages), + mode: "cors", + credentials: "include", + } + ); + setMessages(updatedMessages); + } catch (error) { + console.error("Error toggling message visibility:", error); + } +}; + +export const handleDeleteMessage = async ( + index: number, + messages: Message[], + conversationId: string, + setMessages: React.Dispatch>, + cookies: { [key: string]: string } +) => { + try { + const updatedMessages = messages.filter((_, i) => i !== index); + await fetch( + `${process.env.BACKEND_URL}/conversations/${conversationId}/messages`, + { + method: "PUT", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${cookies.token}`, + }, + body: JSON.stringify(updatedMessages), + mode: "cors", + credentials: "include", + } + ); + setMessages(updatedMessages); + } catch (error) { + console.error("Error deleting message:", error); + } +}; \ No newline at end of file