'use client';
import { getAirports } from "@/api/airport";
import { Airport } from "@/api/airport.types";
import { useEffect, useState } from "react";
import { useRecoilState, useRecoilValue } from "recoil";
import { Autocomplete, Badge, Box, Button, Card, Grid, Group, SimpleGrid, Text, Title } from "@mantine/core";
import classes from './profile.module.css';
import { addFavorite, getFavorites, removeFavorite } from "@/api/users";
import { getMetars } from "@/api/metar";
import { Metar } from "@/api/metar.types";
import { MdLocationSearching } from 'react-icons/md';
import { useRouter } from "next/navigation";
import { coordinatesState } from "@/state/map";
import { userState } from "@/state/auth";
export default function Page() {
const user = useRecoilValue(userState);
return (
{user?.first_name} {user?.last_name}
);
}
function TopSection() {
const [airports, setAirports] = useState([]);
const [metars, setMetars] = useState([]);
const [search, setSearch] = useState('');
const [searchAirports, setSearchAirports] = useState([]);
const router = useRouter();
const [_, setCoordinates] = useRecoilState(coordinatesState);
useEffect(() => {
updateFavorites();
}, []);
function metarColor(metar?: Metar): string {
switch (metar?.flight_category) {
case 'VFR':
return 'green';
case 'MVFR':
return 'blue';
case 'IFR':
return 'red';
case 'LIFR':
return 'purple';
default:
return 'gray';
}
}
function AirportCard(airport: Airport) {
let metar = metars.find((m) => m.station_id === airport.icao);
let color = metarColor(metar);
let text = metar?.flight_category || 'UNKN';
return (
{airport.name}{text} {
setCoordinates({
lat: airport.latitude,
lon: airport.longitude,
});
router.push('/');
}}>
{airport.latitude.toFixed(3)}, {airport.longitude.toFixed(3)}
);
}
async function updateFavorites() {
const favorites = await getFavorites();
const m = (await getMetars(favorites)).data;
setMetars(m);
const a = (await getAirports({ icaos: favorites })).data;
setAirports(a);
}
return (
Logbook
Your logbook is a list of your flights. You can add flights to your logbook by clicking the "Add to logbook" button on the flight page.
Saved Airports
({ value: a.icao, label: `${a.icao} - ${a.name}` }))}
limit={5}
style={{ paddingBottom: '10px' }}
onChange={async (value) => {
setSearch(value);
if (value) {
const a = await getAirports({ icaos: [value], name: value });
setSearchAirports(a.data);
}
}}
onOptionSubmit={async (value) => {
if (!airports.find((a) => a.icao === value)) {
await addFavorite(value);
await updateFavorites();
}
setSearch('');
}}
/>
{airports.map((airport) => AirportCard(airport))}