import { compassPointOf, type PaddleLakeStyle, type WindForecastResponse, type WindHour } from '@trek/shared'; import { ChevronDown, RefreshCw, WifiOff, Wind } from 'lucide-react'; import { useState } from 'react'; import { useTranslation } from '../../../i18n'; import { formatDate } from '../../../utils/formatters'; import { SoftPill } from '../../Planner/planParts'; import { fs } from '../../shared/DialogShell'; import { Tooltip } from '../../shared/Tooltip'; import { RailCard, TourNotice } from '../tourParts'; import { FigureBadge } from './FigureBadge'; import { windForShore } from './lakeStyleAdvice'; import { useTourWind, type TourWind } from './useTourWind'; type Translate = (key: string, params?: Record) => string; /** The hours a day tour is out on the water, as the strip shows them. */ const FIRST_HOUR = 6; const LAST_HOUR = 21; /** * An arrow the way the wind blows: meteorological degrees name where it comes * from, so the arrow turns half round from them. */ function FlowArrow({ from, size }: { from: number; size: number }) { // Drawn about its own centre, so every hour's arrow turns in place at the same height. return ( ); } function directionName(degrees: number | null, t: Translate): string | null { return degrees === null ? null : t(`tours.paddle.wind.dir.${compassPointOf(degrees)}`); } /** * The day's hours as columns: the mean wind as a bar, the gusts as the paler * bar behind it, an arrow the way it blows on top. An hour the source left * out stays an empty column, never a calm one. */ function HourStrip({ hours, t }: { hours: readonly WindHour[]; t: Translate }) { const shown = hours.filter((hour) => hour.hour >= FIRST_HOUR && hour.hour <= LAST_HOUR); const top = Math.max(10, ...shown.map((hour) => hour.gusts_kmh ?? hour.speed_kmh ?? 0)); const height = (value: number | null) => `${Math.max(4, ((value ?? 0) / top) * 100)}%`; return (
{shown.map((hour) => { const from = hour.direction_deg; const known = hour.speed_kmh !== null; const label = known ? t('tours.paddle.wind.hourInfo', { hour: hour.hour, speed: hour.speed_kmh, gusts: hour.gusts_kmh ?? '–', direction: directionName(from, t) ?? '–', }) : t('tours.paddle.wind.hourUnknown', { hour: hour.hour }); return ( {from !== null && } {hour.gusts_kmh !== null && ( )} {known && ( )} {hour.hour} ); })}
); } /** The forecast of the chosen day: its figures, its hours and where and when it was asked. */ function Forecast({ forecast, t, locale }: { forecast: WindForecastResponse; t: Translate; locale: string }) { const day = forecast.day; const from = day?.direction_dominant_deg ?? null; const fetched = new Date(forecast.fetched_at).toLocaleTimeString(locale, { hour: '2-digit', minute: '2-digit' }); return (
{from !== null && ( } info={t('tours.paddle.wind.fromInfo', { direction: directionName(from, t) ?? '' })} /> )}
{t('tours.paddle.wind.legendMean')} {t('tours.paddle.wind.legendGusts')}

{t('tours.paddle.wind.source', { source: forecast.source, time: fetched })}

); } /** Why there is no forecast to show, each case in its own words, with a way to ask again after a failure. */ function Unavailable({ wind, t, locale }: { wind: TourWind; t: Translate; locale: string }) { const { state } = wind; if (state.kind === 'loading') return
; if (state.kind === 'error') return ( }> {t('tours.paddle.wind.error')} ); if (state.kind === 'offline') return }>{t('tours.paddle.wind.offline')}; if (state.kind === 'ready' && state.forecast.status === 'beyond_horizon') { const from = new Date(Date.parse(state.forecast.date) - (state.forecast.horizon_days - 1) * 86_400_000); return ( }> {t('tours.paddle.wind.beyond', { date: formatDate(from.toISOString().slice(0, 10), locale) })} ); } // A day without a forecast to ask for: past, not on a day, a day without a date, a tour without a start. const notice = state.kind === 'ready' ? t('tours.paddle.wind.past') : t(`tours.paddle.wind.${state.kind}`); return }>{notice}; } /** What wind means for a paddler, in general terms; it rates no passage and clears nothing. */ function WindHints({ t }: { t: Translate }) { const [open, setOpen] = useState(false); return (
{open && (
    {(['headwind', 'drift', 'crossing', 'gusts', 'local'] as const).map((hint) => (
  • {t(`tours.paddle.wind.hint.${hint}`)}
  • ))}
)}
); } /** The lake style of the tour being edited, for the wind card to suggest the shore on a windy day. */ export interface LakeStyleChoice { value: PaddleLakeStyle; onChoose: (style: PaddleLakeStyle) => void; } /** * PAD-11: on a windy day, the offer to keep near the shores on the lakes, * with the wind that prompts it; once taken, it says the lakes are planned so. */ function ShoreAdvice({ wind, lakes, t, }: { wind: { meanKmh: number; gustsKmh: number }; lakes: LakeStyleChoice; t: Translate; }) { if (lakes.value === 'shore') { return ( }> {t('tours.paddle.wind.shoreTaken')} ); } return ( }> {t('tours.paddle.wind.shoreAdvice', { mean: wind.meanKmh, gusts: wind.gustsKmh })} ); } /** * PAD-08: the wind on the day a paddle tour is planned for, at its start. The * mean, the gusts and the direction are figures of their own, with the source * and when it was asked; no value is turned into a verdict, and missing data * is never shown as calm. */ export function PaddleWindCard({ wind, lakes }: { wind: TourWind; lakes?: LakeStyleChoice }) { const { t, locale } = useTranslation(); const ready = wind.state.kind === 'ready' && wind.state.forecast.status === 'forecast' ? wind.state.forecast : null; const shoreWind = ready && lakes ? windForShore(ready) : null; return ( } title={t('tours.paddle.wind.title')} foldId="paddle-wind" aside={ wind.tourDays.length > 1 ? ( {wind.tourDays.map(({ day, index }) => ( ))} ) : wind.day ? ( {formatDate(wind.day.day.date, locale) || t('dayplan.dayN', { n: wind.day.index + 1 })} ) : null } >
{ready ? ( ) : ( )} {shoreWind && lakes && }
); } /** The wind card for one tour: its days read from the trip, its start as the place the forecast is for. */ export function PaddleWindSection({ placeId, start, lakes, }: { placeId: number | null; start: { lat: number; lng: number } | null; /** Given while the tour is edited and its route crosses a lake: the card may suggest the shore. */ lakes?: LakeStyleChoice; }) { const wind = useTourWind(placeId, start); return ; }