Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

1.8 KB
Raw Blame
import { agentDisplayName } from '@lobechat/types';
import { type ItemType } from '@lobehub/ui';
import { AtSign } from 'lucide-react';
import { memo, useMemo } from 'react';
import { useTranslation } from 'react-i18next';

import Avatar from '@/components/Avatar';
import { useMentionStore } from '@/store/mention';
import { useSessionStore } from '@/store/session';
import { sessionSelectors } from '@/store/session/selectors';
import { type LobeGroupSession } from '@/types/session';

import { ChatInputAction } from '../components/ChatInputAction';

const Mention = memo(() => {
  const { t } = useTranslation('chat');
  const addMentionedUser = useMentionStore((s: any) => s.addMentionedUser);

  const handleMemberSelect = (agentId: string) => {
    addMentionedUser(agentId);
  };

  const useMentionItems = () => {
    const currentSession = useSessionStore(sessionSelectors.currentSession) as LobeGroupSession;

    const items: ItemType[] = useMemo(() => {
      const memberItems: ItemType[] = [];

      currentSession.members?.forEach((agent) => {
        memberItems.push({
          icon: (
            <Avatar
              avatar={agent.avatar}
              background={agent.backgroundColor ?? undefined}
              shape={'square'}
              size={24}
            />
          ),
          key: agent.id,
          label: agentDisplayName(agent, agent.id),
          onClick: () => handleMemberSelect(agent.id),
        });
      });

      return memberItems;
    }, [currentSession]);

    return items;
  };

  const items = useMentionItems();

  // Only show for group sessions
  if (!items.length) return null;

  return (
    <ChatInputAction
      icon={AtSign}
      title={t('mention.title')}
      dropdown={{
        maxHeight: 320,
        menu: { items },
        minWidth: 200,
      }}
    />
  );
});

export default Mention;