File size: 9,816 Bytes
d15f07c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import { useEffect, useRef, useState } from 'react'
import { MagnifyingGlassIcon, XMarkIcon } from '@heroicons/react/24/outline'
import { nominatimUsStateCode } from '../utils/stateMapping'
import { tracedFetch } from '../lib/api'

/** Smallest geographic unit the search result actually identifies. */
export type MapAddressGranularity = 'state' | 'county' | 'place' | 'address'

export interface MapAddressResult {
  displayName: string
  shortLabel: string
  lat: number
  lon: number
  stateCode: string | null
  county: string
  city: string
  /** Classifies the Nominatim hit so callers can route to the right map tier. */
  granularity: MapAddressGranularity
}

interface MapAddressSearchProps {
  onPick: (result: MapAddressResult) => void
  onClear?: () => void
  initialValue?: string
  className?: string
  placeholder?: string
}

interface NominatimSuggestion {
  osm_id: number
  osm_type: string
  lat: string
  lon: string
  display_name: string
  address?: Record<string, unknown>
  /** Nominatim's coarse classification ("state", "county", "city", "house"…). */
  addresstype?: string
  /** Coarse OSM class ("boundary", "place", "building"…). */
  class?: string
  /** Finer-grained OSM type within the class ("administrative", "city"…). */
  type?: string
}

/**
 * Classify a Nominatim hit into the smallest tier our map can route to.
 *
 * - `state`   when the result IS a state (e.g. "Georgia")
 * - `county`  when the result IS a county (e.g. "Tuscaloosa County")
 * - `place`   when the result is a city/town/village/CDP (e.g. "Tuscaloosa")
 * - `address` when there's a house number, road, or anything finer-grained
 *
 * Nominatim's `addresstype` is the primary signal; we fall back to inspecting
 * the parsed `address` object for ambiguous cases (e.g. a hit on a city named
 * the same as its county).
 */
function classifyGranularity(s: NominatimSuggestion): MapAddressGranularity {
  const at = (s.addresstype ?? '').toLowerCase()
  const cls = (s.class ?? '').toLowerCase()
  const typ = (s.type ?? '').toLowerCase()
  if (at === 'state') return 'state'
  if (at === 'county') return 'county'
  if (at === 'city' || at === 'town' || at === 'village' || at === 'hamlet' || at === 'municipality') {
    return 'place'
  }
  // Boundary-class results without an addresstype: lean on type for state/county.
  if (cls === 'boundary' && typ === 'administrative') {
    const addr = s.address ?? {}
    if ((addr as Record<string, unknown>).state && !(addr as Record<string, unknown>).county) return 'state'
    if ((addr as Record<string, unknown>).county) return 'county'
  }
  // Anything with a house number or road is definitely an address.
  const a = (s.address ?? {}) as Record<string, unknown>
  if (a.house_number || a.road || a.building || a.amenity) return 'address'
  // Place-class hits (neighborhood, suburb…) collapse to the parent place.
  if (cls === 'place') return 'place'
  return 'address'
}

function shortLabelFromAddress(addr: Record<string, unknown> | undefined, fallback: string): string {
  if (!addr) return fallback
  const road = (addr.road as string) || ''
  const houseNumber = (addr.house_number as string) || ''
  const city =
    (addr.city as string) ||
    (addr.town as string) ||
    (addr.village as string) ||
    (addr.hamlet as string) ||
    (addr.municipality as string) ||
    ''
  const state = (addr.state as string) || ''
  const street = [houseNumber, road].filter(Boolean).join(' ').trim()
  const parts = [street, city, state].filter(Boolean)
  return parts.length ? parts.join(', ') : fallback
}

