import { memo, useEffect, useRef } from 'react'; import { IconButton } from '~/components/ui/IconButton'; import type { PreviewInfo } from '~/lib/stores/previews'; interface PortDropdownProps { activePreviewIndex: number; setActivePreviewIndex: (index: number) => void; isDropdownOpen: boolean; setIsDropdownOpen: (value: boolean) => void; setHasSelectedPreview: (value: boolean) => void; previews: PreviewInfo[]; } export const PortDropdown = memo( ({ activePreviewIndex, setActivePreviewIndex, isDropdownOpen, setIsDropdownOpen, setHasSelectedPreview, previews, }: PortDropdownProps) => { const dropdownRef = useRef(null); // sort previews, preserving original index const sortedPreviews = previews .map((previewInfo, index) => ({ ...previewInfo, index })) .sort((a, b) => a.port - b.port); // close dropdown if user clicks outside useEffect(() => { const handleClickOutside = (event: MouseEvent) => { if (dropdownRef.current && !dropdownRef.current.contains(event.target as Node)) { setIsDropdownOpen(false); } }; if (isDropdownOpen) { window.addEventListener('mousedown', handleClickOutside); } else { window.removeEventListener('mousedown', handleClickOutside); } return () => { window.removeEventListener('mousedown', handleClickOutside); }; }, [isDropdownOpen]); return (
setIsDropdownOpen(!isDropdownOpen)} /> {isDropdownOpen && (
Ports
{sortedPreviews.map((preview) => (
{ setActivePreviewIndex(preview.index); setIsDropdownOpen(false); setHasSelectedPreview(true); }} > {preview.port}
))}
)}
); }, );