All files / src/components/BrowserHand BrowserHandCard.tsx

0% Statements 0/170
0% Branches 0/1
0% Functions 0/1
0% Lines 0/170

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
/**
 * BrowserHandCard Component
 *
 * Main card for Browser Hand with real-time status and screenshot preview.
 */
 
import React from 'react';
import {
  Globe,
  Camera,
  RefreshCw,
  Settings,
  Play,
  Loader2,
  CheckCircle,
  XCircle,
  AlertCircle,
  ExternalLink,
} from 'lucide-react';
import { cn } from '../../lib/utils';
import { useBrowserHandStore } from '../../store/browserHandStore';
import { ScreenshotPreview } from './ScreenshotPreview';
import { TaskTemplateModal } from './TaskTemplateModal';
import type { Hand } from '../../types/hands';
 
interface BrowserHandCardProps {
  hand: Hand;
  onOpenSettings?: () => void;
}
 
export function BrowserHandCard({ onOpenSettings }: BrowserHandCardProps) {
  const {
    execution,
    sessions,
    activeSessionId,
    isTemplateModalOpen,
    isLoading,
    error,
    openTemplateModal,
    closeTemplateModal,
    takeScreenshot,
    createSession,
    closeSession,
    clearError,
  } = useBrowserHandStore();
 
  const [isStarting, setIsStarting] = React.useState(false);
 
  // Auto-start session if needed
  React.useEffect(() => {
    if (sessions.length === 0 && !activeSessionId) {
      setIsStarting(true);
      createSession({ headless: true })
        .then(() => setIsStarting(false))
        .catch(() => setIsStarting(false));
    }
  }, [sessions.length, activeSessionId, createSession]);
 
  // Get status display
  const getStatusDisplay = () => {
    if (isStarting || isLoading) {
      return { text: '连接中...', color: 'text-yellow-500', icon: Loader2 };
    }
    if (error) {
      return { text: '错误', color: 'text-red-500', icon: XCircle };
    }
    if (execution.isRunning) {
      return { text: '运行中', color: 'text-blue-500', icon: Play };
    }
    if (activeSessionId) {
      return { text: '就绪', color: 'text-green-500', icon: CheckCircle };
    }
    return { text: '未连接', color: 'text-gray-500', icon: AlertCircle };
  };
 
  const status = getStatusDisplay();
  const StatusIcon = status.icon;
 
  return (
    <div className="rounded-xl border border-gray-200 dark:border-gray-700 bg-white dark:bg-gray-900 overflow-hidden">
      {/* Header */}
      <div className="flex items-center justify-between p-4 border-b border-gray-200 dark:border-gray-700">
        <div className="flex items-center gap-3">
          <div className="p-2 rounded-lg bg-blue-100 dark:bg-blue-900/30">
            <Globe className="h-5 w-5 text-blue-600 dark:text-blue-400" />
          </div>
          <div>
            <h3 className="font-semibold text-gray-900 dark:text-white">
              Browser Hand
            </h3>
            <p className="text-sm text-gray-500 dark:text-gray-400">
              浏览器自动化能力
            </p>
          </div>
        </div>
        <div className="flex items-center gap-2">
          <span className={cn('flex items-center gap-1 text-sm', status.color)}>
            <StatusIcon className="h-4 w-4" />
            {status.text}
          </span>
        </div>
      </div>
 
      {/* Screenshot Preview */}
      <div className="p-4 border-b border-gray-200 dark:border-gray-700">
        <ScreenshotPreview
          base64={execution.lastScreenshot}
          isLoading={isLoading || execution.isRunning}
          onRefresh={() => {
            if (activeSessionId) {
              takeScreenshot();
            }
          }}
          altText={execution.isRunning ? '执行中...' : '等待截图'}
        />
      </div>
 
      {/* Status Bar */}
      {(execution.isRunning || execution.currentUrl) && (
        <div className="px-4 py-2 border-b border-gray-200 dark:border-gray-700 bg-gray-50 dark:bg-gray-800/50">
          {execution.currentUrl && (
            <div className="flex items-center gap-2 text-sm text-gray-600 dark:text-gray-400 mb-2">
            <ExternalLink className="h-4 w-4" />
            <span className="truncate font-mono">{execution.currentUrl}</span>
          </div>
          )}
          {execution.isRunning && (
            <>
              <div className="flex items-center gap-2 text-sm text-gray-600 dark:text-gray-400 mb-2">
                <Play className="h-4 w-4" />
                <span>{execution.currentAction || '处理中...'}</span>
              </div>
              <div className="w-full bg-gray-200 dark:bg-gray-700 rounded-full h-2 overflow-hidden">
                <div
                  className="h-full bg-blue-500 transition-all duration-300"
                  style={{ width: `${execution.progress}%` }}
                />
              </div>
            </>
          )}
        </div>
      )}
 
      {/* Error Display */}
      {error && (
        <div className="px-4 py-3 bg-red-50 dark:bg-red-900/20 border-b border-red-200 dark:border-red-800">
          <div className="flex items-center justify-between">
            <div className="flex items-center gap-2 text-red-600 dark:text-red-400">
              <AlertCircle className="h-4 w-4" />
              <span className="text-sm">{error}</span>
            </div>
            <button
              onClick={clearError}
              className="text-red-600 dark:text-red-400 hover:text-red-700"
            >
              <XCircle className="h-4 w-4" />
            </button>
          </div>
        </div>
      )}
 
      {/* Actions */}
      <div className="p-4">
        <div className="flex flex-wrap gap-2">
          <button
            onClick={openTemplateModal}
            disabled={isLoading || execution.isRunning || !activeSessionId}
            className={cn(
              'flex-1 flex items-center justify-center gap-2 px-3 py-2 rounded-lg font-medium transition-colors',
              activeSessionId && !execution.isRunning
                ? 'bg-gray-700 dark:bg-gray-600 text-white hover:bg-gray-800 dark:hover:bg-gray-500'
                : 'bg-gray-200 text-gray-400 dark:bg-gray-700 dark:text-gray-500 cursor-not-allowed'
            )}
          >
            <Play className="h-4 w-4" />
            执行任务
          </button>
 
          <button
            onClick={() => activeSessionId && takeScreenshot()}
            disabled={isLoading || execution.isRunning || !activeSessionId}
            className={cn(
              'flex items-center justify-center gap-2 px-3 py-2 rounded-lg border transition-colors',
              activeSessionId && !execution.isRunning
                ? 'border-gray-300 dark:border-gray-600 hover:bg-gray-100 dark:hover:bg-gray-800 text-gray-700 dark:text-gray-300'
                : 'border-gray-200 dark:border-gray-700 text-gray-400 dark:text-gray-500 cursor-not-allowed'
            )}
            title="截图"
          >
            <Camera className="h-4 w-4" />
          </button>
 
          <button
            onClick={() => {
              if (activeSessionId) {
                closeSession(activeSessionId);
              }
            }}
            disabled={isLoading || execution.isRunning || !activeSessionId}
            className={cn(
              'flex items-center justify-center gap-2 px-3 py-2 rounded-lg border transition-colors',
              activeSessionId && !execution.isRunning
                ? 'border-gray-300 dark:border-gray-600 hover:bg-gray-100 dark:hover:bg-gray-800 text-gray-700 dark:text-gray-300'
                : 'border-gray-200 dark:border-gray-700 text-gray-400 dark:text-gray-500 cursor-not-allowed'
            )}
            title="重置会话"
          >
            <RefreshCw className="h-4 w-4" />
          </button>
 
          {onOpenSettings && (
            <button
              onClick={onOpenSettings}
              className="flex items-center justify-center gap-2 px-3 py-2 rounded-lg border border-gray-300 dark:border-gray-600 hover:bg-gray-100 dark:hover:bg-gray-800 text-gray-700 dark:text-gray-300 transition-colors"
              title="设置"
            >
              <Settings className="h-4 w-4" />
            </button>
          )}
        </div>
      </div>
 
      {/* Template Modal */}
      <TaskTemplateModal
        isOpen={isTemplateModalOpen}
        onClose={closeTemplateModal}
        onSelect={(template, params) => {
          const { executeTemplate } = useBrowserHandStore.getState();
          executeTemplate(template.id, params);
        }}
      />
    </div>
  );
}