Documentation Index
Fetch the complete documentation index at: https://mintlify.com/cristhianblaffita-web/Weather-App-Ts/llms.txt
Use this file to discover all available pages before exploring further.
WeatherContext is the central nervous system of Weather App TS. It owns every piece of shared state the application needs: the search input string, the list of geocoded city candidates, the currently selected city, the raw weather API response, and the transformed hourly and daily forecast arrays that components consume directly. Rather than threading props through multiple component layers, any component in the tree can call useWeatherContext() to read or update this state. The context is created in src/context/WeatherContext.tsx and exposed through a WeatherProvider wrapper component that is mounted at the application root in main.tsx.
Context Shape
WeatherContextType defines every field available to consumers. The table below describes each field, its TypeScript type, and its purpose.
| Field | Type | Description |
|---|---|---|
search | string | Current value of the city search input |
setSearch | Dispatch<SetStateAction<string>> | Updates the search input string |
cities | GeocodingResponse | null | Geocoding API results for the current debounced search term |
selectedCity | Place | null | The city whose forecast is currently displayed |
handleSelect | (city: Place) => void | Selects a city, updating state and persisting to localStorage |
weather | WeatherResponse | null | Full raw response from the Open-Meteo forecast API |
hourlyWeather | HourlyWeather[] | Transformed hourly forecast starting from the current hour (7 entries) |
dailyWeather | DailyWeather[] | Transformed 7-day forecast mapped from the daily API data |
currentWeather | CurrentWeatherResponse | null | Current conditions extracted from weather.current |
loading | boolean | true while the weather fetch is in progress |
error | string | null | Error message string if the weather fetch fails, otherwise null |
Default City
WhenlocalStorage contains no stored city, the context initialises selectedCity to the following constant:
useState initialiser wraps the localStorage read in a try/catch so a corrupted stored value also falls back cleanly to DEFAULT_CITY.
Using the Context
ImportuseWeatherContext and destructure the fields your component needs. The hook is a thin wrapper around useContext that also enforces the provider boundary at runtime.
Data Transformation
Raw API arrays are long — the Open-Meteo forecast endpoint returns 168 hourly entries and 7–16 daily entries depending on the requested range. TwouseMemo calls inside WeatherProvider slice and reshape this data into the exact structures that HourlyWeather and DailyWeather expect.
Hourly weather is computed by finding the index of the current hour within weather.hourly.time, then mapping the parallel arrays into typed objects and taking a 7-entry slice from that index forward. Each object also receives an isDay boolean computed by the isDayTime utility, which compares the entry’s timestamp against the daily sunrise and sunset arrays:
weather changes, keeping derived state in sync with the source of truth without redundant recalculation.
City Persistence
On mount, theselectedCity state initialiser reads from localStorage:
try/catch ensures that if JSON.parse throws — for example because a previous version of the app stored an incompatible value — the app still starts successfully on Berlin rather than crashing. Whenever selectedCity changes, a separate useEffect serialises the new value back to localStorage:
Wrapping the App
WeatherProvider is mounted in main.tsx so that the context is available to App and every component beneath it from the very first render: