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

import { Avatar, Button, cssVar, Flexbox, Icon, Text, Tooltip, useModalContext } from '@lobehub/ui';
import { SquareArrowOutUpRight } from 'lucide-react';
import { memo, useEffect, useRef } from 'react';
import { useTranslation } from 'react-i18next';

import { useSkillConnect } from '@/features/SkillStore/SkillList/LobeHub/useSkillConnect';
import { usePermission } from '@/hooks/usePermission';

import { useDetailContext } from './DetailContext';
import { ICON_SIZE, styles } from './styles';

// Check if a string is likely an emoji or short text (not a URL or icon component)
const isEmojiOrText = (str: string): boolean => {
  // If it starts with http/https or contains common image extensions, it's a URL
  if (/^https?:\/\//.test(str) || /\.(?:png|jpg|jpeg|gif|svg|webp)$/i.test(str)) {
    return false;
  }
  // Short strings (<=4 chars) are likely emojis or short text
  return str.length <= 4;
};

interface HeaderProps {
  type: 'builtin' | 'composio' | 'lobehub';
}

const Header = memo<HeaderProps>(({ type }) => {
  const { t } = useTranslation(['setting']);
  const { close } = useModalContext();
  const { allowed: canCreate, reason: createReason } = usePermission('create_content');
  const { allowed: canEdit, reason: editReason } = usePermission('edit_own_content');
  const { identifier, serverName, icon, label, localizedDescription, isConnected } =
    useDetailContext();

  // Only use skill connect hook for non-builtin types
  const isBuiltin = type === 'builtin';
  const {
    handleConnect,
    isConnecting,
    isConnected: hookIsConnected,
  } = useSkillConnect({
    identifier,
    serverName,
    type: isBuiltin ? 'lobehub' : type, // Use lobehub as fallback for builtin
  });

  const hasTriggeredConnectRef = useRef(false);

  useEffect(() => {
    if (!isBuiltin && hasTriggeredConnectRef.current && hookIsConnected) {
      close();
    }
  }, [hookIsConnected, close, isBuiltin]);

  const handleConnectWithTracking = async () => {
    if (!canCreate || !canEdit) return;

    hasTriggeredConnectRef.current = true;
    await handleConnect();
  };

  const renderIcon = () => {
    if (typeof icon === 'string') {
      // Use Avatar for emoji/text avatars, img for URLs
      if (isEmojiOrText(icon)) {
        return <Avatar avatar={icon} size={ICON_SIZE} />;
      }
      return (
        <img
          alt={label}
          src={icon}
          style={{ maxHeight: ICON_SIZE, maxWidth: ICON_SIZE, objectFit: 'contain' }}
        />
      );
    }
    return <Icon fill={cssVar.colorText} icon={icon as any} size={ICON_SIZE} />;
  };

  const renderConnectButton = () => {
    if (isBuiltin || isConnected) return null;

    if (isConnecting) {
      return (
        <Button disabled loading type="default">
          {t('tools.composio.connect', { defaultValue: 'Connect' })}
        </Button>
      );
    }

    return (
      <Tooltip title={!canCreate ? createReason : editReason}>
        <Button
          disabled={!canCreate || !canEdit}
          icon={<Icon icon={SquareArrowOutUpRight} />}
          type="primary"
          onClick={handleConnectWithTracking}
        >
          {t('tools.composio.connect', { defaultValue: 'Connect' })}
        </Button>
      </Tooltip>
    );
  };

  return (
    <Flexbox
      horizontal
      align="center"
      className={styles.header}
      justify="space-between"
      style={{ flexWrap: 'nowrap' }}
    >
      <Flexbox horizontal align="center" gap={16}>
        <div className={styles.icon}>{renderIcon()}</div>
        <Flexbox gap={4}>
          <span className={styles.title}>{label}</span>
          <Text style={{ fontSize: 14 }} type="secondary">
            {localizedDescription}
          </Text>
        </Flexbox>
      </Flexbox>
      {renderConnectButton()}
    </Flexbox>
  );
});

export default Header;