Refactor, fixed search

This commit is contained in:
2023-09-15 09:08:33 -04:00
parent 4114d533b2
commit 128181bed6
28 changed files with 343 additions and 342 deletions

View File

@@ -0,0 +1,133 @@
'use client';
import { getAirports } from '@/app/_api/airport';
import { Airport } from '@/app/_api/airport.types';
import { getMetars } from '@/app/_api/metar';
import { Metar } from '@/app/_api/metar.types';
import { FaLocationPin } from 'react-icons/fa6';
import { DivIcon, LatLngBounds } from 'leaflet';
import { useEffect, useState } from 'react';
import ReactDOMServer from 'react-dom/server';
import { Marker, TileLayer, Tooltip, useMap, useMapEvents } from 'react-leaflet';
import MetarDialog from './MetarDialog';
export default function MapTiles() {
const [isOpen, setIsOpen] = useState(false);
const [airports, setAirports] = useState<Airport[]>([]);
const [selectedAirport, setSelectedAirport] = useState<Airport | undefined>();
const [zoomLevel, setZoomLevel] = useState(8);
// const [dragging, setDragging] = useState(false);
const map = useMap();
const mapEvents = useMapEvents({
zoomend: async () => {
setZoomLevel(mapEvents.getZoom());
await updateAirports(mapEvents.getBounds());
},
movestart: () => {
// setDragging(true);
},
moveend: async () => {
// setDragging(false);
await updateAirports(mapEvents.getBounds());
}
});
function handleOpen(airport: Airport) {
setSelectedAirport(airport);
setIsOpen(true);
}
async function updateAirports(bounds: LatLngBounds) {
const ne = bounds.getNorthEast();
const sw = bounds.getSouthWest();
const { data: _airports } = await getAirports({
bounds: {
northEast: { lat: ne.lat, lon: ne.lng },
southWest: { lat: sw.lat, lon: sw.lng }
},
limit: 100,
page: 1
});
const { data: metars } = await getMetars(_airports);
metars.forEach((metar) => {
_airports.forEach((airport) => {
if (metar.station_id == airport.icao) {
airport.metar = metar;
}
});
});
setAirports(_airports);
}
function metarTextColor(metar: Metar | undefined) {
if (metar?.flight_category == 'VFR') {
return 'text-emerald-700';
} else if (metar?.flight_category == 'MVFR') {
return 'text-blue-700';
} else if (metar?.flight_category == 'IFR') {
return 'text-red-700';
} else if (metar?.flight_category == 'LIFR') {
return 'text-purple-700';
} else {
return 'text-black/50';
}
}
function iconSize() {
if (zoomLevel <= 4) {
return 'text-xs';
} else if (zoomLevel <= 5) {
return 'text-sm';
} else if (zoomLevel <= 6) {
return 'text-base';
} else if (zoomLevel <= 7) {
return 'text-lg';
} else if (zoomLevel <= 9) {
return 'text-2xl';
} else if (zoomLevel <= 11) {
return 'text-3xl';
} else if (zoomLevel >= 12) {
return 'text-4xl';
}
}
function icon(airport: Airport) {
return new DivIcon({
html: ReactDOMServer.renderToString(
<FaLocationPin className={`${iconSize()} ${metarTextColor(airport.metar)}`} />
),
className: 'metar-marker-icon'
});
}
useEffect(() => {
updateAirports(map.getBounds());
}, []);
return (
<>
{selectedAirport && <MetarDialog isOpen={isOpen} onClose={() => setIsOpen(false)} airport={selectedAirport} />}
<TileLayer
attribution='&copy; <a href="https://www.osm.org/copyright">OpenStreetMap</a> contributors'
url='http://{s}.tile.osm.org/{z}/{x}/{y}.png'
/>
{airports.map((airport) => (
<Marker
key={airport.icao}
position={[airport.point.y, airport.point.x]}
icon={icon(airport)}
eventHandlers={{
click: () => handleOpen(airport)
}}
>
{!isOpen && (
<Tooltip className='metar-tooltip' direction='top' offset={[5, -5]} opacity={1}>
<b>{airport.icao}</b> - {airport.full_name}
</Tooltip>
)}
</Marker>
))}
</>
);
}

