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.

2.0 KB
Raw Blame
'use client';

import { DEFAULT_INBOX_TITLE } from '@lobechat/const';
import type { PropsWithChildren } from 'react';
import React, { memo } from 'react';
import { useTranslation } from 'react-i18next';

import AgentAvatar from '@/components/AgentAvatar';
import { DEFAULT_INBOX_AVATAR } from '@/const/meta';
import { useAgentAvatarActivity } from '@/features/AgentAvatarActivity/useAgentAvatarActivity';
import { SkeletonItem } from '@/features/NavPanel/components/SkeletonList';
import { useNavItemMetrics } from '@/features/NavPanel/density';
import { SidebarHeaderSelectTrigger } from '@/features/NavPanel/SidebarHeaderSelect';
import { useAgentStore } from '@/store/agent';
import { agentSelectors, builtinAgentSelectors } from '@/store/agent/selectors';

import SwitchPanel from './SwitchPanel';

const Agent = memo<PropsWithChildren>(() => {
  const { t } = useTranslation(['chat', 'common']);
  const { headerAvatarSize } = useNavItemMetrics();

  const [agentId, isLoading, isInbox, title, avatar, backgroundColor] = useAgentStore((s) => [
    s.activeAgentId,
    agentSelectors.isAgentConfigLoading(s),
    builtinAgentSelectors.isInboxAgent(s),
    agentSelectors.currentAgentDisplayName(s),
    agentSelectors.currentAgentAvatar(s),
    agentSelectors.currentAgentBackgroundColor(s),
  ]);

  const activity = useAgentAvatarActivity(agentId);

  const displayTitle = isInbox
    ? title || DEFAULT_INBOX_TITLE
    : title || t('defaultSession', { ns: 'common' });

  if (isLoading) return <SkeletonItem height={headerAvatarSize + 4} padding={0} />;

  return (
    <SwitchPanel>
      <SidebarHeaderSelectTrigger
        name={displayTitle}
        title={displayTitle}
        avatarNode={
          <AgentAvatar
            {...activity.avatar}
            {...activity.hover}
            agentId={agentId}
            avatar={isInbox ? avatar || DEFAULT_INBOX_AVATAR : avatar}
            background={backgroundColor || undefined}
            name={displayTitle}
            size={headerAvatarSize}
          />
        }
      />
    </SwitchPanel>
  );
});

export default Agent;