-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1105 from hackclub/malted/tavern-map
Add tavern map to shipyard
- Loading branch information
Showing
8 changed files
with
272 additions
and
39 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,186 @@ | ||
'use client' | ||
|
||
import { useEffect, useState } from 'react' | ||
import { | ||
getTavernPeople, | ||
getTavernEvents, | ||
type TavernPersonItem, | ||
type TavernEventItem, | ||
} from './tavern-utils' | ||
import { type LatLngExpression, DivIcon, Icon } from 'leaflet' | ||
import { MapContainer, TileLayer, Marker, useMap, Tooltip } from 'react-leaflet' | ||
import 'leaflet/dist/leaflet.css' | ||
import { Card } from '@/components/ui/card' | ||
|
||
const MAP_ZOOM = 11, | ||
MAP_CENTRE: LatLngExpression = [0, 0] | ||
|
||
export default function Map() { | ||
const [tavernPeople, setTavernPeople] = useState<TavernPersonItem[]>([]) | ||
const [tavernEvents, setTavernEvents] = useState<TavernEventItem[]>([]) | ||
|
||
useEffect(() => { | ||
Promise.all([getTavernPeople(), getTavernEvents()]).then(([tp, te]) => { | ||
setTavernPeople(tp) | ||
setTavernEvents(te) | ||
}) | ||
}, []) | ||
|
||
return ( | ||
<div> | ||
<MapContainer | ||
className="h-96 rounded-lg" | ||
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> | ||
<Card className="mt-8 p-3 flex flex-row justify-center items-center gap-5 flex-wrap"> | ||
<p className="w-full text-center">Map Legend</p> | ||
<div className="flex flex-row justify-start items-center gap-2"> | ||
<img | ||
src="/tavern.png" | ||
alt="a star representing a tavern" | ||
width={20} | ||
className="inline-block ml-4 hidden sm:inline" | ||
/> | ||
<p>Mystic Tavern</p> | ||
</div> | ||
<div className="flex flex-row justify-start items-center gap-2"> | ||
<img | ||
src="/handraise.png" | ||
alt="someone raising a hand" | ||
width={20} | ||
className="inline-block ml-4 hidden sm:inline" | ||
/> | ||
<p>Mystic Tavern without organizer</p> | ||
</div> | ||
<div className="flex flex-row justify-start items-center gap-2"> | ||
<div className="h-7 w-7 rounded-full border-2 border-white bg-[#cfdfff]"></div> | ||
<p>Someone unable to organize or attend</p> | ||
</div> | ||
<div className="flex flex-row justify-start items-center gap-2"> | ||
<div className="h-7 w-7 rounded-full border-2 border-white bg-[#ffd66e]"></div> | ||
<p>Someone able to organize</p> | ||
</div> | ||
<div className="flex flex-row justify-start items-center gap-2"> | ||
<div className="h-7 w-7 rounded-full border-2 border-white bg-[#f82b60]"></div> | ||
<p>Someone able to attend</p> | ||
</div> | ||
<div className="flex flex-row justify-start items-center gap-2"> | ||
<div className="h-7 w-7 rounded-full border-2 border-white bg-[#666666]"></div> | ||
<p>Someone who has not responded</p> | ||
</div> | ||
</Card> | ||
</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) => { | ||
let iconClass = `rounded-full border-2 border-white w-full h-full ` | ||
|
||
switch (t.status) { | ||
case 'none': { | ||
iconClass += 'bg-[#cfdfff]' | ||
break | ||
} | ||
case 'organizer': { | ||
iconClass += 'bg-[#ffd66e]' | ||
break | ||
} | ||
case 'participant': { | ||
iconClass += 'bg-[#f82b60]' | ||
break | ||
} | ||
default: { | ||
iconClass += 'bg-[#666666]' | ||
break | ||
} | ||
} | ||
|
||
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) => { | ||
if (!e.geocode) { | ||
return null | ||
} | ||
|
||
const geocodeObj = JSON.parse(atob(e.geocode.slice(2).trim())) | ||
|
||
if (geocodeObj.o.status !== 'OK') { | ||
return null | ||
} | ||
|
||
let iconUrl | ||
if (e.organizers.length === 0) { | ||
iconUrl = '/handraise.png' | ||
} else { | ||
iconUrl = '/tavern.png' | ||
} | ||
|
||
const icon = new Icon({ | ||
iconUrl, | ||
iconSize: [50, 50], | ||
}) | ||
|
||
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,75 @@ | ||
'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 = 30 * 60 * 1000 | ||
|
||
export const getTavernPeople = async () => { | ||
if (Date.now() - lastPeopleFetch < TTL) return cachedPeople | ||
|
||
console.log('Fetching tavern people') | ||
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() | ||
|
||
const items = records.map((r) => ({ | ||
id: r.id, | ||
status: r.get('tavern_rsvp_status'), | ||
coordinates: r.get('tavern_map_coordinates'), | ||
})) as TavernPersonItem[] | ||
|
||
cachedPeople = items | ||
lastPeopleFetch = Date.now() | ||
|
||
return items | ||
} | ||
|
||
export const getTavernEvents = async () => { | ||
if (Date.now() - lastEventsFetch < TTL) return cachedEvents | ||
|
||
console.log('Fetching tavern events') | ||
const base = Airtable.base(process.env.BASE_ID!) | ||
const records = await base('taverns') | ||
.select({ | ||
fields: ['city', 'map_geocode', 'organizers'], | ||
}) | ||
.all() | ||
|
||
const items = records.map((r) => ({ | ||
id: r.id, | ||
city: r.get('city'), | ||
geocode: r.get('map_geocode'), | ||
organizers: r.get('organizers') ?? [], | ||
})) as TavernEventItem[] | ||
|
||
cachedEvents = items | ||
lastEventsFetch = Date.now() | ||
return items | ||
} |
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