Open-source and local-first Slack and Notion alternative that puts you in control of your data
TypeScript chatchat-applicationcrdteditorelectronknowledge-baselocal-firstnotionnotion-alternativerealtime-collaborationself-hostedslacksqliteteam-collaborationwikiyjs
Refactor message reactions
Hakan Shehue4e74ad87f16
diff --git a/apps/desktop/src/renderer/components/messages/message-actions.tsx b/apps/desktop/src/renderer/components/messages/message-actions.tsxnew file mode 100644index 00000000..d99dead7--- /dev/null+++ b/apps/desktop/src/renderer/components/messages/message-actions.tsx@@ -0,0 +1,110 @@+import { useConversation } from '@/renderer/contexts/conversation';+import { MessageNode } from '@/shared/types/messages';+import { MessagesSquare, Reply } from 'lucide-react';+import { MessageReactionCreatePopover } from '@/renderer/components/messages/message-reaction-create-popover';+import { useMutation } from '@/renderer/hooks/use-mutation';+import { useWorkspace } from '@/renderer/contexts/workspace';+import { MessageDeleteButton } from '@/renderer/components/messages/message-delete-button';+import { EmojiElement } from '@/renderer/components/emojis/emoji-element';+import { Separator } from '@/renderer/components/ui/separator';+import React from 'react';++const MessageAction = ({ children }: { children: React.ReactNode }) => {+ return (+ <li className="flex h-8 w-7 cursor-pointer items-center justify-center hover:bg-gray-200">+ {children}+ </li>+ );+};++interface MessageActionsProps {+ message: MessageNode;+}++const quickReactions = [+ '01jc5vxx33b9nmy1a6yd71pee6em',+ '01jc5vxx32ez8g5394y7mzt2smem',+ '01jc5vxx374h4gxvgjsjf3kbn7em',+];++export const MessageActions = ({ message }: MessageActionsProps) => {+ const workspace = useWorkspace();+ const conversation = useConversation();+ const { mutate, isPending } = useMutation();++ const canDelete = conversation.canDeleteMessage(message);+ const canReplyInThread = false;++ const handleReactionClick = React.useCallback(+ (reaction: string) => {+ if (isPending) {+ return;+ }++ console.log('reaction', reaction);++ mutate({+ input: {+ type: 'node_reaction_create',+ nodeId: message.id,+ userId: workspace.userId,+ reaction,+ },+ });+ },+ [isPending, mutate, workspace.userId, message.id]+ );++ return (+ <ul className="invisible absolute -top-2 right-1 z-10 flex flex-row items-center bg-gray-100 text-muted-foreground shadow group-hover:visible">+ {quickReactions.map((reaction) => (+ <MessageAction key={reaction}>+ <EmojiElement+ id={reaction}+ className="size-4"+ onClick={() => handleReactionClick(reaction)}+ />+ </MessageAction>+ ))}+ <Separator orientation="vertical" className="h-6 w-[2px] mx-1" />+ {canReplyInThread && (+ <MessageAction>+ <MessagesSquare className="size-4 cursor-pointer" />+ </MessageAction>+ )}+ <MessageAction>+ <MessageReactionCreatePopover+ onReactionClick={(reaction) => {+ if (isPending) {+ return;+ }++ mutate({+ input: {+ type: 'node_reaction_create',+ nodeId: message.id,+ userId: workspace.userId,+ reaction,+ },+ });+ }}+ />+ </MessageAction>+ {conversation.canCreateMessage && (+ <MessageAction>+ <Reply+ className="size-4 cursor-pointer"+ onClick={() => {+ conversation.onReply(message);+ }}+ />+ </MessageAction>+ )}+ {canDelete && (+ <MessageAction>+ <MessageDeleteButton id={message.id} />+ </MessageAction>+ )}+ </ul>+ );+};diff --git a/apps/desktop/src/renderer/components/messages/message-reactions.tsx b/apps/desktop/src/renderer/components/messages/message-reactions.tsxindex 6cf52301..500c681d 100644--- a/apps/desktop/src/renderer/components/messages/message-reactions.tsx+++ b/apps/desktop/src/renderer/components/messages/message-reactions.tsx@@ -1,16 +1,17 @@import { MessageNode } from '@/shared/types/messages';import { EmojiElement } from '@/renderer/components/emojis/emoji-element';import { cn } from '@/shared/lib/utils';+import { useWorkspace } from '@/renderer/contexts/workspace';+import { useMutation } from '@/renderer/hooks/use-mutation';interface MessageReactionsProps {message: MessageNode;- onReactionClick: (reaction: string) => void;}-export const MessageReactions = ({- message,- onReactionClick,-}: MessageReactionsProps) => {+export const MessageReactions = ({ message }: MessageReactionsProps) => {+ const workspace = useWorkspace();+ const { mutate, isPending } = useMutation();+if (message.reactionCounts.length === 0) {return null;}@@ -31,9 +32,39 @@ export const MessageReactions = ({'cursor-pointer text-sm text-muted-foreground hover:text-foreground',hasReacted ? 'bg-blue-100' : 'bg-gray-50')}- onClick={() => onReactionClick(reaction.reaction)}+ onClick={() => {+ if (isPending) {+ return;+ }++ if (+ message.reactionCounts.some(+ (reactionCount) =>+ reactionCount.reaction === reaction.reaction &&+ reactionCount.isReactedTo+ )+ ) {+ mutate({+ input: {+ type: 'node_reaction_delete',+ nodeId: message.id,+ userId: workspace.userId,+ reaction: reaction.reaction,+ },+ });+ } else {+ mutate({+ input: {+ type: 'node_reaction_create',+ nodeId: message.id,+ userId: workspace.userId,+ reaction: reaction.reaction,+ },+ });+ }+ }}>- <EmojiElement id={reaction.reaction} className="h-5 w-5" />+ <EmojiElement id={reaction.reaction} className="size-5" /><span>{reaction.count}</span></div>);diff --git a/apps/desktop/src/renderer/components/messages/message.tsx b/apps/desktop/src/renderer/components/messages/message.tsxindex 82db635c..44b86072 100644--- a/apps/desktop/src/renderer/components/messages/message.tsx+++ b/apps/desktop/src/renderer/components/messages/message.tsx@@ -6,16 +6,12 @@ import {} from '@/renderer/components/ui/tooltip';import { formatDate, timeAgo } from '@/shared/lib/utils';import { InView } from 'react-intersection-observer';-import { MessageReactionCreatePopover } from '@/renderer/components/messages/message-reaction-create-popover';-import { MessageDeleteButton } from '@/renderer/components/messages/message-delete-button';import { NodeRenderer } from '@/renderer/editor/renderers/node';import { MessageNode } from '@/shared/types/messages';import { MessageReactions } from '@/renderer/components/messages/message-reactions';-import { useMutation } from '@/renderer/hooks/use-mutation';import { useWorkspace } from '@/renderer/contexts/workspace';-import { MessagesSquare, Reply } from 'lucide-react';-import { useConversation } from '@/renderer/contexts/conversation';import { useRadar } from '@/renderer/contexts/radar';+import { MessageActions } from '@/renderer/components/messages/message-actions';interface MessageProps {message: MessageNode;@@ -42,13 +38,8 @@ const shouldDisplayUserInfo = (export const Message = ({ message, previousMessage }: MessageProps) => {const workspace = useWorkspace();- const conversation = useConversation();const radar = useRadar();- const { mutate, isPending } = useMutation();-- const canDelete = conversation.canDeleteMessage(message);- const canReplyInThread = false;const displayUserInfo = shouldDisplayUserInfo(message, previousMessage);return (@@ -97,46 +88,7 @@ export const Message = ({ message, previousMessage }: MessageProps) => {}}}>- <ul className="invisible absolute -top-2 right-1 z-10 flex flex-row bg-gray-100 text-muted-foreground shadow group-hover:visible">- {canReplyInThread && (- <li className="flex h-8 w-7 cursor-pointer items-center justify-center hover:bg-gray-200">- <MessagesSquare className="size-4 cursor-pointer" />- </li>- )}- <li className="flex h-8 w-7 cursor-pointer items-center justify-center hover:bg-gray-200">- <MessageReactionCreatePopover- onReactionClick={(reaction) => {- if (isPending) {- return;- }-- mutate({- input: {- type: 'node_reaction_create',- nodeId: message.id,- userId: workspace.userId,- reaction,- },- });- }}- />- </li>- {conversation.canCreateMessage && (- <li className="flex h-8 w-7 cursor-pointer items-center justify-center hover:bg-gray-200">- <Reply- className="size-4 cursor-pointer"- onClick={() => {- conversation.onReply(message);- }}- />- </li>- )}- {canDelete && (- <li className="flex h-8 w-7 cursor-pointer items-center justify-center hover:bg-gray-200">- <MessageDeleteButton id={message.id} />- </li>- )}- </ul>+ <MessageActions message={message} /><div className="text-foreground">{message.content.map((node) => (<NodeRenderer@@ -146,42 +98,7 @@ export const Message = ({ message, previousMessage }: MessageProps) => {/>))}</div>- {message.reactionCounts?.length > 0 && (- <MessageReactions- message={message}- onReactionClick={(reaction) => {- if (isPending) {- return;- }-- if (- message.reactionCounts.some(- (reactionCount) =>- reactionCount.reaction === reaction &&- reactionCount.isReactedTo- )- ) {- mutate({- input: {- type: 'node_reaction_delete',- nodeId: message.id,- userId: workspace.userId,- reaction,- },- });- } else {- mutate({- input: {- type: 'node_reaction_create',- nodeId: message.id,- userId: workspace.userId,- reaction,- },- });- }- }}- />- )}+ <MessageReactions message={message} /></InView></div></div>