Spaces:
Running
Running
Download src/components/Sidebar.tsx from climateexploer/eurometeo: direct link, hf CLI and curl.
- Browser
- Download file 21.2 kB
-
https://huggingface.co/spaces/climateexploer/eurometeo/resolve/main/src/components/Sidebar.tsx
- Command line
-
hf download hf://spaces/climateexploer/eurometeo/src/components/Sidebar.tsx
-
curl -L -o Sidebar.tsx https://huggingface.co/spaces/climateexploer/eurometeo/resolve/main/src/components/Sidebar.tsx
21.2 kB
| import React, { useMemo, useState, useRef, useEffect } from "react"; | |
| import { | |
| Search, | |
| Calendar, | |
| CloudRain, | |
| Thermometer, | |
| Wind, | |
| Database, | |
| Info, | |
| ChevronDown, | |
| XCircle | |
| } from "lucide-react"; | |
| import { countryMatches } from "@/utils/country"; | |
| interface Station { | |
| id: string; | |
| name: string; | |
| country: string; | |
| longitude: number; | |
| latitude: number; | |
| elevation: number | null; | |
| available_params: string; | |
| } | |
| interface SidebarProps { | |
| stations: Station[]; | |
| selectedCountry: string; | |
| setSelectedCountry: (country: string) => void; | |
| selectedStation: string; | |
| setSelectedStation: (stationId: string) => void; | |
| startDate: string; | |
| setStartDate: (date: string) => void; | |
| endDate: string; | |
| setEndDate: (date: string) => void; | |
| parameter: string; | |
| setParameter: (param: string) => void; | |
| selectedHour: number; | |
| observations?: Record<string, number[]>; | |
| } | |
| const FilterLabel = ({ | |
| id, | |
| label, | |
| help, | |
| }: { | |
| id: string; | |
| label: string; | |
| help: string; | |
| }) => { | |
| const [isOpen, setIsOpen] = useState(false); | |
| const wrapperRef = useRef<HTMLDivElement>(null); | |
| useEffect(() => { | |
| if (!isOpen) return; | |
| const handlePointerDown = (event: MouseEvent | TouchEvent) => { | |
| if (wrapperRef.current && !wrapperRef.current.contains(event.target as Node)) { | |
| setIsOpen(false); | |
| } | |
| }; | |
| const handleKeyDown = (event: KeyboardEvent) => { | |
| if (event.key === "Escape") { | |
| setIsOpen(false); | |
| } | |
| }; | |
| document.addEventListener("mousedown", handlePointerDown); | |
| document.addEventListener("touchstart", handlePointerDown); | |
| document.addEventListener("keydown", handleKeyDown); | |
| return () => { | |
| document.removeEventListener("mousedown", handlePointerDown); | |
| document.removeEventListener("touchstart", handlePointerDown); | |
| document.removeEventListener("keydown", handleKeyDown); | |
| }; | |
| }, [isOpen]); | |
| const handleBlur = (event: React.FocusEvent<HTMLDivElement>) => { | |
| if (!event.currentTarget.contains(event.relatedTarget as Node | null)) { | |
| setIsOpen(false); | |
| } | |
| }; | |
| return ( | |
| <div | |
| ref={wrapperRef} | |
| onMouseEnter={() => setIsOpen(true)} | |
| onMouseLeave={() => setIsOpen(false)} | |
| onFocus={() => setIsOpen(true)} | |
| onBlur={handleBlur} | |
| className="relative w-full text-sm font-semibold text-slate-400 lg:text-slate-500 tracking-wider uppercase flex items-center gap-1.5" | |
| > | |
| <span>{label}</span> | |
| <button | |
| type="button" | |
| onClick={(event) => { | |
| event.preventDefault(); | |
| event.stopPropagation(); | |
| setIsOpen(true); | |
| }} | |
| aria-label={`Explain ${label}`} | |
| aria-expanded={isOpen} | |
| aria-controls={id} | |
| className="inline-flex min-h-[44px] min-w-[44px] lg:min-h-[24px] lg:min-w-[24px] items-center justify-center rounded-full text-slate-400 transition-colors hover:bg-white/10 lg:hover:bg-slate-100 hover:text-blue-500 focus:outline-none focus:ring-2 focus:ring-blue-500/20" | |
| > | |
| <Info size={12} /> | |
| </button> | |
| {isOpen && ( | |
| <div | |
| id={id} | |
| role="tooltip" | |
| className="absolute left-0 top-7 z-[70] w-full rounded-lg border border-white/20 bg-slate-800 text-slate-200 lg:border-slate-200 lg:bg-white lg:text-slate-600 px-3 py-2 text-xs font-medium leading-relaxed shadow-lg normal-case tracking-normal" | |
| > | |
| {help} | |
| </div> | |
| )} | |
| </div> | |
| ); | |
| }; | |
| // Custom Searchable Dropdown for Stations | |
| const SearchableStationSelect = ({ | |
| stations, | |
| selectedStation, | |
| setSelectedStation | |
| }: { | |
| stations: Station[], | |
| selectedStation: string, | |
| setSelectedStation: (id: string) => void | |
| }) => { | |
| const [isOpen, setIsOpen] = useState(false); | |
| const [search, setSearch] = useState(""); | |
| const dropdownRef = useRef<HTMLDivElement>(null); | |
| // Close when clicking outside or pressing Escape | |
| useEffect(() => { | |
| const handleClickOutside = (e: MouseEvent) => { | |
| if (dropdownRef.current && !dropdownRef.current.contains(e.target as Node)) { | |
| setIsOpen(false); | |
| } | |
| }; | |
| const handleKeyDown = (e: KeyboardEvent) => { | |
| if (e.key === "Escape" && isOpen) { | |
| setIsOpen(false); | |
| } | |
| }; | |
| document.addEventListener("mousedown", handleClickOutside); | |
| document.addEventListener("keydown", handleKeyDown); | |
| return () => { | |
| document.removeEventListener("mousedown", handleClickOutside); | |
| document.removeEventListener("keydown", handleKeyDown); | |
| }; | |
| }, [isOpen]); | |
| const selected = stations.find(s => s.id === selectedStation); | |
| const filtered = useMemo(() => { | |
| if (!search) return stations; | |
| const lower = search.toLowerCase(); | |
| return stations.filter(s => | |
| s.name.toLowerCase().includes(lower) || | |
| s.id.toLowerCase().includes(lower) || | |
| s.country.toLowerCase().includes(lower) | |
| ); | |
| }, [stations, search]); | |
| return ( | |
| <div className="relative" ref={dropdownRef}> | |
| <button | |
| type="button" | |
| onClick={() => setIsOpen(!isOpen)} | |
| className="w-full bg-white/10 border border-white/20 text-white lg:bg-white/70 lg:border-slate-200 rounded-xl pl-9 pr-8 py-2.5 text-base md:text-sm lg:text-slate-700 font-medium focus:outline-none focus:ring-2 focus:ring-blue-500/20 focus:border-blue-500 transition-all cursor-pointer text-left flex items-center justify-between" | |
| > | |
| <div className="truncate pr-2"> | |
| {selected ? `${selected.name} (${selected.country})` : <span className="text-slate-400">Select a station...</span>} | |
| </div> | |
| <ChevronDown size={15} className={`text-slate-400 transition-transform ${isOpen ? "rotate-180" : ""}`} /> | |
| </button> | |
| <Search className="absolute left-3.5 top-1/2 -translate-y-1/2 text-slate-400 pointer-events-none" size={15} /> | |
| {isOpen && ( | |
| <div className="absolute z-50 mt-1 w-full bg-slate-800/95 border-white/20 lg:bg-white/95 lg:border-slate-200 backdrop-blur-md border rounded-xl shadow-xl overflow-hidden flex flex-col"> | |
| <div className="p-2 border-b border-white/20 lg:border-slate-100"> | |
| <div className="relative"> | |
| <Search className="absolute left-3 top-1/2 -translate-y-1/2 text-slate-400" size={14} /> | |
| <input | |
| autoFocus | |
| type="text" | |
| placeholder="Search name, ID or country..." | |
| value={search} | |
| onChange={e => setSearch(e.target.value)} | |
| className="w-full pl-8 pr-3 py-1.5 text-base md:text-sm bg-white/10 border border-white/20 text-white placeholder:text-slate-500 lg:bg-slate-50 lg:border-slate-200 lg:text-slate-700 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500/20 focus:border-blue-500 transition-all" | |
| /> | |
| </div> | |
| </div> | |
| <div className="max-h-[250px] overflow-y-auto custom-scrollbar p-1"> | |
| {selectedStation && ( | |
| <button | |
| type="button" | |
| onClick={() => { | |
| setSelectedStation(""); | |
| setIsOpen(false); | |
| setSearch(""); | |
| }} | |
| className="w-full text-left px-3 py-2 text-sm rounded-lg transition-colors text-rose-300 hover:bg-white/10 lg:text-rose-600 lg:hover:bg-rose-50 font-semibold mb-1 border-b border-white/10 lg:border-slate-100 flex items-center justify-between" | |
| > | |
| <span>Clear Selection</span> | |
| <XCircle size={14} className="text-rose-400 lg:text-rose-500" /> | |
| </button> | |
| )} | |
| {filtered.length === 0 ? ( | |
| <div className="px-3 py-4 text-center text-sm text-slate-400">No stations found</div> | |
| ) : ( | |
| <> | |
| {!selectedStation && ( | |
| <button | |
| type="button" | |
| onClick={() => { | |
| setSelectedStation(""); | |
| setIsOpen(false); | |
| setSearch(""); | |
| }} | |
| className="w-full text-left px-3 py-2 text-sm rounded-lg transition-colors bg-blue-500/20 text-white lg:bg-blue-50 lg:text-blue-700 font-bold" | |
| > | |
| Clear Selection | |
| </button> | |
| )} | |
| {filtered.map(st => ( | |
| <button | |
| key={st.id} | |
| type="button" | |
| onClick={() => { | |
| setSelectedStation(st.id); | |
| setIsOpen(false); | |
| setSearch(""); | |
| }} | |
| className={`w-full text-left px-3 py-2 text-sm rounded-lg transition-colors ${ | |
| selectedStation === st.id ? "bg-blue-500/20 text-white lg:bg-blue-50 lg:text-blue-700 font-bold" : "text-slate-300 hover:bg-white/10 lg:text-slate-700 lg:hover:bg-slate-50" | |
| }`} | |
| > | |
| <div className="truncate font-medium">{st.name}</div> | |
| <div className="text-xs text-slate-500 lg:text-slate-400 flex justify-between"> | |
| <span>{st.id}</span> | |
| <span>{st.country}</span> | |
| </div> | |
| </button> | |
| ))} | |
| </> | |
| )} | |
| </div> | |
| </div> | |
| )} | |
| </div> | |
| ); | |
| }; | |
| export const Sidebar: React.FC<SidebarProps> = ({ | |
| stations, | |
| selectedCountry, | |
| setSelectedCountry, | |
| selectedStation, | |
| setSelectedStation, | |
| startDate, | |
| setStartDate, | |
| endDate, | |
| setEndDate, | |
| parameter, | |
| setParameter, | |
| selectedHour, | |
| observations | |
| }) => { | |
| // Extract unique countries sorted alphabetically | |
| const countries = useMemo(() => { | |
| const unique = new Set(stations.map(st => st.country).filter(Boolean)); | |
| return Array.from(unique).sort(); | |
| }, [stations]); | |
| // Filter stations based on selected country for the station dropdown | |
| const filteredStations = useMemo(() => { | |
| if (!selectedCountry) return stations; | |
| return stations.filter(st => countryMatches(st.country, selectedCountry)); | |
| }, [stations, selectedCountry]); | |
| // Count stations that have actual valid observations for the selected hour | |
| const activeCount = useMemo(() => { | |
| if (!observations || Object.keys(observations).length === 0) { | |
| return filteredStations.length; | |
| } | |
| let count = 0; | |
| filteredStations.forEach((st) => { | |
| const val = observations[st.id]?.[selectedHour]; | |
| if (val !== undefined && val !== null && !isNaN(val)) { | |
| count++; | |
| } | |
| }); | |
| return count; | |
| }, [filteredStations, observations, selectedHour]); | |
| const hoursAgo = useMemo(() => { | |
| const selectedDateStr = `${endDate}T${selectedHour.toString().padStart(2, "0")}:00:00Z`; | |
| const selectedTime = new Date(selectedDateStr).getTime(); | |
| const now = new Date().getTime(); | |
| const diff = now - selectedTime; | |
| return Math.max(0, Math.floor(diff / (1000 * 60 * 60))); | |
| }, [endDate, selectedHour]); | |
| // Date range constraints (max 31 days) | |
| const handleStartDateChange = (newStart: string) => { | |
| setStartDate(newStart); | |
| const startObj = new Date(newStart); | |
| const endObj = new Date(endDate); | |
| // Auto-adjust end date if range exceeds 31 days | |
| if (endObj.getTime() - startObj.getTime() > 31 * 24 * 60 * 60 * 1000) { | |
| const newEnd = new Date(startObj.getTime() + 31 * 24 * 60 * 60 * 1000); | |
| // Ensure we don't set future dates | |
| const maxDate = new Date(); | |
| if (newEnd > maxDate) { | |
| setEndDate(maxDate.toISOString().split("T")[0]); | |
| } else { | |
| setEndDate(newEnd.toISOString().split("T")[0]); | |
| } | |
| } else if (startObj > endObj) { | |
| setEndDate(newStart); | |
| } | |
| }; | |
| const handleEndDateChange = (newEnd: string) => { | |
| setEndDate(newEnd); | |
| const startObj = new Date(startDate); | |
| const endObj = new Date(newEnd); | |
| // Auto-adjust start date if range exceeds 31 days | |
| if (endObj.getTime() - startObj.getTime() > 31 * 24 * 60 * 60 * 1000) { | |
| const newStart = new Date(endObj.getTime() - 31 * 24 * 60 * 60 * 1000); | |
| setStartDate(newStart.toISOString().split("T")[0]); | |
| } else if (startObj > endObj) { | |
| setStartDate(newEnd); | |
| } | |
| }; | |
| return ( | |
| <aside className="w-[280px] h-full shrink-0 glass-sidebar flex flex-col p-5 lg:p-6 bg-slate-950/92 text-white backdrop-blur-md lg:bg-white/90 lg:text-slate-800 lg:backdrop-blur-none"> | |
| {/* Header Branding */} | |
| <div className="flex flex-col gap-1 mt-2 pr-12 lg:pr-0"> | |
| <h2 className="text-lg font-bold tracking-tight text-white lg:text-slate-800"> | |
| Filters | |
| </h2> | |
| </div> | |
| {/* Scrollable Filters Block */} | |
| <div className="flex-1 mt-6 overflow-y-auto overscroll-contain custom-scrollbar pr-1 flex flex-col gap-6"> | |
| {/* Zoom to Country */} | |
| <div className="flex flex-col gap-2"> | |
| <FilterLabel | |
| id="filter-help-country" | |
| label="Zoom to Country" | |
| help="Filters the station list to the selected country and moves the map to that country's station area. All Countries shows every available station." | |
| /> | |
| <div className="relative"> | |
| <select | |
| value={selectedCountry} | |
| onChange={(e) => { | |
| setSelectedCountry(e.target.value); | |
| setSelectedStation(""); // Reset station on country change | |
| }} | |
| className="w-full bg-white/10 border border-white/20 text-white lg:bg-white/70 lg:border-slate-200 rounded-xl px-3.5 py-2.5 text-base md:text-sm lg:text-slate-700 font-medium focus:outline-none focus:ring-2 focus:ring-blue-500/20 focus:border-blue-500 transition-all cursor-pointer appearance-none" | |
| > | |
| <option value="" className="bg-white text-slate-700">All Countries</option> | |
| {countries.map((country) => ( | |
| <option key={country} value={country} className="bg-white text-slate-700"> | |
| {country} | |
| </option> | |
| ))} | |
| </select> | |
| <div className="absolute right-3.5 top-1/2 -translate-y-1/2 pointer-events-none border-l-4 border-r-4 border-t-4 border-transparent border-t-slate-400 w-0 h-0" /> | |
| </div> | |
| </div> | |
| {/* Find Station */} | |
| <div className="flex flex-col gap-2"> | |
| <FilterLabel | |
| id="filter-help-station" | |
| label="Find Station" | |
| help="Searches by station name, WIGOS ID, or country. Selecting a station focuses it on the map and loads its detailed observation dashboard." | |
| /> | |
| <SearchableStationSelect | |
| stations={filteredStations} | |
| selectedStation={selectedStation} | |
| setSelectedStation={setSelectedStation} | |
| /> | |
| {selectedStation && ( | |
| <button | |
| type="button" | |
| onClick={() => setSelectedStation("")} | |
| className="w-full mt-1.5 flex items-center justify-center gap-1.5 px-3 py-2 text-xs font-semibold rounded-xl border transition-all cursor-pointer bg-rose-500/15 border-rose-500/30 text-rose-200 hover:bg-rose-500/25 active:scale-[0.99] lg:bg-rose-50 lg:border-rose-200 lg:text-rose-700 lg:hover:bg-rose-100" | |
| > | |
| <XCircle size={14} className="shrink-0 text-rose-400 lg:text-rose-600" /> | |
| <span>Deselect Station</span> | |
| </button> | |
| )} | |
| </div> | |
| {/* Date Picker Range */} | |
| <div className="flex flex-col gap-2"> | |
| <FilterLabel | |
| id="filter-help-period" | |
| label="Select Period" | |
| help="Sets the observation date range used for station logs, charts, and map values. The range is limited to 31 days and cannot go into the future." | |
| /> | |
| <div className="flex flex-col gap-2"> | |
| <div className="relative"> | |
| <input | |
| type="date" | |
| value={startDate} | |
| min="2026-03-05" | |
| max={endDate || new Date().toISOString().split("T")[0]} | |
| onChange={(e) => handleStartDateChange(e.target.value)} | |
| className="w-full bg-white/10 border border-white/20 text-white lg:bg-white/70 lg:border-slate-200 rounded-xl pl-9 pr-3.5 py-2 text-base md:text-sm lg:text-slate-700 font-medium focus:outline-none focus:ring-2 focus:ring-blue-500/20 focus:border-blue-500 transition-all cursor-pointer" | |
| /> | |
| <Calendar className="absolute left-3.5 top-1/2 -translate-y-1/2 text-slate-400" size={15} /> | |
| </div> | |
| <div className="relative"> | |
| <input | |
| type="date" | |
| value={endDate} | |
| min={startDate || "2026-03-05"} | |
| max={new Date().toISOString().split("T")[0]} | |
| onChange={(e) => handleEndDateChange(e.target.value)} | |
| className="w-full bg-white/10 border border-white/20 text-white lg:bg-white/70 lg:border-slate-200 rounded-xl pl-9 pr-3.5 py-2 text-base md:text-sm lg:text-slate-700 font-medium focus:outline-none focus:ring-2 focus:ring-blue-500/20 focus:border-blue-500 transition-all cursor-pointer" | |
| /> | |
| <Calendar className="absolute left-3.5 top-1/2 -translate-y-1/2 text-slate-400" size={15} /> | |
| </div> | |
| </div> | |
| </div> | |
| {/* Weather Parameter Selector */} | |
| <div className="flex flex-col gap-2"> | |
| <FilterLabel | |
| id="filter-help-parameter" | |
| label="Map Parameter" | |
| help="Chooses the weather variable shown by marker colors on the map. From the Dashboard, selecting a parameter returns to the initial broad Map View so the spatial distribution is visible." | |
| /> | |
| <div className="flex flex-col gap-1.5"> | |
| {[ | |
| { id: "air_temperature", label: "Air Temperature", icon: Thermometer, color: "text-rose-300 bg-rose-500/20 border-rose-400/30 lg:text-rose-500 lg:bg-rose-50 lg:border-rose-100" }, | |
| { id: "precipitation_amount", label: "Precipitation", icon: CloudRain, color: "text-blue-300 bg-blue-500/20 border-blue-400/30 lg:text-blue-500 lg:bg-blue-50 lg:border-blue-100" }, | |
| { id: "air_pressure_at_mean_sea_level", label: "Sea Level Pressure", icon: Database, color: "text-emerald-300 bg-emerald-500/20 border-emerald-400/30 lg:text-emerald-500 lg:bg-emerald-50 lg:border-emerald-100" }, | |
| { id: "wind_speed", label: "Wind Speed", icon: Wind, color: "text-amber-300 bg-amber-500/20 border-amber-400/30 lg:text-amber-500 lg:bg-amber-50 lg:border-amber-100" } | |
| ].map((item) => { | |
| const Icon = item.icon; | |
| const isSelected = parameter === item.id; | |
| return ( | |
| <button | |
| key={item.id} | |
| onClick={() => setParameter(item.id)} | |
| className={`flex items-center gap-3 px-3.5 py-3 rounded-xl border text-sm font-medium transition-all text-left w-full cursor-pointer ${ | |
| isSelected | |
| ? `${item.color} shadow-sm border-blue-200 ring-2 ring-blue-500/10` | |
| : "border-white/20 text-slate-300 hover:border-white/40 hover:bg-white/10 lg:border-slate-100 lg:hover:border-slate-200 lg:hover:bg-slate-50 lg:text-slate-600" | |
| }`} | |
| > | |
| <Icon size={18} className={isSelected ? "" : "text-slate-400"} /> | |
| {item.label} | |
| </button> | |
| ); | |
| })} | |
| </div> | |
| </div> | |
| {/* Footer info & stats — inside scroll area so it scrolls on mobile */} | |
| <div className="mt-auto border-t border-white/20 lg:border-slate-100 flex flex-col gap-3"> | |
| <div className="bg-white/5 border border-white/20 text-slate-300 lg:bg-slate-50 lg:border-slate-100 lg:text-slate-600 rounded-xl p-4 flex flex-col gap-1.5 text-sm font-medium"> | |
| <div><span className="font-bold text-white lg:text-slate-800">{stations.length}</span> stations found</div> | |
| <div className="text-blue-400 lg:text-blue-600 font-semibold border-b border-white/20 lg:border-slate-200 pb-2 mb-1">Showing: {activeCount} stations</div> | |
| <div className="text-white lg:text-slate-800 font-bold">{endDate} {selectedHour.toString().padStart(2, "0")}:00 UTC</div> | |
| <div className="text-slate-400 lg:text-slate-500 text-xs">{hoursAgo} hour(s) ago</div> | |
| </div> | |
| <div className="bg-blue-500/10 border border-blue-400/30 text-slate-300 lg:bg-blue-50/50 lg:border-blue-100/50 lg:text-slate-600 rounded-xl p-3 flex gap-2 items-start text-xs font-medium"> | |
| <Info size={14} className="text-blue-500 shrink-0 mt-0.5" /> | |
| <p>Tip: Click map points to view detailed weather plots.</p> | |
| </div> | |
| </div> | |
| </div> | |
| </aside> | |
| ); | |
| }; | |