"use client";
import { TimeControl, Tournament } from "@/types";
import { LatLngLiteral } from "leaflet";
import {
MapContainer,
TileLayer,
LayerGroup,
useMapEvent,
} from "react-leaflet";
import { FaChevronDown } from "react-icons/fa";
import { useAtomValue, useSetAtom } from "jotai";
import "leaflet/dist/leaflet.css";
import "leaflet-defaulticon-compatibility/dist/leaflet-defaulticon-compatibility.css";
import "leaflet-defaulticon-compatibility";
import {
filteredTournamentsByTimeControlAtom,
mapBoundsAtom,
} from "@/app/atoms";
import Legend from "./Legend";
import { TournamentMarker } from "./TournamentMarker";
import TimeControlFilters from "./TimeControlFilters";
const MapEvents = () => {
const setMapBounds = useSetAtom(mapBoundsAtom);
const map = useMapEvent("moveend", () => {
setMapBounds(map.getBounds());
});
return null;
};
export default function TournamentMap() {
const tournaments = useAtomValue(filteredTournamentsByTimeControlAtom);
const setMapBounds = useSetAtom(mapBoundsAtom);
const center: LatLngLiteral = { lat: 47.0844, lng: 2.3964 };
const onScrollToTable = () => {
const tournamentTable = document.getElementById("tournament-table");
tournamentTable?.scrollIntoView({ behavior: "smooth" });
};
const createLayerGroups = (
timeControl: TimeControl,
colour: string,
tournaments: Tournament[]
) => {
const filteredTournaments = tournaments.filter(
(t) => t.timeControl === timeControl
);
const layerGroup = filteredTournaments.map((tournament) => {
return (
);
});
return {layerGroup};
};
const classicalMarkers = createLayerGroups(
TimeControl.Classic,
"green",
tournaments
);
const rapidMarkers = createLayerGroups(
TimeControl.Rapid,
"blue",
tournaments
);
const blitzMarkers = createLayerGroups(
TimeControl.Blitz,
"yellow",
tournaments
);
const otherMarkers = createLayerGroups(TimeControl.KO, "red", tournaments);
return (
{
if (map) {
setMapBounds(map.getBounds());
}
}}
>
{classicalMarkers}
{rapidMarkers}
{blitzMarkers}
{otherMarkers}
);
}