import { ChatMessage, useUsernames } from 'tailchat-shared';
import _groupBy from 'lodash/groupBy';
import _uniqBy from 'lodash/uniqBy';
import { useMemo } from 'react';
import { Emoji } from '@/components/Emoji';
import React from 'react';
import { Tooltip } from 'antd';
interface GroupedReaction {
name: string;
length: number;
users: string[];
}
/**
* 消息反应的用户名
*/
const ReactionItem: React.FC<{ reaction: GroupedReaction }> = React.memo(
(props) => {
const { reaction } = props;
return (
{reaction.length > 1 && {reaction.length}}
);
}
);
ReactionItem.displayName = 'ReactionItem';
/**
* 消息反应表情渲染
*/
export function useMessageReactions(payload: ChatMessage) {
const reactions = payload.reactions ?? [];
const groupedReactions: GroupedReaction[] = useMemo(() => {
const groups = _groupBy(reactions, 'name');
return Object.keys(groups).map((name) => {
const reactions = _uniqBy(groups[name], 'author');
return {
name,
length: reactions.length,
users: reactions.map((r) => r.author),
};
});
}, [reactions]);
return (
{groupedReactions.map((reaction) => (
))}
);
}