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.8 KB
Raw Blame
'use client';

import {
  ActionIcon,
  Block,
  Center,
  createStaticStyles,
  cssVar,
  cx,
  DropdownMenu,
  Flexbox,
  Icon,
  Text,
  Tooltip,
} from '@lobehub/ui';
import { ChevronDownIcon, PlusIcon } from 'lucide-react';
import { memo, useMemo } from 'react';
import { useTranslation } from 'react-i18next';

import { useNavItemMetrics } from '@/features/NavPanel/density';
import { usePermission } from '@/hooks/usePermission';
import { SessionDefaultGroup } from '@/types/session';

import { useCreateMenuItems } from '../../hooks';

const ACTION_CLASS_NAME = 'create-agent-actions';

const styles = createStaticStyles(({ css }) => ({
  container: css`
    .${ACTION_CLASS_NAME} {
      width: 0;
      margin-inline-end: 2px;
      opacity: 0;
      transition: opacity 0.2s ${cssVar.motionEaseOut};

      &:has([data-popup-open]) {
        width: unset;
        opacity: 1;
      }
    }

    &:hover {
      .${ACTION_CLASS_NAME} {
        width: unset;
        opacity: 1;
      }
    }
  `,
}));

interface CreateAgentButtonProps {
  className?: string;
  groupId?: string;
  visibility?: 'private' | 'public';
}

const CreateAgentButton = memo<CreateAgentButtonProps>(({ groupId, className, visibility }) => {
  const metrics = useNavItemMetrics();
  const { t } = useTranslation('chat');
  const { allowed: canCreate, reason } = usePermission('create_content');
  const {
    createAgentListMenuItem,
    createAgentMenuItem,
    createConnectAgentMenuItem,
    createGroupChatMenuItem,
    createMarketAgentMenuItem,
    openCreateAgentModal,
  } = useCreateMenuItems();

  const isCustomGroup = Boolean(groupId) && groupId !== SessionDefaultGroup.Default;
  // Always carry visibility so agents created inside a private session group
  // land in the private bucket (otherwise they default to public and end up
  // orphaned β€” invisible in both lists). groupId is only attached for custom
  // groups so the default list keeps creating top-level agents.
  const menuOptions = useMemo(
    () =>
      isCustomGroup || visibility
        ? { ...(isCustomGroup ? { groupId } : {}), ...(visibility ? { visibility } : {}) }
        : undefined,
    [groupId, isCustomGroup, visibility],
  );

  const dropdownItems = useMemo(() => {
    const connectItem = createConnectAgentMenuItem(menuOptions);
    // Discovery entries stay available for the private bucket too β€” they only
    // navigate (list / market), so the bucket merely decides which tab the
    // agent-list page opens on.
    const showDiscoveryItems = !isCustomGroup;
    return [
      createAgentMenuItem(menuOptions),
      createGroupChatMenuItem(menuOptions),
      ...(connectItem ? [{ type: 'divider' as const }, connectItem] : []),
      ...(showDiscoveryItems
        ? [
            { type: 'divider' as const },
            createAgentListMenuItem(visibility ? { visibility } : undefined),
            createMarketAgentMenuItem(),
          ]
        : []),
    ];
  }, [
    createAgentListMenuItem,
    createAgentMenuItem,
    createConnectAgentMenuItem,
    createGroupChatMenuItem,
    createMarketAgentMenuItem,
    isCustomGroup,
    menuOptions,
    visibility,
  ]);

  const handleClick = () => {
    openCreateAgentModal(menuOptions);
  };

  const content = (
    <Block
      horizontal
      align={'center'}
      className={cx(styles.container, className)}
      clickable={canCreate}
      gap={metrics.gap}
      height={metrics.rowHeight}
      paddingInline={metrics.paddingInline}
      variant={'borderless'}
      style={
        canCreate
          ? { height: metrics.rowHeight }
          : { cursor: 'not-allowed', height: metrics.rowHeight, opacity: 0.5 }
      }
      onClick={handleClick}
    >
      <Center flex={'none'} height={metrics.iconBox} width={metrics.iconBox}>
        <Icon icon={PlusIcon} size={'small'} />
      </Center>
      <Text style={{ flex: 1 }} type={'secondary'}>
        {t('addAgent')}
      </Text>
      {canCreate && (
        <Flexbox
          horizontal
          align={'center'}
          className={ACTION_CLASS_NAME}
          flex={'none'}
          justify={'flex-end'}
          onClick={(e) => {
            e.preventDefault();
            e.stopPropagation();
          }}
        >
          <DropdownMenu items={dropdownItems}>
            <ActionIcon
              color={cssVar.colorTextQuaternary}
              icon={ChevronDownIcon}
              size={'small'}
              style={{ flex: 'none' }}
            />
          </DropdownMenu>
        </Flexbox>
      )}
    </Block>
  );

  // Wrap in Tooltip when the viewer/member lacks `create_content`. The
  // dropdown is hidden in the disabled state (it'd let users bypass the
  // gate); the main click target is intercepted in `handleClick`.
  return canCreate ? content : <Tooltip title={reason}>{content}</Tooltip>;
});

export default CreateAgentButton;