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.

4.4 KB
Raw Blame
import { ActionIcon, Flexbox, Popover, Text } from '@lobehub/ui';
import { Clock3Icon, PanelRightCloseIcon, PlusIcon } from 'lucide-react';
import { memo, useState } from 'react';
import { useTranslation } from 'react-i18next';

import { conversationSelectors, useConversationStore } from '@/features/Conversation';
import NavHeader from '@/features/NavHeader';
import { useFetchAgentChatTopics } from '@/hooks/useFetchChatTopics';
import { useChatStore } from '@/store/chat';
import { topicSelectors } from '@/store/chat/slices/topic/selectors';
import { useHeaderActionSize } from '@/styles/density';

import { usePageAgentPanelControl, usePageAgentPanelOverride } from '../RightPanel/OverrideContext';
import TopicItem from './TopicSelector/TopicItem';

interface CopilotToolbarProps {
  /** Collapse the hosting panel; when set, the close button calls this instead of the page-agent panel. */
  onCollapse?: () => void;
  onTopicChange?: (topicId: string | null) => void;
  topicId?: string | null;
}

const CopilotToolbar = memo<CopilotToolbarProps>(({ onCollapse, onTopicChange, topicId }) => {
  const headerActionSize = useHeaderActionSize(true);
  const { t } = useTranslation('topic');
  const [topicPopoverOpen, setTopicPopoverOpen] = useState(false);
  const agentId = useConversationStore(conversationSelectors.agentId);

  useFetchAgentChatTopics(agentId);

  const [globalActiveTopicId, switchTopic, topics] = useChatStore((s) => [
    s.activeTopicId,
    s.switchTopic,
    topicSelectors.getTopicsByAgentId(agentId)(s),
  ]);

  const activeTopicId = topicId === undefined ? globalActiveTopicId : topicId;
  const currentTopic = topics?.find((topic) => topic.id === activeTopicId);

  const { toggle: togglePageAgentPanel } = usePageAgentPanelControl();
  const hasOverride = !!usePageAgentPanelOverride();

  const isLoadingTopics = topics === undefined;
  const hideHistory = !isLoadingTopics && topics.length === 0;

  const topicTitle = currentTopic?.title || t('title');

  return (
    <NavHeader
      showTogglePanelButton={false}
      left={
        <Text
          style={{ fontSize: 13, fontWeight: 500, marginLeft: 8 }}
          type={'secondary'}
          ellipsis={{
            tooltipWhenOverflow: true,
          }}
        >
          {topicTitle}
        </Text>
      }
      right={
        <>
          <ActionIcon
            icon={PlusIcon}
            size={headerActionSize}
            title={t('actions.addNewTopic')}
            onClick={() =>
              onTopicChange ? onTopicChange(null) : switchTopic(null, { scope: 'page' })
            }
          />
          {!hideHistory && (
            <Popover
              open={isLoadingTopics ? false : topicPopoverOpen}
              placement="bottomRight"
              trigger="click"
              content={
                <Flexbox
                  gap={4}
                  padding={8}
                  style={{
                    maxHeight: '50vh',
                    overflowY: 'auto',
                    width: '100%',
                  }}
                >
                  {(topics || []).map((topic) => (
                    <TopicItem
                      active={topic.id === activeTopicId}
                      agentId={agentId}
                      fav={topic.favorite}
                      key={topic.id}
                      status={topic.status}
                      topicId={topic.id}
                      topicTitle={topic.title}
                      onClose={() => setTopicPopoverOpen(false)}
                      onTopicChange={(id) => (onTopicChange ? onTopicChange(id) : switchTopic(id))}
                    />
                  ))}
                </Flexbox>
              }
              styles={{
                content: {
                  padding: 0,
                  width: 240,
                },
              }}
              onOpenChange={setTopicPopoverOpen}
            >
              <ActionIcon
                disabled={isLoadingTopics}
                icon={Clock3Icon}
                loading={isLoadingTopics}
                size={headerActionSize}
              />
            </Popover>
          )}
          {(onCollapse || !hasOverride) && (
            <ActionIcon
              icon={PanelRightCloseIcon}
              size={headerActionSize}
              onClick={() => (onCollapse ? onCollapse() : togglePageAgentPanel())}
            />
          )}
        </>
      }
    />
  );
});

CopilotToolbar.displayName = 'CopilotToolbar';

export default CopilotToolbar;