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.

13.0 KB
Raw Blame
import {
  getConnectorMode,
  shouldEnableConnectorOnConnect,
  upsertConnectorEnabled,
} from '@lobechat/types';
import { Checkbox, Flexbox, Icon, Spin, stopPropagation } from '@lobehub/ui';
import { SquareArrowOutUpRight } from 'lucide-react';
import type { ReactNode } from 'react';
import { memo, useCallback, useEffect, useRef, useState } from 'react';
import { useTranslation } from 'react-i18next';

import { usePermission } from '@/hooks/usePermission';
import { useAgentStore } from '@/store/agent';
import { agentSelectors } from '@/store/agent/selectors';
import { useToolStore } from '@/store/tool';
import { lobehubSkillStoreSelectors } from '@/store/tool/selectors';
import { LobehubSkillStatus } from '@/store/tool/slices/lobehubSkillStore/types';

import { SKILL_ICON_GAP } from './constants';

const POLL_INTERVAL_MS = 1000;
const POLL_TIMEOUT_MS = 15_000;

interface LobehubSkillServerItemProps {
  /**
   * Optional agent ID to use instead of currentAgentConfig
   * Used in group profile to specify which member's plugins to toggle
   */
  agentId?: string;
  /**
   * Rendered inside the label so the row lines up with the managed skill rows;
   * passing it through the menu's own icon slot would size and space it
   * differently.
   */
  icon?: ReactNode;
  /**
   * Display label for the provider
   */
  label: string;
  /**
   * Provider ID (e.g., 'linear', 'github')
   */
  provider: string;
}

