"use client" import { useEffect, useId, useRef, useState } from "react" import { CaretDown, Check, VideoCamera } from "@phosphor-icons/react" import { Button } from "@/components/ui/button" import { cn } from "@/lib/utils" /* FR-08 (P1): pilih kamera bila perangkat punya lebih dari satu. Pola disclosure sederhana: tombol kepala panel + daftar tombol perangkat, jadi seluruh pemilih bisa dipakai keyboard tanpa komponen baru. */ export function CameraPicker({ devices, selectedId, onSelect, }: { devices: MediaDeviceInfo[] selectedId: string | null onSelect: (deviceId: string) => void }) { const [open, setOpen] = useState(false) const rootRef = useRef(null) const listId = useId() useEffect(() => { if (!open) { return } function handlePointerDown(event: MouseEvent) { if (rootRef.current !== null && !rootRef.current.contains(event.target as Node)) { setOpen(false) } } function handleKeyDown(event: globalThis.KeyboardEvent) { if (event.key === "Escape") { setOpen(false) } } document.addEventListener("mousedown", handlePointerDown) document.addEventListener("keydown", handleKeyDown) return () => { document.removeEventListener("mousedown", handlePointerDown) document.removeEventListener("keydown", handleKeyDown) } }, [open]) const selectedIndex = devices.findIndex( (device) => device.deviceId === selectedId ) const selectedDevice = selectedIndex === -1 ? null : devices[selectedIndex] return (
{open ? ( ) : null}
) }