View File

@@ -0,0 +1,73 @@
import { Airport } from '@/app/_api/airport.types';
import { Metar } from '@/app/_api/metar.types';
import { FaArrowsSpin, FaLocationArrow } from 'react-icons/fa6';
import { Modal } from 'antd';
interface MetarDialogProps {
airport: Airport;
isOpen: boolean;
onClose(): void;
}
export default function MetarDialog({ airport, isOpen, onClose }: MetarDialogProps) {
function metarBGColor(metar: Metar | undefined) {
if (metar?.flight_category == 'VFR') {
return 'bg-emerald-600';
} else if (metar?.flight_category == 'MVFR') {
return 'bg-blue-600';
} else if (metar?.flight_category == 'IFR') {
return 'bg-red-600';
} else if (metar?.flight_category == 'LIFR') {
return 'bg-purple-600';
} else {
return 'bg-black';
}
}
function windColor(metar: Metar | undefined) {
if (Number(metar?.wind_speed_kt) <= 9) {
return 'bg-green-300';
} else if (Number(metar?.wind_speed_kt) > 9) {
return 'bg-orange-300';
} else if (Number(metar?.wind_speed_kt) > 12) {
return 'bg-red-300';
}
}
return (
<Modal title={`${airport.icao} ${airport.full_name}`} open={isOpen} onCancel={onClose} closable={false} footer={[]}>
<div className='min-w-0 flex-1 select-none'>
<hr />
<p className='text-sm font-medium text-gray-500'>{airport.metar?.raw_text}</p>
<div className='mt-2 flex'>
<span
className={`flex inline-block text-sm text-white ${metarBGColor(airport.metar)} py-2 px-4 rounded-full`}
>
{airport.metar?.flight_category ? airport.metar?.flight_category : 'UNKN'}
</span>
<div className='flex inline-block px-2'>
<span className={`text-sm text-black ${windColor(airport.metar)} py-2 px-3 rounded-full`}>
{airport.metar && airport.metar.wind_dir_degrees && Number(airport.metar.wind_dir_degrees) > 0 ? (
<FaLocationArrow
className='align-middle'
style={{ rotate: `${-45 + 180 + Number(airport.metar.wind_dir_degrees)}deg` }}
/>
) : (
<></>
)}
{airport.metar && airport.metar.wind_dir_degrees && airport.metar.wind_dir_degrees == 'VRB' ? (
<FaArrowsSpin className='align-middle' />
) : (
<></>
)}
<span className='align-middle pl-1.5'>
{airport.metar?.wind_speed_kt != undefined && airport.metar?.wind_speed_kt > 0
? `${airport.metar?.wind_speed_kt} KT`
: 'CALM'}
</span>
</span>
</div>
</div>
</div>
</Modal>
);
}

View File

@@ -0,0 +1,23 @@
'use client';
import { MapContainer } from 'react-leaflet';
import MapTiles from './MapTiles';
export default function Map({ className = '' }: { className?: string }) {
return (
<>
<MapContainer
center={[38.7209, -77.5133]}
zoom={8}
maxZoom={12}
minZoom={1}
id='map-container'
style={{ height: '94.5vh' }}
className={`${className} overflow-y-hidden overflow-x-hidden`}
attributionControl={false}
>
<MapTiles />
</MapContainer>
</>
);
}

View File

@@ -0,0 +1,16 @@
import { Metar } from '@/app/_api/metar.types';
import dynamic from 'next/dynamic';
export default async function Metar({ className = '' }: { className?: string }) {
const Map = dynamic(() => import('@/app/_components/Metars/MetarMap'), {
loading: () => (
<div className='grid min-h-full place-items-center px-6 py-24 sm:py-32 lg:px-8'>
<div className='text-center'>
<p className='mt-4 text-3xl font-bold tracking-tight text-gray-300 sm:text-5xl'>Loading...</p>
</div>
</div>
),
ssr: false
});
return <Map className={className} />;
}