@@ -11,6 +11,7 @@ import { TEAL_ON_DARK_RGB } from "@/lib/constants/theme";
1111import { DEFAULT_MAP_VIEW , MAP_BASE_STYLES } from "@/lib/map/viewState" ;
1212import { openZarrStore } from "@/lib/zarr/store" ;
1313import { ZarrChunkReader } from "@/lib/zarr/ZarrChunkReader" ;
14+ import { DEFAULT_HISTORY_YEARS } from "@/lib/zarr/timeRange" ;
1415import type { MapSelection } from "@/types/map" ;
1516import { useTheme } from "@/providers/ThemeProvider" ;
1617import { MapReadout } from "@/components/map/MapReadout" ;
@@ -25,54 +26,70 @@ export function EarthMap({ className }: EarthMapProps) {
2526 const requestIdRef = useRef ( 0 ) ;
2627
2728 const [ selection , setSelection ] = useState < MapSelection | null > ( null ) ;
29+ const [ historyYears , setHistoryYears ] = useState ( DEFAULT_HISTORY_YEARS ) ;
2830 const [ loadingSeries , setLoadingSeries ] = useState ( false ) ;
2931 const [ seriesError , setSeriesError ] = useState < string | null > ( null ) ;
3032 const [ seriesLength , setSeriesLength ] = useState < number | null > ( null ) ;
3133 const [ seriesPreview , setSeriesPreview ] = useState < number [ ] | null > ( null ) ;
3234 const [ seriesUnits , setSeriesUnits ] = useState < string | null > ( null ) ;
3335
34- const loadTimeSeries = useCallback ( async ( nextSelection : MapSelection ) => {
35- const requestId = ++ requestIdRef . current ;
36- setLoadingSeries ( true ) ;
37- setSeriesError ( null ) ;
38- setSeriesLength ( null ) ;
39- setSeriesPreview ( null ) ;
40- setSeriesUnits ( null ) ;
41-
42- try {
43- if ( ! readerPromiseRef . current ) {
44- readerPromiseRef . current = openZarrStore ( )
45- . then ( ( ds ) => new ZarrChunkReader ( ds ) )
46- . catch ( ( error ) => {
47- readerPromiseRef . current = null ;
48- throw error ;
49- } ) ;
36+ const loadTimeSeries = useCallback (
37+ async ( nextSelection : MapSelection , years : number ) => {
38+ const requestId = ++ requestIdRef . current ;
39+ setLoadingSeries ( true ) ;
40+ setSeriesError ( null ) ;
41+ setSeriesLength ( null ) ;
42+ setSeriesPreview ( null ) ;
43+ setSeriesUnits ( null ) ;
44+
45+ try {
46+ if ( ! readerPromiseRef . current ) {
47+ readerPromiseRef . current = openZarrStore ( )
48+ . then ( ( ds ) => new ZarrChunkReader ( ds ) )
49+ . catch ( ( error ) => {
50+ readerPromiseRef . current = null ;
51+ throw error ;
52+ } ) ;
53+ }
54+
55+ const reader = await readerPromiseRef . current ;
56+
57+ const { values, units } = await reader . getTimeSeries (
58+ nextSelection . grid ,
59+ undefined ,
60+ years ,
61+ ) ;
62+
63+ if ( requestId !== requestIdRef . current ) return ;
64+
65+ setSeriesLength ( values . length ) ;
66+ setSeriesPreview ( Array . from ( values . subarray ( 0 , 3 ) ) ) ;
67+ setSeriesUnits ( units ?? null ) ;
68+ } catch ( error ) {
69+ if ( requestId !== requestIdRef . current ) return ;
70+ setSeriesError (
71+ error instanceof Error
72+ ? error . message
73+ : "Could not load the Zarr time series." ,
74+ ) ;
75+ } finally {
76+ if ( requestId === requestIdRef . current ) {
77+ setLoadingSeries ( false ) ;
78+ }
5079 }
80+ } ,
81+ [ ] ,
82+ ) ;
5183
52- const reader = await readerPromiseRef . current ;
53-
54- const { values, units } = await reader . getTimeSeries (
55- nextSelection . grid ,
56- ) ;
57-
58- if ( requestId !== requestIdRef . current ) return ;
59-
60- setSeriesLength ( values . length ) ;
61- setSeriesPreview ( Array . from ( values . subarray ( 0 , 3 ) ) ) ;
62- setSeriesUnits ( units ?? null ) ;
63- } catch ( error ) {
64- if ( requestId !== requestIdRef . current ) return ;
65- setSeriesError (
66- error instanceof Error
67- ? error . message
68- : "Could not load the Zarr time series." ,
69- ) ;
70- } finally {
71- if ( requestId === requestIdRef . current ) {
72- setLoadingSeries ( false ) ;
84+ const handleHistoryYearsChange = useCallback (
85+ ( years : number ) => {
86+ setHistoryYears ( years ) ;
87+ if ( selection ) {
88+ void loadTimeSeries ( selection , years ) ;
7389 }
74- }
75- } , [ ] ) ;
90+ } ,
91+ [ selection , loadTimeSeries ] ,
92+ ) ;
7693
7794 const handleClick = useCallback (
7895 ( info : PickingInfo ) => {
@@ -85,9 +102,9 @@ export function EarthMap({ className }: EarthMapProps) {
85102 } ;
86103
87104 setSelection ( nextSelection ) ;
88- void loadTimeSeries ( nextSelection ) ;
105+ void loadTimeSeries ( nextSelection , historyYears ) ;
89106 } ,
90- [ loadTimeSeries ] ,
107+ [ historyYears , loadTimeSeries ] ,
91108 ) ;
92109
93110 const layers = useMemo ( ( ) => {
@@ -130,6 +147,8 @@ export function EarthMap({ className }: EarthMapProps) {
130147
131148 < MapReadout
132149 selection = { selection }
150+ historyYears = { historyYears }
151+ onHistoryYearsChange = { handleHistoryYearsChange }
133152 loadingSeries = { loadingSeries }
134153 seriesError = { seriesError }
135154 seriesLength = { seriesLength }
0 commit comments