|
|
@ -1,7 +1,7 @@
|
|
|
|
import { ChatMessage, useUsernames } from 'tailchat-shared';
|
|
|
|
import { ChatMessage, removeReaction, useUsernames } from 'tailchat-shared';
|
|
|
|
import _groupBy from 'lodash/groupBy';
|
|
|
|
import _groupBy from 'lodash/groupBy';
|
|
|
|
import _uniqBy from 'lodash/uniqBy';
|
|
|
|
import _uniqBy from 'lodash/uniqBy';
|
|
|
|
import { useMemo } from 'react';
|
|
|
|
import { useCallback, useMemo } from 'react';
|
|
|
|
import { Emoji } from '@/components/Emoji';
|
|
|
|
import { Emoji } from '@/components/Emoji';
|
|
|
|
import React from 'react';
|
|
|
|
import React from 'react';
|
|
|
|
import { Tooltip } from 'antd';
|
|
|
|
import { Tooltip } from 'antd';
|
|
|
@ -15,14 +15,16 @@ interface GroupedReaction {
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 消息反应的用户名
|
|
|
|
* 消息反应的用户名
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
const ReactionItem: React.FC<{ reaction: GroupedReaction }> = React.memo(
|
|
|
|
const ReactionItem: React.FC<{
|
|
|
|
(props) => {
|
|
|
|
reaction: GroupedReaction;
|
|
|
|
const { reaction } = props;
|
|
|
|
onClick: () => void;
|
|
|
|
|
|
|
|
}> = React.memo((props) => {
|
|
|
|
|
|
|
|
const { reaction, onClick } = props;
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
return (
|
|
|
|
<div className="py-0.5 px-1 bg-black bg-opacity-20 rounded flex">
|
|
|
|
<div className="py-0.5 px-1 bg-black bg-opacity-20 hover:bg-opacity-40 rounded flex cursor-pointer">
|
|
|
|
<Tooltip title={useUsernames(reaction.users)}>
|
|
|
|
<Tooltip title={useUsernames(reaction.users)}>
|
|
|
|
<div>
|
|
|
|
<div onClick={onClick}>
|
|
|
|
<Emoji emoji={reaction.name} />
|
|
|
|
<Emoji emoji={reaction.name} />
|
|
|
|
|
|
|
|
|
|
|
|
{reaction.length > 1 && <span>{reaction.length}</span>}
|
|
|
|
{reaction.length > 1 && <span>{reaction.length}</span>}
|
|
|
@ -30,14 +32,14 @@ const ReactionItem: React.FC<{ reaction: GroupedReaction }> = React.memo(
|
|
|
|
</Tooltip>
|
|
|
|
</Tooltip>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
);
|
|
|
|
|
|
|
|
ReactionItem.displayName = 'ReactionItem';
|
|
|
|
ReactionItem.displayName = 'ReactionItem';
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 消息反应表情渲染
|
|
|
|
* 消息反应表情渲染
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
export function useMessageReactions(payload: ChatMessage) {
|
|
|
|
export function useMessageReactions(payload: ChatMessage) {
|
|
|
|
|
|
|
|
const messageId = payload._id;
|
|
|
|
const reactions = payload.reactions ?? [];
|
|
|
|
const reactions = payload.reactions ?? [];
|
|
|
|
|
|
|
|
|
|
|
|
const groupedReactions: GroupedReaction[] = useMemo(() => {
|
|
|
|
const groupedReactions: GroupedReaction[] = useMemo(() => {
|
|
|
@ -53,10 +55,21 @@ export function useMessageReactions(payload: ChatMessage) {
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}, [reactions]);
|
|
|
|
}, [reactions]);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const handleClick = useCallback(
|
|
|
|
|
|
|
|
(reactionName: string) => {
|
|
|
|
|
|
|
|
removeReaction(messageId, reactionName);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
[messageId]
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
return (
|
|
|
|
<div className="flex chat-message-reactions gap-1 py-0.5">
|
|
|
|
<div className="flex chat-message-reactions gap-1 py-0.5">
|
|
|
|
{groupedReactions.map((reaction) => (
|
|
|
|
{groupedReactions.map((reaction) => (
|
|
|
|
<ReactionItem key={reaction.name} reaction={reaction} />
|
|
|
|
<ReactionItem
|
|
|
|
|
|
|
|
key={reaction.name}
|
|
|
|
|
|
|
|
reaction={reaction}
|
|
|
|
|
|
|
|
onClick={() => handleClick(reaction.name)}
|
|
|
|
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
))}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
);
|
|
|
|