Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

6.2 KB
Raw Blame
import { type ReactNode, useEffect, useRef, useState } from 'react';

import { cn } from '../../lib/cn';
import { useT } from '../../lib/i18n/I18nContext';
import Button from '../ui/Button';

interface UnifiedSkillCardProps {
  icon: ReactNode;
  title: string;
  description: string;
  statusDot?: string;
  statusLabel?: string;
  statusColor?: string;
  ctaLabel: string;
  ctaVariant?: 'primary' | 'sage' | 'amber';
  onCtaClick: () => void;
  testId?: string;
  ctaTestId?: string;
  badge?: ReactNode;
  secondaryActions?: Array<{
    label: string;
    icon: ReactNode;
    onClick: () => void;
    disabled?: boolean;
    testId?: string;
  }>;
  syncProgress?: { active: boolean; percent?: number; message?: string; metricsText?: string };
  syncSummaryText?: string;
  ctaDisabled?: boolean;
}

const CTA_STYLES: Record<string, string> = {
  primary: 'border-primary-200 bg-primary-50 text-primary-700 hover:bg-primary-100',
  sage: 'border-sage-200 bg-sage-50 text-sage-700 hover:bg-sage-100',
  amber: 'border-amber-200 bg-amber-50 text-amber-700 hover:bg-amber-100',
};

function UnifiedSkillCard({
  icon,
  title,
  description,
  statusDot,
  statusLabel,
  statusColor,
  ctaLabel,
  ctaVariant = 'primary',
  onCtaClick,
  testId,
  ctaTestId,
  secondaryActions,
  syncProgress,
  syncSummaryText,
  ctaDisabled,
}: UnifiedSkillCardProps) {
  const { t } = useT();
  const [menuOpen, setMenuOpen] = useState(false);
  const menuRef = useRef<HTMLDivElement>(null);

  useEffect(() => {
    if (!menuOpen) return;
    const handleClick = (e: MouseEvent) => {
      if (menuRef.current && !menuRef.current.contains(e.target as Node)) {
        setMenuOpen(false);
      }
    };
    document.addEventListener('mousedown', handleClick);
    return () => document.removeEventListener('mousedown', handleClick);
  }, [menuOpen]);

  const ctaStyle = CTA_STYLES[ctaVariant] ?? CTA_STYLES.primary;

  return (
    <div
      data-testid={testId}
      className="flex items-center gap-3 rounded-xl border border-line-subtle bg-surface p-3 transition-colors hover:bg-surface-hover">
      <div className="flex h-8 w-8 shrink-0 items-center justify-center text-content-secondary">
        {icon}
      </div>

      <div className="min-w-0 flex-1">
        <div className="flex items-center gap-2">
          <span className="truncate text-sm font-semibold text-content">
            {title}
          </span>
          {statusDot && <div className={`h-1.5 w-1.5 shrink-0 rounded-full ${statusDot}`} />}
          {statusLabel && (
            <span
              className={`shrink-0 text-xs ${statusColor ?? 'text-content-faint'}`}>
              {statusLabel}
            </span>
          )}
        </div>
        {description && (
          <p className="mt-1 line-clamp-2 text-xs leading-relaxed text-content-secondary">
            {description}
          </p>
        )}
        {syncSummaryText && !syncProgress?.active && (
          <p className="mt-1 truncate text-[11px] text-content-muted">
            {syncSummaryText}
          </p>
        )}
        {syncProgress?.active && (
          <div className="mt-1.5">
            <div className="h-1.5 w-full overflow-hidden rounded-full bg-surface-subtle">
              {syncProgress.percent != null ? (
                <div
                  className="h-full rounded-full bg-primary-400 transition-all duration-300"
                  style={{ width: `${syncProgress.percent}%` }}
                />
              ) : (
                <div className="h-full w-1/2 animate-pulse rounded-full bg-primary-400/80" />
              )}
            </div>
            {syncProgress.message && (
              <p className="mt-1 truncate text-[11px] text-primary-600">{syncProgress.message}</p>
            )}
            {syncProgress.metricsText && (
              <p className="mt-0.5 truncate text-[11px] text-content-muted">
                {syncProgress.metricsText}
              </p>
            )}
          </div>
        )}
      </div>

      <div className="flex shrink-0 items-center gap-1">
        {secondaryActions && secondaryActions.length > 0 && (
          <div className="relative" ref={menuRef}>
            <Button
              iconOnly
              variant="tertiary"
              size="sm"
              onClick={e => {
                e.stopPropagation();
                setMenuOpen(prev => !prev);
              }}
              title={t('skills.card.moreActions')}
              aria-label={t('skills.card.moreActions')}
              className="h-7 w-7 text-content-faint">
              <svg className="h-3.5 w-3.5" fill="currentColor" viewBox="0 0 24 24">
                <circle cx="5" cy="12" r="2" />
                <circle cx="12" cy="12" r="2" />
                <circle cx="19" cy="12" r="2" />
              </svg>
            </Button>
            {menuOpen && (
              <div className="absolute right-0 top-8 z-10 w-36 rounded-xl border border-line bg-surface py-1 shadow-md">
                {secondaryActions.map(action => (
                  <Button
                    key={action.label}
                    variant="tertiary"
                    size="sm"
                    data-testid={action.testId}
                    disabled={action.disabled}
                    onClick={e => {
                      e.stopPropagation();
                      setMenuOpen(false);
                      action.onClick();
                    }}
                    leadingIcon={action.icon}
                    className="flex h-auto w-full justify-start rounded-none px-3 py-2 text-xs text-content-secondary hover:bg-surface-hover">
                    {action.label}
                  </Button>
                ))}
              </div>
            )}
          </div>
        )}
        <Button
          variant="secondary"
          size="xs"
          data-testid={ctaTestId}
          disabled={ctaDisabled}
          onClick={e => {
            e.stopPropagation();
            onCtaClick();
          }}
          className={cn(
            'h-auto shrink-0 rounded-lg px-3 py-1.5 text-[11px] font-medium',
            ctaStyle,
            ctaDisabled && 'cursor-not-allowed opacity-50'
          )}>
          {ctaLabel}
        </Button>
      </div>
    </div>
  );
}

export default UnifiedSkillCard;