import { useEffect, useRef, useState } from 'react'; import { Laptop, MonitorSpeaker, Smartphone } from 'lucide-react'; import { usePlaybackSyncContext } from './PlaybackSyncProvider'; /** * Device menu. Picking a device moves the audio there: the chosen browser * resumes the same track at the same position, and the one that had it stops. */ export function DevicePicker() { const { devices, deviceId, isOwner, hasRemoteOwner, transferTo } = usePlaybackSyncContext(); const [open, setOpen] = useState(false); const wrapper = useRef(null); useEffect(() => { if (!open) return; const onPointerDown = (event: MouseEvent) => { if (!wrapper.current?.contains(event.target as Node)) setOpen(false); }; document.addEventListener('mousedown', onPointerDown); return () => document.removeEventListener('mousedown', onPointerDown); }, [open]); const online = devices.filter((device) => device.online || device.id === deviceId); // Nothing to switch between, so the control would only take up room. if (online.length < 2 && !hasRemoteOwner) return null; const owner = devices.find((device) => device.isOwner) ?? null; return (
{open && (
Play on
{online.map((device) => { const isThis = device.id === deviceId; return ( ); })} {!isOwner && !hasRemoteOwner && (
Press play to take over the session.
)}
)}
); }