From 429538dd49775ef9cfdba3c5c345ab428615d53a Mon Sep 17 00:00:00 2001 From: Pavel Prichodko <14926950+prichodko@users.noreply.github.com> Date: Wed, 15 Jun 2022 16:13:35 +0200 Subject: [PATCH] feat: add chat message actions --- .../chat/components/chat-message/actions.tsx | 7 +- .../chat/components/chat-message/index.tsx | 66 ++++++++++++------- 2 files changed, 47 insertions(+), 26 deletions(-) diff --git a/packages/status-react/src/routes/chat/components/chat-message/actions.tsx b/packages/status-react/src/routes/chat/components/chat-message/actions.tsx index 53105db9..2587cba3 100644 --- a/packages/status-react/src/routes/chat/components/chat-message/actions.tsx +++ b/packages/status-react/src/routes/chat/components/chat-message/actions.tsx @@ -23,6 +23,7 @@ interface Props { onReplyClick: () => void onEditClick: () => void onPinClick: () => void + onDeleteClick: () => void onReactionClick: (reaction: Reaction) => void reacting: boolean onReactingChange: (reacting: boolean) => void @@ -32,12 +33,13 @@ interface Props { export const Actions = (props: Props) => { const { owner, - // pinned, onReplyClick, onEditClick, + onDeleteClick, + // pinned, // onPinClick, - onReactionClick, reacting, + onReactionClick, onReactingChange, reactions, } = props @@ -103,6 +105,7 @@ export const Actions = (props: Props) => { description="Are you sure you want to delete this message?" actionLabel="Delete" actionVariant="danger" + onAction={onDeleteClick} cancelLabel="Cancel" /> diff --git a/packages/status-react/src/routes/chat/components/chat-message/index.tsx b/packages/status-react/src/routes/chat/components/chat-message/index.tsx index ccad0976..316d1d95 100644 --- a/packages/status-react/src/routes/chat/components/chat-message/index.tsx +++ b/packages/status-react/src/routes/chat/components/chat-message/index.tsx @@ -1,12 +1,10 @@ import React, { useState } from 'react' -import snarkdown from 'snarkdown' - import { UserProfileDialog } from '~/src/components/user-profile-dialog' import { useChatContext } from '~/src/contexts/chat-context' import { BellIcon } from '~/src/icons/bell-icon' -import { PinIcon } from '~/src/icons/pin-icon' -import { useProtocol } from '~/src/protocol/provider' +// import { PinIcon } from '~/src/icons/pin-icon' +import { useProtocol } from '~/src/protocol' import { styled } from '~/src/styles/config' import { Avatar, @@ -28,11 +26,11 @@ import { Actions } from './actions' import { MessageReply } from './message-reply' import { MessageReactions } from './reactions' -import type { Message, Reaction } from '~/src/protocol/use-messages' +import type { Message, Reaction } from '~/src/protocol' interface Props { message: Message - previousMessage?: Message + prevMessage?: Message } // const MessageLink = forwardRef(function MessageLink( @@ -57,17 +55,28 @@ interface Props { // }) export const ChatMessage = (props: Props) => { - const { client } = useProtocol() + const { client, account } = useProtocol() const { message } = props - const owner = false const mention = false const pinned = false - const reply = false - const { messageId, chatId, contentType, clock, displayName, reactions } = - message + const { + messageId, + chatId, + contentType, + clock, + reactions, + sender, + responseTo, + } = message + + // TODO: remove usage of 0x prefix + const owner = '0x' + account?.publicKey === sender + const chat = client.community.getChatById(chatId) + + const member = client.community.getMember(sender) ?? {} const [editing, setEditing] = useState(false) const [reacting, setReacting] = useState(false) @@ -77,11 +86,20 @@ export const ChatMessage = (props: Props) => { const userProfileDialog = useDialog(UserProfileDialog) const handleMessageSubmit = (message: string) => { - client.community.chats.get(chatId).sendTextMessage(message) + chat.sendTextMessage(message) + } + + const handleMessageEdit = (message: string) => { + chat.editMessage(messageId, message) + setEditing(false) + } + + const handleMessageDelete = () => { + chat.deleteMessage(messageId) } const handleReaction = (reaction: Reaction) => { - client.community.getChatById(chatId).sendReaction(messageId, reaction) + chat.sendReaction(messageId, reaction) } const handleReplyClick = () => { @@ -96,10 +114,7 @@ export const ChatMessage = (props: Props) => { if (editing) { return ( - + - {displayName} + {member.username} } - onSelect={() => - userProfileDialog.open({ name: displayName }) - } + onSelect={() => userProfileDialog.open({ member })} > View Profile @@ -210,7 +227,7 @@ export const ChatMessage = (props: Props) => { - {displayName} + {member.username} {new Date(Number(clock)).toLocaleTimeString([], { @@ -235,6 +252,7 @@ export const ChatMessage = (props: Props) => { onEditClick={() => setEditing(true)} onReplyClick={handleReplyClick} onPinClick={handlePinClick} + onDeleteClick={handleMessageDelete} onReactionClick={handleReaction} reacting={reacting} onReactingChange={setReacting}