@@ -113,11 +113,9 @@ const MapTiff = ({
113
113
}
114
114
} , [ isReduced , mapContainer ] ) ;
115
115
116
- const loadMapLayer = useCallback (
116
+ const loadSource = useCallback (
117
117
async ( name : string , year : string ) => {
118
- setLoading ( true ) ;
119
118
if ( ! map ?. getSource ( name + year ) && mapsData . length > 0 ) {
120
- // Filter the contentful map data by name
121
119
const mapData = mapsData . find (
122
120
( data ) => data . fields . id === name ,
123
121
) ?. fields ;
@@ -134,6 +132,7 @@ const MapTiff = ({
134
132
body,
135
133
} ,
136
134
) ;
135
+
137
136
if ( response . status !== 200 ) {
138
137
setLoading ( false ) ;
139
138
@@ -145,14 +144,34 @@ const MapTiff = ({
145
144
map ?. addSource ( name + year , {
146
145
type : "raster" ,
147
146
tiles : [ url ] ,
148
- tileSize : 256 ,
147
+ tileSize : isReduced ? 128 : 256 ,
149
148
} ) ;
150
149
}
151
150
}
151
+ } ,
152
+ [ map , mapsData , setLoading , isReduced ] ,
153
+ ) ;
154
+
155
+ const cacheMapData = useCallback ( async ( ) => {
156
+ mapsData . forEach ( async ( data ) => {
157
+ const id = data . fields . id ;
158
+ const imageData = data . fields . imageData ;
159
+ if ( ! isReduced ) {
160
+ Object . keys ( imageData ) . forEach ( async ( year ) => {
161
+ if ( map && ! map ?. getSource ( data + year ) ) loadSource ( id , year ) ;
162
+ } ) ;
163
+ } else {
164
+ const dates = Object . keys ( imageData ) ;
165
+ loadSource ( id , dates [ dates . length - 1 ] ) ;
166
+ }
167
+ } ) ;
168
+ } , [ mapsData , loadSource , isReduced , map ] ) ;
169
+
170
+ const loadMapLayer = useCallback (
171
+ async ( name : string , year : string ) => {
172
+ setLoading ( true ) ;
173
+ await loadSource ( name , year ) ;
152
174
153
- // This code adds a raster layer on top of the first "symbol" layer in the map's style.
154
- // It checks if the map and its layers exist, finds the first layer of type "symbol",
155
- // and inserts the raster layer right above it.
156
175
const symbolLayer = map
157
176
?. getStyle ( )
158
177
?. layers ?. find ( ( layer ) => layer . type === "symbol" ) ;
@@ -167,9 +186,12 @@ const MapTiff = ({
167
186
symbolLayer . id ,
168
187
) ;
169
188
}
189
+
190
+ cacheMapData ( ) ;
191
+
170
192
setLoading ( false ) ;
171
193
} ,
172
- [ map , mapsData , setLoading ] ,
194
+ [ map , setLoading , loadSource , cacheMapData ] ,
173
195
) ;
174
196
175
197
const addPopupEffect = useCallback (
0 commit comments