import { useEffect, useState, useCallback } from 'react' import { Plus, MapPin, User, Phone, Radio } from 'lucide-react' import { useStore } from '../store' import { fetchContacts } from '../api' export default function ContactList() { const contacts = useStore((s) => s.contacts) const contactsLoaded = useStore((s) => s.contactsLoaded) const setContacts = useStore((s) => s.setContacts) const setEditingContact = useStore((s) => s.setEditingContact) const setSelectedPlace = useStore((s) => s.setSelectedPlace) const [filter, setFilter] = useState('') const [authFailed, setAuthFailed] = useState(false) const loadContacts = useCallback(async () => { const data = await fetchContacts() if (data?.auth === false) { setAuthFailed(true) return } if (Array.isArray(data)) { setContacts(data) setAuthFailed(false) } }, [setContacts]) useEffect(() => { if (!contactsLoaded) loadContacts() }, [contactsLoaded, loadContacts]) if (authFailed) { return (

Sign in to use contacts

) } const q = filter.toLowerCase() const filtered = q ? contacts.filter((c) => (c.label || '').toLowerCase().includes(q) || (c.name || '').toLowerCase().includes(q) || (c.call_sign || '').toLowerCase().includes(q) || (c.phone || '').includes(q) ) : contacts const handleClick = (c) => { if (c.lat != null && c.lon != null) { setSelectedPlace({ lat: c.lat, lon: c.lon, name: c.label, address: c.address || null, type: 'contact', source: 'contacts', matchCode: null, raw: { osm_type: c.osm_type, osm_id: c.osm_id, contact: c }, }) } else { setEditingContact(c) } } return (
{/* Search + add */}
setFilter(e.target.value)} />
{/* List */} {filtered.length === 0 ? (
{contacts.length === 0 ? 'No contacts yet' : 'No matches'}
) : (
{filtered.map((c) => (
handleClick(c)} > {c.lat != null ? : c.call_sign ? : }
{c.label}
{c.name || c.address || c.phone || ''}
{c.phone && ( )} {c.show_proximity && c.lat != null && ( prox )}
))}
)}
) }