Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
204 changes: 200 additions & 4 deletions apps/frontend/src/components/Map.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<svg
aria-hidden="true"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
className="h-4 w-4 shrink-0 text-[var(--sea-ink-soft)]"
>
<circle cx="11" cy="11" r="7" />
<path d="m20 20-3.5-3.5" />
</svg>
)
}

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 (
<div className="absolute top-3 right-3 z-[1000] w-[min(20rem,calc(100%-1.5rem))]">
<form
role="search"
onSubmit={(event) => {
event.preventDefault()
const first = matches.at(0)
if (first) select(first)
}}
>
<label htmlFor={inputId} className="sr-only">
Search for a city
</label>
<div className="flex items-center gap-2 rounded-xl border border-[var(--line)] bg-[var(--surface-strong)] px-3 py-2 shadow-lg backdrop-blur">
<SearchIcon />
<input
id={inputId}
type="search"
value={query}
onChange={(event) => {
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 && (
<button
type="button"
aria-label="Clear search"
onClick={() => {
setQuery('')
setOpen(false)
}}
className="shrink-0 rounded-full px-1 text-sm leading-none text-[var(--sea-ink-soft)] hover:text-[var(--sea-ink)]"
>
✕
</button>
)}
</div>
</form>

{showResults && (
<ul
id={listId}
role="listbox"
className="mt-2 overflow-hidden rounded-xl border border-[var(--line)] bg-[var(--surface-strong)] shadow-lg backdrop-blur"
>
{isLoading && (
<li className="px-3 py-2 text-sm text-[var(--sea-ink-soft)]">
Loading cities…
</li>
)}
{isError && (
<li className="px-3 py-2 text-sm text-[var(--sea-ink-soft)]">
City search is unavailable right now.
</li>
)}
{!isLoading && !isError && matches.length === 0 && (
<li className="px-3 py-2 text-sm text-[var(--sea-ink-soft)]">
No city matches “{query.trim()}”.
</li>
)}
{matches.map((city) => (
<li key={city.id} role="option" aria-selected={false}>
<button
type="button"
onMouseDown={(event) => event.preventDefault()}
onClick={() => select(city)}
className="w-full px-3 py-2 text-left text-sm text-[var(--sea-ink)] hover:bg-[var(--chip-bg)]"
>
{city.name}
</button>
</li>
))}
</ul>
)}
</div>
)
}

export default function Map() {
const [selectedCity, setSelectedCity] = useState<City | null>(null)

const { data, isPending, isError } = useQuery({
queryKey: ['cities'],
queryFn: () => graphqlFetch<{ cities: City[] }>(GET_CITIES),
})

return (
<section id="map">
<section id="map" className="relative">
<CitySearch
cities={data?.cities ?? []}
isLoading={isPending}
isError={isError}
onSelect={setSelectedCity}
/>
<MapContainer
center={[63.4305, 10.3951]}
center={DEFAULT_CENTER}
zoom={15}
scrollWheelZoom={false}
className="h-100 w-full rounded-2xl"
Expand All @@ -13,11 +201,19 @@ export default function Map() {
attribution='&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
/>
<Marker position={[63.4305, 10.3951]}>
<FlyToCity city={selectedCity} />
<Marker position={DEFAULT_CENTER}>
<Popup>
<span>Guinness!!!</span>
</Popup>
</Marker>
{selectedCity && (
<Marker position={[selectedCity.lat, selectedCity.lon]}>
<Popup>
<span>{selectedCity.name}</span>
</Popup>
</Marker>
)}
</MapContainer>
</section>
)
Expand Down