From 822a239e6a1ddf177561ee81eff68abf74b9fba6 Mon Sep 17 00:00:00 2001 From: sivertlv Date: Tue, 27 Oct 2026 19:13:06 +0100 Subject: [PATCH] add search field, dosent work as db not queryable yet --- apps/frontend/src/components/Map.tsx | 204 ++++++++++++++++++++++++++- 1 file changed, 200 insertions(+), 4 deletions(-) diff --git a/apps/frontend/src/components/Map.tsx b/apps/frontend/src/components/Map.tsx index 8137d86..b0d4ca0 100644 --- a/apps/frontend/src/components/Map.tsx +++ b/apps/frontend/src/components/Map.tsx @@ -1,10 +1,198 @@ -import { MapContainer, Marker, Popup, TileLayer } from 'react-leaflet' +import { useQuery } from '@tanstack/react-query' +import { useEffect, useId, useMemo, useState } from 'react' +import { MapContainer, Marker, Popup, TileLayer, useMap } from 'react-leaflet' + +import { graphqlFetch } from '../graphql/client' + +const DEFAULT_CENTER: [number, number] = [63.4305, 10.3951] + +// The backend does not expose a `cities` query yet. This is written against the +// shape it will have once it does: id, name and the city centre coordinates. +const GET_CITIES = ` + query GetCities { + cities { + id + name + lat + lon + } + } +` + +interface City { + id: string + name: string + lat: number + lon: number +} + +function FlyToCity({ city }: { city: City | null }) { + const map = useMap() + + useEffect(() => { + if (city) { + map.flyTo([city.lat, city.lon], 12) + } + }, [city, map]) + + return null +} + +function SearchIcon() { + return ( + + ) +} + +function CitySearch({ + cities, + isLoading, + isError, + onSelect, +}: { + cities: City[] + isLoading: boolean + isError: boolean + onSelect: (city: City) => void +}) { + const inputId = useId() + const listId = useId() + const [query, setQuery] = useState('') + const [open, setOpen] = useState(false) + + const matches = useMemo(() => { + const needle = query.trim().toLowerCase() + if (!needle) return [] + + return cities + .filter((city) => city.name.toLowerCase().includes(needle)) + .slice(0, 6) + }, [cities, query]) + + function select(city: City) { + setQuery(city.name) + setOpen(false) + onSelect(city) + } + + const showResults = open && query.trim().length > 0 + + return ( +
+
{ + event.preventDefault() + const first = matches.at(0) + if (first) select(first) + }} + > + +
+ + { + setQuery(event.target.value) + setOpen(true) + }} + onFocus={() => setOpen(true)} + onBlur={() => setOpen(false)} + placeholder="Search for a city…" + autoComplete="off" + aria-controls={listId} + aria-expanded={showResults} + aria-autocomplete="list" + className="w-full bg-transparent text-sm text-[var(--sea-ink)] outline-none placeholder:text-[var(--sea-ink-soft)] [&::-webkit-search-cancel-button]:appearance-none" + /> + {query.length > 0 && ( + + )} +
+
+ + {showResults && ( + + )} +
+ ) +} export default function Map() { + const [selectedCity, setSelectedCity] = useState(null) + + const { data, isPending, isError } = useQuery({ + queryKey: ['cities'], + queryFn: () => graphqlFetch<{ cities: City[] }>(GET_CITIES), + }) + return ( -
+
+ - + + Guinness!!! + {selectedCity && ( + + + {selectedCity.name} + + + )}
)