import { useEffect, useState, useCallback } from "react"; import type { BluetoothDevice } from "../types/electron"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { Button } from "@/components/ui/button"; export function BluetoothDevicePicker() { const [devices, setDevices] = useState([]); const [isOpen, setIsOpen] = useState(false); const [isScanning, setIsScanning] = useState(false); useEffect(() => { // Only set up listener in Electron if (window.electronAPI?.onBluetoothDeviceList) { window.electronAPI.onBluetoothDeviceList((deviceList) => { console.log("[BluetoothPicker] Received device list:", deviceList); setDevices(deviceList); // Open the picker when scan starts (even if empty at first) if (!isOpen) { setIsOpen(true); setIsScanning(true); } // Stop showing scanning state once we have devices if (deviceList.length > 0) { setIsScanning(false); } }); } }, [isOpen]); const handleSelectDevice = useCallback((deviceId: string) => { console.log("[BluetoothPicker] User selected device:", deviceId); window.electronAPI?.selectBluetoothDevice(deviceId); setIsOpen(false); setDevices([]); }, []); const handleCancel = useCallback(() => { console.log("[BluetoothPicker] User cancelled device selection"); window.electronAPI?.selectBluetoothDevice(""); setIsOpen(false); setDevices([]); setIsScanning(false); }, []); return ( !open && handleCancel()}> Select Bluetooth Device {isScanning && devices.length === 0 ? (
Scanning for Bluetooth devices...
) : ( `${devices.length} device${devices.length !== 1 ? "s" : ""} found. Select a device to connect:` )}
{!isScanning && devices.length > 0 && (
{devices.map((device) => ( ))}
)}
); }