import { useMemo, useState } from "react"; import { useTranslation } from "react-i18next"; import { Sheet } from "react-modal-sheet"; import type { BusStopUsagePoint } from "~/api/schema"; interface StopUsageModalProps { isOpen: boolean; onClose: () => void; usage: BusStopUsagePoint[]; } export const StopUsageModal = ({ isOpen, onClose, usage, }: StopUsageModalProps) => { const { t } = useTranslation(); // Get current day of week (1=Monday, 7=Sunday) // JS getDay(): 0=Sunday, 1=Monday ... 6=Saturday const currentJsDay = new Date().getDay(); const initialDay = currentJsDay === 0 ? 7 : currentJsDay; const [selectedDay, setSelectedDay] = useState(initialDay); const days = [ { id: 1, label: t("days.monday") }, { id: 2, label: t("days.tuesday") }, { id: 3, label: t("days.wednesday") }, { id: 4, label: t("days.thursday") }, { id: 5, label: t("days.friday") }, { id: 6, label: t("days.saturday") }, { id: 7, label: t("days.sunday") }, ]; const filteredData = useMemo(() => { const data = usage.filter((u) => u.d === selectedDay); // Ensure all 24 hours are represented const fullDay = Array.from({ length: 24 }, (_, h) => { const match = data.find((u) => u.h === h); return { h, t: match?.t ?? 0 }; }); return fullDay; }, [usage, selectedDay]); const maxUsage = useMemo(() => { const max = Math.max(...filteredData.map((d) => d.t)); return max === 0 ? 1 : max; }, [filteredData]); // Use a square root scale to make smaller values more visible const getScaledHeight = (value: number) => { if (value <= 0) return 0; const scaledValue = Math.sqrt(value); const scaledMax = Math.sqrt(maxUsage); return (scaledValue / scaledMax) * 100; }; return (

{t("stop.usage_title", "Ocupación por horas")}

{days.map((day) => ( ))}
{/* Horizontal grid lines */}
{filteredData.map((data) => { const height = getScaledHeight(data.t); const isServiceHour = data.h >= 7 && data.h < 23; return (
{data.t > 0 && (
{data.t} {t("stop.usage_passengers", "pas.")}
)}
{data.h % 4 === 0 && ( {data.h}h )}
); })}
{/* Spacer for hour labels */}

{t( "stop.usage_scale_info", "Escala no lineal para resaltar valores bajos" )}

{t( "stop.usage_disclaimer", "Datos históricos aproximados de ocupación." )}

); };