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.

3.4 KB
Raw Blame
'use client';

import {
  ActionIcon,
  Block,
  confirmModal,
  cssVar,
  DropdownMenu,
  Flexbox,
  Icon,
  stopPropagation,
} from '@lobehub/ui';
import { Loader2, MoreVerticalIcon, Plus, Unplug } from 'lucide-react';
import React, { memo } from 'react';
import { useTranslation } from 'react-i18next';

import { itemStyles } from '@/features/SkillStore/SkillList/style';
import { usePermission } from '@/hooks/usePermission';

import { useAgentComposioConnect } from './useAgentComposioConnect';

interface ItemProps {
  agentId: string;
  appSlug: string;
  description?: string;
  icon: string | React.ComponentType;
  identifier: string;
  label: string;
}

/**
 * Agent-scoped Composio connector card — mirrors SkillStore's LobeHub Item but
 * its connected state + connect/disconnect are bound to the agent, not the user.
 */
const Item = memo<ItemProps>(({ agentId, appSlug, description, icon, identifier, label }) => {
  const { t } = useTranslation('setting');
  const styles = itemStyles;
  const { allowed: canCreate } = usePermission('create_content');
  const { allowed: canEdit } = usePermission('edit_own_content');

  const { handleConnect, handleDisconnect, isConnected, isConnecting } = useAgentComposioConnect({
    agentId,
    appSlug,
    identifier,
    label,
  });

  const localizedDescription = t(`tools.composio.servers.${identifier}.description` as any, {
    defaultValue: description ?? '',
  });

  const confirmDisconnect = () => {
    if (!canEdit) return;
    confirmModal({
      cancelText: t('cancel', { ns: 'common' }),
      content: t('settingAgent.tools.connection.removeConfirm.desc'),
      okButtonProps: { danger: true },
      okText: t('tools.lobehubSkill.disconnect'),
      onOk: handleDisconnect,
    });
  };

  const renderIcon = () => {
    if (typeof icon === 'string') return <img alt={label} height={40} src={icon} width={40} />;
    return <Icon fill={cssVar.colorText} icon={icon as any} size={40} />;
  };

  const renderAction = () => {
    if (isConnecting) return <ActionIcon loading icon={Loader2} />;
    if (isConnected) {
      return (
        <DropdownMenu
          nativeButton={false}
          placement="bottomRight"
          items={[
            {
              danger: true,
              disabled: !canEdit,
              icon: <Icon icon={Unplug} />,
              key: 'disconnect',
              label: t('tools.lobehubSkill.disconnect'),
              onClick: confirmDisconnect,
            },
          ]}
        >
          <ActionIcon disabled={!canEdit} icon={MoreVerticalIcon} />
        </DropdownMenu>
      );
    }
    return (
      <ActionIcon
        disabled={!canCreate || !canEdit}
        icon={Plus}
        title={t('tools.lobehubSkill.connect')}
        onClick={() => {
          if (!canCreate || !canEdit) return;
          handleConnect();
        }}
      />
    );
  };

  return (
    <Block
      horizontal
      align={'center'}
      className={styles.container}
      gap={12}
      paddingBlock={12}
      paddingInline={12}
      variant={'outlined'}
    >
      {renderIcon()}
      <Flexbox flex={1} gap={4} style={{ minWidth: 0, overflow: 'hidden' }}>
        <span className={styles.title}>{label}</span>
        {localizedDescription && <span className={styles.description}>{localizedDescription}</span>}
      </Flexbox>
      <div onClick={stopPropagation}>{renderAction()}</div>
    </Block>
  );
});

Item.displayName = 'AgentSkillStoreItem';

export default Item;