-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
224 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
'use client' | ||
|
||
import dynamic from 'next/dynamic' | ||
|
||
const DynamicMap = dynamic(() => import('./map'), { | ||
ssr: false, | ||
}) | ||
|
||
export default function MapParent(props) { | ||
return <DynamicMap {...props} /> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
import Head from 'next/head' | ||
|
||
import { useEffect, useState } from 'react' | ||
import { | ||
getTavernPeople, | ||
getTavernEvents, | ||
type TavernPersonItem, | ||
type TavernEventItem, | ||
} from './tavern-utils' | ||
import MapParent from './map-parent' | ||
import { type LatLngExpression, DivIcon, Icon } from 'leaflet' | ||
import { MapContainer, TileLayer, Marker, useMap, Tooltip } from 'react-leaflet' | ||
|
||
const MAP_ZOOM = 11, | ||
MAP_CENTRE: LatLngExpression = [0, 0] | ||
|
||
export default function Map() { | ||
const [tavernPeople, setTavernPeople] = useState<TavernPersonItem[]>([]) | ||
const [tavernEvents, setTavernEvents] = useState<TavernEventItem[]>([]) | ||
|
||
// useEffect(() => { | ||
// async function foo() { | ||
// setTavernPeople(await getTavernPeople()) | ||
// setTavernEvents(await getTavernEvents()) | ||
// // Promise.all([getTavernPeople(), getTavernEvents()]).then(([tp, te]) => { | ||
// // setTavernPeople(tp) | ||
// // setTavernEvents(te) | ||
// // }) | ||
// } | ||
// foo() | ||
// }, []) | ||
|
||
return ( | ||
<div> | ||
{/* <Head> | ||
<link | ||
rel="stylesheet" | ||
href="https://unpkg.com/[email protected]/dist/leaflet.css" | ||
integrity="sha256-p4NxAoJBhIIN+hmNHrzRCf9tD/miZyoHS5obTRR9BMY=" | ||
crossOrigin="" | ||
/> | ||
</Head> */} | ||
|
||
{/* <MapParent tavernPeople={tavernPeople} tavernEvents={tavernEvents}> | ||
<MapContainer | ||
center={MAP_CENTRE} | ||
zoom={MAP_ZOOM} | ||
scrollWheelZoom={false} | ||
> | ||
<TileLayer | ||
attribution='© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors' | ||
url="https://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png" | ||
/> | ||
<TavernMarkers people={tavernPeople} events={tavernEvents} /> | ||
<UserLocation /> | ||
</MapContainer> | ||
</MapParent> */} | ||
</div> | ||
) | ||
} | ||
|
||
function UserLocation() { | ||
const map = useMap() | ||
|
||
useEffect(() => { | ||
if (navigator.geolocation) { | ||
navigator.geolocation.getCurrentPosition((loc) => { | ||
if (map !== null) { | ||
map.setView([loc.coords.latitude, loc.coords.longitude], 11) | ||
} | ||
}) | ||
} | ||
}, [map]) | ||
|
||
return null | ||
} | ||
|
||
function TavernMarkers(props: MapProps) { | ||
const map = useMap() | ||
|
||
if (!map) return null | ||
|
||
const peopleMarkers = props.people.map((t) => { | ||
const iconClass = `tavern-marker tavern-${!t.status ? 'default' : t.status}` | ||
|
||
const icon = new DivIcon({ | ||
className: iconClass, | ||
iconSize: [25, 25], | ||
}) | ||
|
||
return ( | ||
<Marker | ||
key={t.id} | ||
position={ | ||
t.coordinates.split(', ').map((c) => Number(c)) as LatLngExpression | ||
} | ||
icon={icon} | ||
/> | ||
) | ||
}) | ||
const eventMarkers = props.events | ||
.map((e) => { | ||
let iconUrl | ||
if (e.organizers.length === 0) { | ||
iconUrl = '/handraise.png' | ||
} else { | ||
iconUrl = '/tavern.png' | ||
} | ||
|
||
const icon = new Icon({ | ||
iconUrl, | ||
iconSize: [50, 50], | ||
}) | ||
|
||
const geocodeObj = JSON.parse(atob(e.geocode.slice(2).trim())) | ||
|
||
if (geocodeObj.o.status !== 'OK') { | ||
return null | ||
} | ||
|
||
return ( | ||
<Marker | ||
key={e.id} | ||
position={[geocodeObj.o.lat, geocodeObj.o.lng]} | ||
icon={icon} | ||
zIndexOffset={20} | ||
> | ||
<Tooltip>{e.city}</Tooltip> | ||
</Marker> | ||
) | ||
}) | ||
.filter((e) => e !== null) | ||
|
||
return [...peopleMarkers, ...eventMarkers] | ||
} | ||
|
||
type MapProps = { | ||
people: TavernPersonItem[] | ||
events: TavernEventItem[] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
'use server' | ||
|
||
import Airtable from 'airtable' | ||
|
||
Airtable.configure({ | ||
apiKey: process.env.AIRTABLE_API_KEY, | ||
endpointUrl: process.env.AIRTABLE_ENDPOINT_URL, | ||
}) | ||
|
||
type RsvpStatus = 'none' | 'organizer' | 'participant' | ||
export type TavernPersonItem = { | ||
id: string | ||
status: RsvpStatus | ||
coordinates: string | ||
} | ||
export type TavernEventItem = { | ||
id: string | ||
city: string | ||
geocode: string | ||
organizers: string[] | ||
} | ||
|
||
let cachedPeople: TavernPersonItem[] | null, | ||
cachedEvents: TavernEventItem[] | null | ||
let lastPeopleFetch = 0, | ||
lastEventsFetch = 0 | ||
const TTL = 5 * 60 * 1000 | ||
|
||
export const getTavernPeople = async () => { | ||
if (Date.now() - lastPeopleFetch < TTL) return cachedPeople | ||
|
||
console.log('Fetching tavern people') | ||
lastPeopleFetch = Date.now() | ||
const base = Airtable.base(process.env.BASE_ID!) | ||
const records = await base('people') | ||
.select({ | ||
fields: ['tavern_rsvp_status', 'tavern_map_coordinates'], | ||
filterByFormula: | ||
'AND({tavern_map_coordinates} != "", OR(tavern_rsvp_status != "", shipped_ship_count >= 1))', | ||
}) | ||
.all() | ||
|
||
return records.map((r) => ({ | ||
id: r.id, | ||
status: r.get('tavern_rsvp_status'), | ||
coordinates: r.get('tavern_map_coordinates'), | ||
})) as TavernPersonItem[] | ||
} | ||
|
||
export const getTavernEvents = async () => { | ||
if (Date.now() - lastEventsFetch < TTL) return cachedEvents | ||
|
||
console.log('Fetching tavern events') | ||
lastEventsFetch = Date.now() | ||
const base = Airtable.base(process.env.BASE_ID!) | ||
const records = await base('taverns') | ||
.select({ | ||
fields: ['city', 'map_geocode', 'organizers'], | ||
}) | ||
.all() | ||
|
||
return records.map((r) => ({ | ||
id: r.id, | ||
city: r.get('city'), | ||
geocode: r.get('map_geocode'), | ||
organizers: r.get('organizers') ?? [], | ||
})) as TavernEventItem[] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters