import React, { useState, useEffect, useRef } from 'react'; import { AndroidVM } from '../types'; import { Terminal, Send, Copy, Check, Trash2, Play, ShieldAlert, Cpu, Layers, Search, } from 'lucide-react'; interface AdbConsoleProps { vm: AndroidVM; vms: AndroidVM[]; onSelectVm: (vmId: string) => void; onExecuteAdb: (command: string) => Promise; } export const AdbConsole: React.FC = ({ vm, vms, onSelectVm, onExecuteAdb, }) => { const [commandInput, setCommandInput] = useState(''); const [terminalHistory, setTerminalHistory] = useState< { command: string; output: string; timestamp: string }[] >([ { command: 'adb connect 192.168.122.105:5555', output: `* daemon not running; starting now at tcp:5037\n* daemon started successfully\nconnected to ${vm.ipAddress}:${vm.adbPort}`, timestamp: new Date().toLocaleTimeString(), }, ]); const [isExecuting, setIsExecuting] = useState(false); const [copiedIndex, setCopiedIndex] = useState(null); const terminalEndRef = useRef(null); const scrollToBottom = () => { terminalEndRef.current?.scrollIntoView({ behavior: 'smooth' }); }; useEffect(() => { scrollToBottom(); }, [terminalHistory]); const handleRunCommand = async (cmdToRun?: string) => { const cmd = (cmdToRun || commandInput).trim(); if (!cmd) return; setIsExecuting(true); const result = await onExecuteAdb(cmd); setTerminalHistory((prev) => [ ...prev, { command: cmd, output: result, timestamp: new Date().toLocaleTimeString(), }, ]); setCommandInput(''); setIsExecuting(false); }; const presetCommands = [ { label: 'System Build Props', cmd: 'getprop' }, { label: 'List Packages', cmd: 'pm list packages' }, { label: 'Check Root Privilege', cmd: 'su' }, { label: 'CPU Info', cmd: 'cat /proc/cpuinfo' }, { label: 'Battery Dumpsys', cmd: 'dumpsys battery' }, { label: 'Live Logcat', cmd: 'logcat -d' }, ]; const handleCopy = (text: string, index: number) => { navigator.clipboard.writeText(text); setCopiedIndex(index); setTimeout(() => setCopiedIndex(null), 2000); }; return (
{/* Target VM Selector & Network Status Bar */}
Target Instance:
ADB TCP: {vm.ipAddress}:{vm.adbPort} SELinux: {vm.bootloader.selinuxMode}
{/* Preset Quick Commands */}
Quick ADB Shell Presets:
{presetCommands.map((preset) => ( ))}
{/* Interactive ADB Shell Terminal View */}
{/* Terminal Header */}
adb shell shell@{vm.profile.id}:/$
{/* Console Log Area */}
{terminalHistory.map((item, idx) => (
$ {item.command}
{item.timestamp}
                {item.output}
              
))}
{/* Terminal Command Input */}
{ e.preventDefault(); handleRunCommand(); }} className="flex items-center space-x-2" > $ setCommandInput(e.target.value)} placeholder="Type adb shell command (e.g. getprop, logcat, su)..." disabled={isExecuting} className="flex-1 bg-slate-950 border border-slate-800 rounded-xl px-3.5 py-2 text-xs font-mono text-slate-100 placeholder-slate-600 focus:outline-none focus:border-emerald-500" />
); };