Skip to content

Self-improving AI Employees for everyone

4.1 KB
Raw Blame
/**
 * PanelRenderer — lazy panel dispatch.
 *
 * Phase 5 of the credentials-scaling plan: each panel is loaded on
 * demand via `React.lazy` + Vite dynamic `import()`. First selection of
 * a given panel kind pays a one-time ~10–50 ms chunk-fetch cost; every
 * subsequent selection is instant. Reduces the credentials modal's
 * initial JS payload and lets Vite emit one chunk per panel kind.
 *
 * Panels are cached at module scope so switching providers of the same
 * kind does not trip Suspense on every click.
 */

import React, { Suspense, useMemo } from 'react';
import { Loader2, Info, AlertTriangle } from 'lucide-react';
import type { ProviderConfig, PanelKind } from './types';

// Vite will emit one chunk per panel under dist/assets/ApiKeyPanel-*.js,
// OAuthPanel-*.js, etc. Keep the import paths inside the arrow fns so
// code splitting is preserved.
const PANEL_LOADERS: Record<PanelKind, () => Promise<{ default: React.ComponentType<CredentialPanelProps> }>> = {
  apiKey: () => import('./panels/ApiKeyPanel'),
  oauth: () => import('./panels/OAuthPanel'),
  qrPairing: () => import('./panels/QrPairingPanel'),
  email: () => import('./panels/EmailPanel'),
  browserProfiles: () => import('./panels/BrowserProfilesPanel'),
  mcp: () => import('./panels/McpConnectorPanel'),
  mcpAdd: () => import('./panels/McpAddPanel'),
};

// Memoize the lazy wrappers at module scope so switching providers of
// the same kind doesn't re-trigger Suspense. React.lazy memoizes too
// internally, but declaring them once makes the intent explicit and
// avoids any HMR edge cases.
const LAZY_PANELS: Record<PanelKind, React.LazyExoticComponent<React.ComponentType<CredentialPanelProps>>> = {
  apiKey: React.lazy(PANEL_LOADERS.apiKey),
  oauth: React.lazy(PANEL_LOADERS.oauth),
  qrPairing: React.lazy(PANEL_LOADERS.qrPairing),
  email: React.lazy(PANEL_LOADERS.email),
  browserProfiles: React.lazy(PANEL_LOADERS.browserProfiles),
  mcp: React.lazy(PANEL_LOADERS.mcp),
  mcpAdd: React.lazy(PANEL_LOADERS.mcpAdd),
};

export interface CredentialPanelProps {
  config: ProviderConfig;
  visible: boolean;
  /** Set by the shared host from shell mode; connection inputs stay available in both modes. */
  showTechnicalSections?: boolean;
  /** Leave this provider's page: the panel removed the provider itself. */
  onLeave?: () => void;
  /** Show another provider's page: one the panel just added. */
  onOpen?: (providerId: string) => void;
}

interface Props extends Omit<CredentialPanelProps, 'config'> {
  config: ProviderConfig | null;
}

const PanelFallback: React.FC = () => (
  <div className="flex h-full min-h-[240px] w-full items-center justify-center">
    <Loader2 className="h-6 w-6 animate-spin text-muted-foreground" />
  </div>
);

const EmptyState: React.FC<{ icon: React.ReactNode; message: string }> = ({ icon, message }) => (
  <div className="flex h-full w-full flex-col items-center justify-center gap-3 p-8 text-center">
    {icon}
    <p className="text-sm text-muted-foreground">{message}</p>
  </div>
);

const PanelRenderer: React.FC<Props> = ({ config, visible, showTechnicalSections = false, onLeave, onOpen }) => {
  const Lazy = useMemo(() => {
    if (!config) return null;
    return LAZY_PANELS[config.kind] ?? null;
  }, [config]);

  if (!config) {
    return (
      <EmptyState
        icon={<Info className="h-8 w-8 text-muted-foreground" />}
        message="Select a credential to configure"
      />
    );
  }
  if (!Lazy) {
    return (
      <EmptyState
        icon={<AlertTriangle className="h-8 w-8 text-warning" />}
        message={`Unknown credential kind: ${config.kind}`}
      />
    );
  }

  if (config.unsupportedReason) {
    return <EmptyState icon={<AlertTriangle className="h-8 w-8 text-warning" />} message={config.unsupportedReason} />;
  }

  return (
    <Suspense fallback={<PanelFallback />}>
      {/* Provider identity resets transient errors/reveal state. Mode changes
          keep the same panel mounted and preserve the active connection draft. */}
      <Lazy key={config.id} config={config} visible={visible} showTechnicalSections={showTechnicalSections} onLeave={onLeave} onOpen={onOpen} />
    </Suspense>
  );
};

export default PanelRenderer;