Files
colanode/desktop/src/components/messages/message.tsx

152 lines
5.1 KiB
TypeScript
Raw Normal View History

2024-08-03 19:14:38 +02:00
import React from 'react';
import { Avatar } from '@/components/ui/avatar';
import {
Tooltip,
TooltipContent,
TooltipTrigger,
} from '@/components/ui/tooltip';
import { formatDate, timeAgo } from '@/lib/utils';
import { InView } from 'react-intersection-observer';
2024-09-15 16:29:47 +02:00
import { MessageReactionCreatePopover } from '@/components/messages/message-reaction-create-popover';
2024-08-03 19:14:38 +02:00
import { Icon } from '@/components/ui/icon';
import { MessageDeleteButton } from '@/components/messages/message-delete-button';
import { NodeRenderer } from '@/editor/renderers/node';
2024-08-22 20:17:36 +02:00
import { MessageNode } from '@/types/messages';
2024-09-15 16:29:47 +02:00
import { MessageReactions } from '@/components/messages/message-reactions';
import { useReactionCreateMutation } from '@/mutations/use-reaction-create-mutation';
import { useReactionDeleteMutation } from '@/mutations/use-reaction-delete-mutation';
2024-08-03 19:14:38 +02:00
interface MessageProps {
2024-08-22 20:17:36 +02:00
message: MessageNode;
previousMessage?: MessageNode;
2024-08-03 19:14:38 +02:00
}
2024-09-15 16:29:47 +02:00
const shouldDisplayUserInfo = (
message: MessageNode,
previousMessage?: MessageNode,
) => {
if (!previousMessage) {
return true;
}
2024-09-15 16:29:47 +02:00
const previousMessageDate = new Date(previousMessage.createdAt);
const currentMessageDate = new Date(message.createdAt);
2024-09-15 16:29:47 +02:00
if (previousMessageDate.getDate() !== currentMessageDate.getDate()) {
return true;
}
2024-09-15 16:29:47 +02:00
return previousMessage.author.id !== message.author.id;
};
2024-08-03 19:14:38 +02:00
2024-09-15 16:29:47 +02:00
export const Message = ({ message, previousMessage }: MessageProps) => {
const { mutate: createReaction, isPending: isCreatingReaction } =
useReactionCreateMutation();
const { mutate: deleteReaction, isPending: isDeletingReaction } =
useReactionDeleteMutation();
2024-08-03 19:14:38 +02:00
if (!message.content || message.content.length === 0) {
2024-08-27 15:20:16 +02:00
return null;
}
2024-09-15 16:29:47 +02:00
const canEdit = true;
const canDelete = true;
const canReplyInThread = false;
const displayUserInfo = shouldDisplayUserInfo(message, previousMessage);
2024-08-27 15:20:16 +02:00
return (
<div
id={`message-${message.id}`}
key={`message-${message.id}`}
className={`group flex flex-row px-1 hover:bg-gray-50 ${
2024-09-15 16:29:47 +02:00
displayUserInfo ? 'mt-2 first:mt-0' : ''
2024-08-27 15:20:16 +02:00
}`}
>
<div className="mr-2 w-10 pt-1">
2024-09-15 16:29:47 +02:00
{displayUserInfo && (
<Avatar
id={message.author.id}
name={message.author.name}
size="medium"
/>
2024-08-27 15:20:16 +02:00
)}
</div>
2024-08-03 19:14:38 +02:00
2024-08-27 15:20:16 +02:00
<div className="relative w-full">
2024-09-15 16:29:47 +02:00
{displayUserInfo && (
2024-08-27 15:20:16 +02:00
<p className="font-medium">
{message.author.name}
2024-08-27 15:20:16 +02:00
<Tooltip>
<TooltipTrigger asChild>
<span className="ml-2 text-xs text-muted-foreground">
{timeAgo(message.createdAt)}
</span>
</TooltipTrigger>
<TooltipContent>
<span className="border border-gray-100 bg-white p-2 text-sm text-muted-foreground shadow-md">
{formatDate(message.createdAt)}
</span>
</TooltipContent>
</Tooltip>
</p>
)}
2024-08-03 19:14:38 +02:00
2024-08-27 15:20:16 +02:00
<InView
rootMargin="50px"
onChange={(inView) => {
// onVisibilityChange?.(data.id, inView);
}}
>
<ul className="invisible absolute -top-2 right-1 z-10 flex flex-row bg-gray-100 text-muted-foreground shadow group-hover:visible">
{canReplyInThread && (
2024-09-15 16:29:47 +02:00
<li className="flex h-8 w-7 cursor-pointer items-center justify-center hover:bg-gray-200">
2024-08-27 15:20:16 +02:00
<Icon name="question-answer-line" className="cursor-pointer" />
</li>
)}
<li className="flex h-8 w-7 cursor-pointer items-center justify-center hover:bg-gray-200">
2024-09-15 16:29:47 +02:00
<MessageReactionCreatePopover
onReactionClick={(reaction) => {
if (isCreatingReaction || isDeletingReaction) {
return;
}
createReaction({ nodeId: message.id, reaction });
2024-08-27 15:20:16 +02:00
}}
/>
</li>
2024-09-15 16:29:47 +02:00
<li className="flex h-8 w-7 cursor-pointer items-center justify-center hover:bg-gray-200">
2024-08-27 15:20:16 +02:00
<Icon name="reply-line" className="cursor-pointer" />
</li>
{canDelete && (
<li className="flex h-8 w-7 cursor-pointer items-center justify-center hover:bg-gray-200">
<MessageDeleteButton id={message.id} />
2024-08-03 19:14:38 +02:00
</li>
2024-08-27 15:20:16 +02:00
)}
</ul>
<div className="text-foreground">
{message.content.map((node) => (
<NodeRenderer key={node.id} node={node} keyPrefix={node.id} />
))}
2024-08-27 15:20:16 +02:00
</div>
2024-09-15 16:29:47 +02:00
{message.reactionCounts.length > 0 && (
<MessageReactions
message={message}
onReactionClick={(reaction) => {
if (isCreatingReaction || isDeletingReaction) {
return;
}
if (message.userReactions.includes(reaction)) {
deleteReaction({ nodeId: message.id, reaction });
} else {
createReaction({ nodeId: message.id, reaction });
}
}}
/>
)}
2024-08-27 15:20:16 +02:00
</InView>
2024-08-03 19:14:38 +02:00
</div>
2024-08-27 15:20:16 +02:00
</div>
);
};