const LobehubSkillServerItem = memo<LobehubSkillServerItemProps>(
  ({ provider, icon, label, agentId }) => {
    const { t } = useTranslation('setting');
    const [isConnecting, setIsConnecting] = useState(false);
    const [isToggling, setIsToggling] = useState(false);
    const [isWaitingAuth, setIsWaitingAuth] = useState(false);
    const { allowed: canCreate } = usePermission('create_content');
    const { allowed: canEdit } = usePermission('edit_own_content');

    const oauthWindowRef = useRef<Window | null>(null);
    const windowCheckIntervalRef = useRef<ReturnType<typeof setInterval> | null>(null);
    const pollIntervalRef = useRef<ReturnType<typeof setInterval> | null>(null);
    const pollTimeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null);

    const server = useToolStore(lobehubSkillStoreSelectors.getServerByIdentifier(provider));
    const checkStatus = useToolStore((s) => s.checkLobehubSkillStatus);
    const getAuthorizeUrl = useToolStore((s) => s.getLobehubSkillAuthorizeUrl);

    // Get effective agent ID (agentId prop or current active agent)
    const activeAgentId = useAgentStore((s) => s.activeAgentId);
    const effectiveAgentId = agentId || activeAgentId || '';

    const cleanup = useCallback(() => {
      if (windowCheckIntervalRef.current) {
        clearInterval(windowCheckIntervalRef.current);
        windowCheckIntervalRef.current = null;
      }
      if (pollIntervalRef.current) {
        clearInterval(pollIntervalRef.current);
        pollIntervalRef.current = null;
      }
      if (pollTimeoutRef.current) {
        clearTimeout(pollTimeoutRef.current);
        pollTimeoutRef.current = null;
      }
      oauthWindowRef.current = null;
      setIsWaitingAuth(false);
    }, []);

    useEffect(() => {
      return () => {
        cleanup();
      };
    }, [cleanup]);

    useEffect(() => {
      if (server?.status === LobehubSkillStatus.CONNECTED && isWaitingAuth) {
        cleanup();
      }
    }, [server?.status, isWaitingAuth, cleanup]);

    const startFallbackPolling = useCallback(() => {
      if (pollIntervalRef.current) return;

      pollIntervalRef.current = setInterval(async () => {
        try {
          await checkStatus(provider);
        } catch (error) {
          console.error('[LobehubSkill] Failed to check status:', error);
        }
      }, POLL_INTERVAL_MS);

      pollTimeoutRef.current = setTimeout(() => {
        if (pollIntervalRef.current) {
          clearInterval(pollIntervalRef.current);
          pollIntervalRef.current = null;
        }
        setIsWaitingAuth(false);
      }, POLL_TIMEOUT_MS);
    }, [checkStatus, provider]);

    const startWindowMonitor = useCallback(
      (oauthWindow: Window) => {
        windowCheckIntervalRef.current = setInterval(() => {
          try {
            if (oauthWindow.closed) {
              if (windowCheckIntervalRef.current) {
                clearInterval(windowCheckIntervalRef.current);
                windowCheckIntervalRef.current = null;
              }
              oauthWindowRef.current = null;
              checkStatus(provider);
            }
          } catch {
            console.info(
              '[LobehubSkill] COOP blocked window.closed access, falling back to polling',
            );
            if (windowCheckIntervalRef.current) {
              clearInterval(windowCheckIntervalRef.current);
              windowCheckIntervalRef.current = null;
            }
            startFallbackPolling();
          }
        }, 500);
      },
      [checkStatus, provider, startFallbackPolling],
    );

    const openOAuthWindow = useCallback(
      (authorizeUrl: string) => {
        cleanup();
        setIsWaitingAuth(true);

        const oauthWindow = window.open(authorizeUrl, '_blank', 'width=600,height=700');
        if (oauthWindow) {
          oauthWindowRef.current = oauthWindow;
          startWindowMonitor(oauthWindow);
        } else {
          startFallbackPolling();
        }
      },
      [cleanup, startWindowMonitor, startFallbackPolling],
    );

    const pluginId = server ? server.identifier : '';
    const config = useAgentStore(agentSelectors.getAgentConfigById(effectiveAgentId));
    const plugins = config?.plugins;
    const checked = getConnectorMode(plugins, pluginId, config?.chatConfig) === 'auto';
    const updateAgentConfigById = useAgentStore((s) => s.updateAgentConfigById);

    // Toggle plugin for the effective agent
    const setPluginEnabled = useCallback(
      async (identifier: string, enabled: boolean) => {
        if (!effectiveAgentId) return;
        const current = agentSelectors.getAgentConfigById(effectiveAgentId)(
          useAgentStore.getState(),
        );
        await updateAgentConfigById(effectiveAgentId, {
          plugins: upsertConnectorEnabled(current?.plugins, identifier, enabled),
        });
      },
      [effectiveAgentId, updateAgentConfigById],
    );

    // Listen for OAuth success message from popup window
    useEffect(() => {
      const handleMessage = async (event: MessageEvent) => {
        // Verify origin for security
        if (event.origin !== window.location.origin) return;

        if (
          event.data?.type === 'LOBEHUB_SKILL_AUTH_SUCCESS' &&
          event.data?.provider === provider
        ) {
          console.info('[LobehubSkill] OAuth success message received for provider:', provider);

          // Cleanup polling/window monitoring
          cleanup();

          // Refresh status to get the connected state
          await checkStatus(provider);

          // Auto-enable the plugin after successful OAuth
          // Need to get the latest server state after checkStatus
          const latestServer = useToolStore
            .getState()
            .lobehubSkillServers?.find((s) => s.identifier === provider);
          if (latestServer?.status === LobehubSkillStatus.CONNECTED) {
            const newPluginId = latestServer.identifier;
            const currentAgentConfig = agentSelectors.getAgentConfigById(effectiveAgentId)(
              useAgentStore.getState(),
            );
            if (
              canEdit &&
              shouldEnableConnectorOnConnect(
                currentAgentConfig?.plugins,
                newPluginId,
                currentAgentConfig?.chatConfig,
              )
            ) {
              await setPluginEnabled(newPluginId, true);
            }
          }
        }
      };

      window.addEventListener('message', handleMessage);
      return () => window.removeEventListener('message', handleMessage);
    }, [canEdit, provider, cleanup, checkStatus, setPluginEnabled, effectiveAgentId]);

    const handleConnect = async () => {
      if (!canCreate || !canEdit) return;
      // Only block reconnection when already connected
      if (server?.isConnected) return;

      setIsConnecting(true);
      try {
        // Use /oauth/callback/success as redirect URI with provider param for auto-enable
        // Skip redirectUri on desktop (app:// protocol) since the system browser can't navigate to it
        const redirectUri = window.location.protocol.startsWith('http')
          ? `${window.location.origin}/oauth/callback/success?provider=${encodeURIComponent(provider)}`
          : undefined;
        const { authorizeUrl } = await getAuthorizeUrl(provider, { redirectUri });
        openOAuthWindow(authorizeUrl);
      } catch (error) {
        console.error('[LobehubSkill] Failed to get authorize URL:', error);
      } finally {
        setIsConnecting(false);
      }
    };

    const handleToggle = async () => {
      if (!canEdit || !server) return;
      setIsToggling(true);
      await setPluginEnabled(pluginId, !checked);
      setIsToggling(false);
    };

    const renderRightControl = () => {
      if (isConnecting) {
        return (
          <Flexbox horizontal align="center" gap={4} onClick={stopPropagation}>
            <Spin size="small" />
          </Flexbox>
        );
      }

      if (!server) {
        return (
          <Flexbox
            horizontal
            align="center"
            gap={4}
            style={{ cursor: canCreate && canEdit ? 'pointer' : 'not-allowed', opacity: 0.65 }}
            onClick={(e) => {
              e.stopPropagation();
              if (!canCreate || !canEdit) return;
              handleConnect();
            }}
          >
            {t('tools.lobehubSkill.connect', { defaultValue: 'Connect' })}
            <Icon icon={SquareArrowOutUpRight} size="small" />
          </Flexbox>
        );
      }

      switch (server.status) {
        case LobehubSkillStatus.CONNECTED: {
          if (isToggling) {
            return <Spin size="small" />;
          }
          return (
            <Checkbox
              checked={checked}
              disabled={!canEdit}
              onClick={(e) => {
                e.stopPropagation();
                if (!canEdit) return;
                handleToggle();
              }}
            />
          );
        }
        case LobehubSkillStatus.CONNECTING: {
          if (isWaitingAuth) {
            return (
              <Flexbox horizontal align="center" gap={4} onClick={stopPropagation}>
                <Spin size="small" />
              </Flexbox>
            );
          }
          return (
            <Flexbox
              horizontal
              align="center"
              gap={4}
              style={{ cursor: canEdit ? 'pointer' : 'not-allowed', opacity: 0.65 }}
              onClick={async (e) => {
                e.stopPropagation();
                if (!canEdit) return;
                try {
                  const redirectUri = window.location.protocol.startsWith('http')
                    ? `${window.location.origin}/oauth/callback/success?provider=${encodeURIComponent(provider)}`
                    : undefined;
                  const { authorizeUrl } = await getAuthorizeUrl(provider, { redirectUri });
                  openOAuthWindow(authorizeUrl);
                } catch (error) {
                  console.error('[LobehubSkill] Failed to get authorize URL:', error);
                }
              }}
            >
              {t('tools.lobehubSkill.authorize', { defaultValue: 'Authorize' })}
              <Icon icon={SquareArrowOutUpRight} size="small" />
            </Flexbox>
          );
        }
        case LobehubSkillStatus.NOT_CONNECTED: {
          return (
            <Flexbox
              horizontal
              align="center"
              gap={4}
              style={{ cursor: canCreate && canEdit ? 'pointer' : 'not-allowed', opacity: 0.65 }}
              onClick={(e) => {
                e.stopPropagation();
                if (!canCreate || !canEdit) return;
                handleConnect();
              }}
            >
              {t('tools.lobehubSkill.connect', { defaultValue: 'Connect' })}
              <Icon icon={SquareArrowOutUpRight} size="small" />
            </Flexbox>
          );
        }
        case LobehubSkillStatus.ERROR: {
          return (
            <span style={{ color: 'red', fontSize: 12 }}>
              {t('tools.lobehubSkill.error', { defaultValue: 'Error' })}
            </span>
          );
        }
        default: {
          return null;
        }
      }
    };

    return (
      <Flexbox
        horizontal
        align={'center'}
        gap={24}
        justify={'space-between'}
        onClick={(e) => {
          e.stopPropagation();
          if (canEdit && server?.status === LobehubSkillStatus.CONNECTED) {
            handleToggle();
          }
        }}
      >
        <Flexbox horizontal align={'center'} gap={SKILL_ICON_GAP}>
          {icon}
          {label}
        </Flexbox>
        {renderRightControl()}
      </Flexbox>
    );
  },
);

export default LobehubSkillServerItem;