import { useEffect, useRef, useState } from 'react'; import { X, Loader2 } from 'lucide-react'; import { SearchAwardReferences } from '../../wailsjs/go/main/App'; import { useI18n } from '@/lib/i18n'; type Ref = { code: string; name: string; dxcc: number; group: string; subgrp: string }; interface Props { code: string; // award code, e.g. "POTA" label: string; // display label dxcc?: number; // contacted-station DXCC; used when "this country" is on countryOnly: boolean; value: string; // currently assigned reference onChange: (ref: string) => void; } // AwardRefPicker — type-ahead search over an award's reference list (POTA parks, // SOTA summits, …), optionally restricted to the contacted DXCC. Picking a // result assigns it to the QSO. export function AwardRefPicker({ code, label, dxcc, countryOnly, value, onChange }: Props) { const { t } = useI18n(); const [q, setQ] = useState(''); const [results, setResults] = useState([]); const [open, setOpen] = useState(false); const [busy, setBusy] = useState(false); const boxRef = useRef(null); useEffect(() => { if (!open) return; const t = window.setTimeout(async () => { setBusy(true); try { const r = await SearchAwardReferences(code, q, countryOnly ? (dxcc ?? 0) : 0, 30); setResults((r ?? []) as any); } catch { setResults([]); } finally { setBusy(false); } }, 200); return () => window.clearTimeout(t); }, [q, open, code, dxcc, countryOnly]); useEffect(() => { if (!open) return; const close = (e: MouseEvent) => { if (boxRef.current && !boxRef.current.contains(e.target as Node)) setOpen(false); }; window.addEventListener('mousedown', close); return () => window.removeEventListener('mousedown', close); }, [open]); return (
{value ? (
{value}
) : ( setOpen(true)} onChange={(e) => { setQ(e.target.value); setOpen(true); }} /> )} {open && !value && (
{busy &&
{t('awrp.searching')}
} {!busy && results.length === 0 && (
{countryOnly && dxcc ? t('awrp.noMatchDxcc') : t('awrp.noMatch')}
)} {results.map((r) => ( ))}
)}
); }