"use client" import { useState, useEffect, useRef } from "react" import { Button } from "@/components/ui/button" import { Card, CardContent } from "@/components/ui/card" import { Input } from "@/components/ui/input" import { Checkbox } from "@/components/ui/checkbox" import { Badge } from "@/components/ui/badge" import { Search, CheckCircle2, AlertCircle, Smartphone, Laptop } from "lucide-react" import { ScrollArea } from "@/components/ui/scroll-area" import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select" import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs" // 模拟设备数据 const mockDevices = Array.from({ length: 20 }).map((_, i) => ({ id: `device-${i + 1}`, name: `设备 ${i + 1}`, model: i % 3 === 0 ? "iPhone 13" : i % 3 === 1 ? "Xiaomi 12" : "Huawei P40", status: i % 5 === 0 ? "offline" : i % 7 === 0 ? "busy" : "online", account: `wxid_${100 + i}`, type: i % 2 === 0 ? "mobile" : "emulator", group: i % 3 === 0 ? "常用设备" : i % 3 === 1 ? "备用设备" : "测试设备", successRate: Math.floor(70 + Math.random() * 30), restrictCount: Math.floor(Math.random() * 5), })) interface DeviceSelectionProps { onNext: () => void onPrevious: () => void initialSelectedDevices?: string[] onDevicesChange: (deviceIds: string[]) => void } export function DeviceSelection({ onNext, onPrevious, initialSelectedDevices = [], onDevicesChange, }: DeviceSelectionProps) { const [selectedDevices, setSelectedDevices] = useState(initialSelectedDevices) const [searchQuery, setSearchQuery] = useState("") const [statusFilter, setStatusFilter] = useState("all") const [typeFilter, setTypeFilter] = useState("all") const [groupFilter, setGroupFilter] = useState("all") // 使用ref来跟踪是否已经通知了父组件初始选择 const initialNotificationRef = useRef(false) const deviceGroups = Array.from(new Set(mockDevices.map((device) => device.group))) const filteredDevices = mockDevices.filter((device) => { const matchesSearch = device.name.toLowerCase().includes(searchQuery.toLowerCase()) || device.account.toLowerCase().includes(searchQuery.toLowerCase()) const matchesStatus = statusFilter === "all" || device.status === statusFilter const matchesType = typeFilter === "all" || device.type === typeFilter const matchesGroup = groupFilter === "all" || device.group === groupFilter return matchesSearch && matchesStatus && matchesType && matchesGroup }) // 只在选择变化时通知父组件,使用防抖 useEffect(() => { if (!initialNotificationRef.current) { initialNotificationRef.current = true return } const timer = setTimeout(() => { onDevicesChange(selectedDevices) }, 300) return () => clearTimeout(timer) }, [selectedDevices, onDevicesChange]) const handleDeviceToggle = (deviceId: string) => { setSelectedDevices((prev) => (prev.includes(deviceId) ? prev.filter((id) => id !== deviceId) : [...prev, deviceId])) } const handleSelectAll = () => { setSelectedDevices(filteredDevices.map((device) => device.id)) } const handleDeselectAll = () => { setSelectedDevices([]) } const getStatusColor = (status: string) => { switch (status) { case "online": return "bg-green-500" case "offline": return "bg-gray-500" case "busy": return "bg-yellow-500" default: return "bg-gray-500" } } const getStatusText = (status: string) => { switch (status) { case "online": return "在线" case "offline": return "离线" case "busy": return "繁忙" default: return status } } return (
setSearchQuery(e.target.value)} className="pl-9" />
已选择 {selectedDevices.length} 台设备
列表视图 网格视图
{filteredDevices.map((device) => (
handleDeviceToggle(device.id)} disabled={device.status === "offline"} /> {device.type === "mobile" ? ( ) : ( )}
{device.name}
{device.model} | {device.account}
成功率:{" "} 90 ? "text-green-600" : "text-yellow-600"}> {device.successRate}%
限制次数:{" "} {device.restrictCount}
{getStatusText(device.status)}
))} {filteredDevices.length === 0 && (
没有找到符合条件的设备
)}
{filteredDevices.map((device) => (
{device.type === "mobile" ? ( ) : ( )} {device.name}
{getStatusText(device.status)}
{device.model}
{device.account}
成功率:{" "} 90 ? "text-green-600" : "text-yellow-600"}> {device.successRate}%
限制:{" "} {device.restrictCount}
))} {filteredDevices.length === 0 && (
没有找到符合条件的设备
)}
{selectedDevices.length === 0 && (
请至少选择一台设备来执行建群任务
)}
) }