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';
|
|
|
|
|
import { MessageReactionPicker } from '@/components/messages/message-reaction-picker';
|
|
|
|
|
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-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-08-27 15:20:16 +02:00
|
|
|
export const Message = ({ message, previousMessage }: MessageProps) => {
|
|
|
|
|
const shouldDisplayUserInfo = () => {
|
|
|
|
|
if (!previousMessage) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2024-08-06 21:45:12 +02:00
|
|
|
|
2024-08-27 15:20:16 +02:00
|
|
|
const previousMessageDate = new Date(previousMessage.createdAt);
|
|
|
|
|
const currentMessageDate = new Date(message.createdAt);
|
2024-08-06 21:45:12 +02:00
|
|
|
|
2024-08-27 15:20:16 +02:00
|
|
|
if (previousMessageDate.getDate() !== currentMessageDate.getDate()) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2024-08-06 21:45:12 +02:00
|
|
|
|
2024-09-10 08:11:29 +02:00
|
|
|
return !previousMessage || previousMessage.author.id !== message.author.id;
|
2024-08-27 15:20:16 +02:00
|
|
|
};
|
2024-08-03 19:14:38 +02:00
|
|
|
|
2024-08-27 15:20:16 +02:00
|
|
|
const canEdit = true;
|
|
|
|
|
const canDelete = true;
|
2024-09-15 14:39:52 +02:00
|
|
|
const canReplyInThread = false;
|
2024-08-03 19:14:38 +02:00
|
|
|
|
2024-09-10 08:11:29 +02:00
|
|
|
if (!message.content || message.content.length === 0) {
|
2024-08-27 15:20:16 +02:00
|
|
|
return null;
|
|
|
|
|
}
|
2024-08-07 15:36:52 +02:00
|
|
|
|
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 ${
|
|
|
|
|
shouldDisplayUserInfo() ? 'mt-2 first:mt-0' : ''
|
|
|
|
|
}`}
|
|
|
|
|
>
|
|
|
|
|
<div className="mr-2 w-10 pt-1">
|
|
|
|
|
{shouldDisplayUserInfo() && (
|
2024-09-10 08:11:29 +02:00
|
|
|
<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">
|
|
|
|
|
{shouldDisplayUserInfo() && (
|
|
|
|
|
<p className="font-medium">
|
2024-09-10 08:11:29 +02:00
|
|
|
{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-08-03 19:14:38 +02:00
|
|
|
<li
|
|
|
|
|
className="flex h-8 w-7 cursor-pointer items-center justify-center hover:bg-gray-200"
|
|
|
|
|
onClick={() => {
|
2024-08-27 15:20:16 +02:00
|
|
|
// workspace.openPanel(data.id);
|
2024-08-03 19:14:38 +02:00
|
|
|
}}
|
|
|
|
|
>
|
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">
|
|
|
|
|
<MessageReactionPicker
|
|
|
|
|
onEmojiSelect={(emoji) => {
|
|
|
|
|
console.log(emoji);
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
</li>
|
|
|
|
|
<li
|
|
|
|
|
className="flex h-8 w-7 cursor-pointer items-center justify-center hover:bg-gray-200"
|
|
|
|
|
onClick={() => {
|
|
|
|
|
// onReply?.(data.id, data.createdBy);
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<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>
|
|
|
|
|
{/*{data.messageReference && <MessageReference />}*/}
|
|
|
|
|
<div className="text-foreground">
|
2024-09-10 08:11:29 +02:00
|
|
|
{message.content.map((node) => (
|
|
|
|
|
<NodeRenderer key={node.id} node={node} keyPrefix={node.id} />
|
|
|
|
|
))}
|
2024-08-27 15:20:16 +02:00
|
|
|
</div>
|
|
|
|
|
{/*<MessageReactions*/}
|
|
|
|
|
{/* message={message}*/}
|
|
|
|
|
{/* onReactionClick={handleEmojiSelect}*/}
|
|
|
|
|
{/*/>*/}
|
|
|
|
|
{/*{canReplyInThread && (*/}
|
|
|
|
|
{/* <MessageThread*/}
|
|
|
|
|
{/* message={data}*/}
|
|
|
|
|
{/* onClick={() => workspace.openPanel(data.id)}*/}
|
|
|
|
|
{/* />*/}
|
|
|
|
|
{/*)}*/}
|
|
|
|
|
</InView>
|
2024-08-03 19:14:38 +02:00
|
|
|
</div>
|
2024-08-27 15:20:16 +02:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|