From 9975ac17a67682ae406b0835a6dd6f19d1120cf8 Mon Sep 17 00:00:00 2001 From: Leo dev Date: Mon, 22 Dec 2025 14:16:19 +0100 Subject: [PATCH] Message edit --- src/components/channel/MessageBox.tsx | 303 ++++++++++++++++---------- src/types/protocol.ts | 3 +- 2 files changed, 186 insertions(+), 120 deletions(-) diff --git a/src/components/channel/MessageBox.tsx b/src/components/channel/MessageBox.tsx index 6f85fad..19b0a9e 100644 --- a/src/components/channel/MessageBox.tsx +++ b/src/components/channel/MessageBox.tsx @@ -11,13 +11,13 @@ import { DropdownMenuTrigger, } from "../ui/dropdown-menu"; import { ChevronLeftIcon, SendIcon, SmilePlusIcon } from "lucide-react"; -import { useRef, useState } from "react"; +import { useEffect, useRef, useState } from "react"; import { format, isToday, isYesterday } from "date-fns"; import ReactMarkdown from "react-markdown"; import remarkGfm from "remark-gfm"; import { UserProfile } from "@/hooks/get-user"; import ProfilePicture from "../ProfilePicture"; -import App from "@/types/app"; +import App, { SetState } from "@/types/app"; import { Textarea } from "../ui/textarea"; import { ContextMenu, @@ -46,8 +46,20 @@ function MessageContainer({ sendRequest: (content: ClientMessage) => void; }) { const [profile, setProfile] = useState(); + const [isEditing, setIsEditing] = useState(false); + const [editText, setEditText] = useState(message.contents); - app.getUserById(message.from).then(setProfile); + useEffect(() => { + let mounted = true; + + app.getUserById(message.from).then((p) => { + if (mounted) setProfile(p); + }); + + return () => { + mounted = false; + }; + }, [app, message.from]); return ( @@ -72,69 +84,95 @@ function MessageContainer({

-
- {message.contents.split("\n\n").map((line, index) => ( -
- {index > 0 &&
} - ( -

- ), - h2: ({ node, ...props }) => ( -

- ), - h3: ({ node, ...props }) => ( -

- ), - h4: ({ node, ...props }) => ( -

- ), - h5: ({ node, ...props }) => ( -

- ), - ul: ({ node, ...props }) => ( -
- ))} +
{ + if (e.key === "Escape" && isEditing) { + setIsEditing(false); + setEditText(message.contents); + } + }} + > + {isEditing ? ( + { + sendRequest({ + type: "edit_message", + params: { + message_id: message.id, + new_contents: editText, + }, + }); + + setIsEditing(false); + }} + /> + ) : ( + message.contents.split("\n\n").map((line, index) => ( + + )) + )}
@@ -142,6 +180,16 @@ function MessageContainer({ Copy message + { + setEditText(message.contents); + setIsEditing(true); + }} + disabled={message.from !== app.profile?.id} + > + Edit + ; + onEnter: () => void; +}) { + const inputRef = useRef(null); + + useEffect(() => { + inputRef.current?.focus(); + }, [inputRef]); + + return text.includes("\n") ? ( +