export default function MapAddressSearch({
  onPick,
  onClear,
  initialValue = '',
  className = '',
  placeholder = 'Search address, city, or place',
}: MapAddressSearchProps) {
  const [value, setValue] = useState(initialValue)
  const [suggestions, setSuggestions] = useState<NominatimSuggestion[]>([])
  const [open, setOpen] = useState(false)
  const [activeIdx, setActiveIdx] = useState(-1)
  const [loading, setLoading] = useState(false)
  const debounceRef = useRef<number | null>(null)
  const rootRef = useRef<HTMLDivElement | null>(null)

  useEffect(() => {
    const onDoc = (e: MouseEvent) => {
      if (!rootRef.current) return
      if (!rootRef.current.contains(e.target as Node)) setOpen(false)
    }
    document.addEventListener('mousedown', onDoc)
    return () => document.removeEventListener('mousedown', onDoc)
  }, [])

  const fetchSuggestions = async (query: string) => {
    if (query.trim().length < 3) {
      setSuggestions([])
      setOpen(false)
      return
    }
    setLoading(true)
    try {
      // Same-origin proxy (api/routes/geocode.py) — avoids Nominatim CORS and
      // honors its 1 req/s policy via server-side throttle + cache.
      const r = await tracedFetch(
        `/api/geocode/search?q=${encodeURIComponent(query)}&limit=6`,
      )
      if (!r.ok) return
      const raw: NominatimSuggestion[] = await r.json()
      const seen = new Set<string>()
      const deduped = raw.filter((s) => {
        const k = `${s.osm_type}_${s.osm_id}`
        if (seen.has(k)) return false
        seen.add(k)
        return true
      })
      setSuggestions(deduped)
      setOpen(deduped.length > 0)
      setActiveIdx(-1)
    } catch {
      // ignore network blips — user can retype
    } finally {
      setLoading(false)
    }
  }

  const handleChange = (v: string) => {
    setValue(v)
    if (debounceRef.current) window.clearTimeout(debounceRef.current)
    debounceRef.current = window.setTimeout(() => fetchSuggestions(v), 280)
  }

  const choose = (s: NominatimSuggestion) => {
    const lat = Number(s.lat)
    const lon = Number(s.lon)
    if (!Number.isFinite(lat) || !Number.isFinite(lon)) return
    const stateCode = nominatimUsStateCode(s.address ?? {}) || null
    const county = ((s.address?.county as string) || '').trim()
    const city =
      ((s.address?.city as string) ||
        (s.address?.town as string) ||
        (s.address?.village as string) ||
        (s.address?.hamlet as string) ||
        (s.address?.municipality as string) ||
        '').trim()
    const shortLabel = shortLabelFromAddress(s.address, s.display_name)
    const granularity = classifyGranularity(s)
    setValue(shortLabel)
    setOpen(false)
    setSuggestions([])
    onPick({
      displayName: s.display_name,
      shortLabel,
      lat,
      lon,
      stateCode,
      county,
      city,
      granularity,
    })
  }

  const onKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
    if (e.key === 'Escape') {
      setOpen(false)
      return
    }
    if (!open || !suggestions.length) {
      if (e.key === 'Enter') {
        e.preventDefault()
        fetchSuggestions(value)
      }
      return
    }
    if (e.key === 'ArrowDown') {
      e.preventDefault()
      setActiveIdx((i) => Math.min(suggestions.length - 1, i + 1))
    } else if (e.key === 'ArrowUp') {
      e.preventDefault()
      setActiveIdx((i) => Math.max(0, i - 1))
    } else if (e.key === 'Enter') {
      e.preventDefault()
      const pick = activeIdx >= 0 ? suggestions[activeIdx] : suggestions[0]
      if (pick) choose(pick)
    }
  }

  const clear = () => {
    setValue('')
    setSuggestions([])
    setOpen(false)
    setActiveIdx(-1)
    onClear?.()
  }

  return (
    <div ref={rootRef} className={`relative ${className}`}>
      <div className="flex items-center gap-2 rounded-full border border-slate-200 bg-white px-3 py-1.5 shadow-md focus-within:border-[#354F52] focus-within:ring-2 focus-within:ring-[#354F52]/30">
        <MagnifyingGlassIcon className="h-4 w-4 shrink-0 text-slate-500" aria-hidden />
        <input
          type="text"
          aria-label="Search address"
          value={value}
          placeholder={placeholder}
          className="min-w-0 flex-1 bg-transparent text-sm text-slate-900 placeholder:text-slate-400 focus:outline-none"
          onChange={(e) => handleChange(e.target.value)}
          onFocus={() => {
            if (suggestions.length) setOpen(true)
          }}
          onKeyDown={onKeyDown}
        />
        {value ? (
          <button
            type="button"
            onClick={clear}
            className="rounded-full p-0.5 text-slate-400 hover:bg-slate-100 hover:text-slate-700"
            aria-label="Clear search"
          >
            <XMarkIcon className="h-4 w-4" />
          </button>
        ) : null}
      </div>
      {open && suggestions.length > 0 ? (
        <ul
          role="listbox"
          className="absolute left-0 right-0 top-[110%] z-40 max-h-72 overflow-auto rounded-xl border border-slate-200 bg-white py-1 shadow-xl"
        >
          {suggestions.map((s, i) => {
            const short = shortLabelFromAddress(s.address, s.display_name)
            const active = i === activeIdx
            return (
              <li key={`${s.osm_type}_${s.osm_id}`}>
                <button
                  type="button"
                  role="option"
                  aria-selected={active}
                  onMouseEnter={() => setActiveIdx(i)}
                  onClick={() => choose(s)}
                  className={`block w-full px-3 py-2 text-left text-sm leading-snug ${
                    active ? 'bg-slate-100 text-slate-900' : 'text-slate-700 hover:bg-slate-50'
                  }`}
                >
                  <div className="font-medium">{short}</div>
                  <div className="mt-0.5 truncate text-xs text-slate-500">{s.display_name}</div>
                </button>
              </li>
            )
          })}
        </ul>
      ) : null}
      {loading ? (
        <div className="pointer-events-none absolute right-10 top-1/2 -translate-y-1/2 text-[10px] text-slate-400">
          …
        </div>
      ) : null}
    </div>
  )
}