"use client" import { useState, useEffect } from "react" import { Button } from "@/components/ui/button" import { Card, CardContent } from "@/components/ui/card" import { Input } from "@/components/ui/input" import { Label } from "@/components/ui/label" import { Badge } from "@/components/ui/badge" import { Search, Filter, Smartphone, Loader2, CheckCircle2 } from "lucide-react" import { Alert, AlertDescription } from "@/components/ui/alert" import { AlertCircle } from "lucide-react" import { useMobile } from "@/hooks/use-mobile" interface Device { id: string name: string status: "online" | "offline" | "busy" lastActive: string type: "android" | "ios" number?: string } // 模拟设备数据 const mockDevices: Device[] = [ { id: "device-1", name: "设备1", status: "online", lastActive: "2023-05-15 14:30", type: "android", number: "13812345678", }, { id: "device-2", name: "设备2", status: "offline", lastActive: "2023-05-14 09:15", type: "ios", number: "13987654321", }, { id: "device-3", name: "设备3", status: "busy", lastActive: "2023-05-15 11:45", type: "android", number: "15612345678", }, { id: "device-4", name: "设备4", status: "online", lastActive: "2023-05-15 13:20", type: "android", number: "18712345678", }, { id: "device-5", name: "设备5", status: "online", lastActive: "2023-05-15 10:05", type: "ios", number: "13612345678", }, ] interface DeviceSelectionProps { onNext: () => void onPrevious: () => void initialSelectedDevices?: string[] onDevicesChange: (deviceIds: string[]) => void } export function DeviceSelection({ onNext, onPrevious, initialSelectedDevices = [], onDevicesChange, }: DeviceSelectionProps) { const isMobile = useMobile() const [searchQuery, setSearchQuery] = useState("") const [selectedDevices, setSelectedDevices] = useState(initialSelectedDevices) const [devices, setDevices] = useState([]) const [loading, setLoading] = useState(true) const [error, setError] = useState(null) const [statusFilter, setStatusFilter] = useState<"all" | "online" | "offline" | "busy">("all") useEffect(() => { const fetchDevices = async () => { setLoading(true) try { // 模拟API调用 await new Promise((resolve) => setTimeout(resolve, 1000)) setDevices(mockDevices) } catch (error) { console.error("获取设备失败:", error) setError("获取设备列表失败,请稍后重试") } finally { setLoading(false) } } fetchDevices() }, []) useEffect(() => { // 只在设备选择变化时通知父组件,而不是每次渲染 const currentSelection = JSON.stringify(selectedDevices) const prevSelection = JSON.stringify(initialSelectedDevices) if (currentSelection !== prevSelection) { onDevicesChange(selectedDevices) } }, [selectedDevices, initialSelectedDevices, onDevicesChange]) const handleDeviceToggle = (deviceId: string) => { setSelectedDevices((prev) => (prev.includes(deviceId) ? prev.filter((id) => id !== deviceId) : [...prev, deviceId])) } const handleSelectAll = () => { if (selectedDevices.length === filteredDevices.length) { setSelectedDevices([]) } else { setSelectedDevices(filteredDevices.map((device) => device.id)) } } const filteredDevices = devices.filter((device) => { const matchesSearch = device.name.toLowerCase().includes(searchQuery.toLowerCase()) || (device.number && device.number.includes(searchQuery)) const matchesStatus = statusFilter === "all" || device.status === statusFilter return matchesSearch && matchesStatus }) const getStatusColor = (status: Device["status"]) => { switch (status) { case "online": return "bg-green-500/10 text-green-500" case "offline": return "bg-gray-500/10 text-gray-500" case "busy": return "bg-yellow-500/10 text-yellow-500" default: return "bg-gray-500/10 text-gray-500" } } const getStatusText = (status: Device["status"]) => { switch (status) { case "online": return "在线" case "offline": return "离线" case "busy": return "忙碌" default: return status } } return (
setSearchQuery(e.target.value)} className="pl-9" />
{loading ? (
正在加载设备列表...
) : error ? ( {error} ) : filteredDevices.length === 0 ? (
未找到匹配的设备
) : (
{filteredDevices.map((device) => (
handleDeviceToggle(device.id)} >
{selectedDevices.includes(device.id) ? ( ) : (
)}
{device.name} {getStatusText(device.status)}
{device.number &&
手机号: {device.number}
}
最后活跃: {device.lastActive}
))}
)}
已选择 {selectedDevices.length} / {filteredDevices.length} 个设备
) }