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 }) => (
-
- ),
- ol: ({ node, ...props }) => (
-
- ),
- a: ({ node, ...props }) => (
-
- ),
- }}
- >
- {line}
-
-
- ))}
+
{
+ 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) => (
+
+ {index > 0 &&
}
+
(
+
+ ),
+ h2: ({ node, ...props }) => (
+
+ ),
+ h3: ({ node, ...props }) => (
+
+ ),
+ h4: ({ node, ...props }) => (
+
+ ),
+ h5: ({ node, ...props }) => (
+
+ ),
+ ul: ({ node, ...props }) => (
+
+ ),
+ ol: ({ node, ...props }) => (
+
+ ),
+ a: ({ node, ...props }) => (
+
+ ),
+ }}
+ >
+ {line}
+
+
+ ))
+ )}
@@ -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") ? (
+