@@ -4,6 +4,14 @@ import { calculateStats } from "../map/utils.ts";
4
4
import tArea from "@turf/area" ;
5
5
import tBboxPolygon from "@turf/bbox-polygon" ;
6
6
import { getFgbData } from "../utils/get-fgb-data.ts" ;
7
+ import { Map } from "maplibre-gl" ;
8
+
9
+ const availableTimestamps = [
10
+ `2024-05-19T05:00:00Z` ,
11
+ `2024-05-19T06:00:00Z` ,
12
+ `2024-05-19T07:00:00Z` ,
13
+ `2024-05-19T08:00:00Z` ,
14
+ ] ;
7
15
8
16
// TODO move to types.ts
9
17
/* eslint-disable no-unused-vars */
@@ -29,20 +37,21 @@ export interface AppState {
29
37
map : any ;
30
38
mapStatus : MapStatus ;
31
39
geojson ?: any ;
40
+ currentTimestamp ?: Date ;
32
41
currentTimestampGeojson ?: any ;
33
42
}
34
43
35
44
export type AppAction =
36
45
| {
37
46
type : AppActionTypes . SET_MAP_REF ;
38
47
data : {
39
- map : any ;
48
+ map : Map ;
40
49
} ;
41
50
}
42
51
| {
43
52
type : AppActionTypes . SET_CURRENT_TIMESTAMP ;
44
53
data : {
45
- currentTimestamp : string ;
54
+ currentTimestamp : Date ;
46
55
} ;
47
56
}
48
57
| {
@@ -66,17 +75,10 @@ export const appInitialState = {
66
75
type : "FeatureCollection" ,
67
76
features : [ ] ,
68
77
} ,
78
+ currentTimestamp : new Date ( availableTimestamps [ 2 ] ) ,
79
+ timestamps : [ ...availableTimestamps ] ,
69
80
} ;
70
81
71
- function applyTimestampFilter ( geojson : any , timestamp : string ) {
72
- return {
73
- type : "FeatureCollection" ,
74
- features : geojson . features . filter (
75
- ( f : any ) => f . properties . timestamp === timestamp ,
76
- ) ,
77
- } ;
78
- }
79
-
80
82
function appReducer ( state : AppState , action : AppAction ) {
81
83
switch ( action . type ) {
82
84
case AppActionTypes . SET_MAP_REF :
@@ -91,13 +93,10 @@ function appReducer(state: AppState, action: AppAction) {
91
93
mapStatus : MapStatus . LOADING ,
92
94
} ;
93
95
case AppActionTypes . UPDATE_VIEW_SUCCESS : {
94
- const { geojson, timestamps } = action . data ;
95
- const currentTimestamp = timestamps [ timestamps . length - 1 ] ;
96
- const currentTimestampGeojson = applyTimestampFilter (
97
- geojson ,
98
- currentTimestamp ,
99
- ) ;
100
- const stats = calculateStats ( currentTimestampGeojson ) ;
96
+ const { geojson } = action . data ;
97
+ const { currentTimestamp } = state ;
98
+
99
+ const stats = calculateStats ( geojson ) ;
101
100
102
101
const bounds = state . map . getBounds ( ) . toArray ( ) ;
103
102
const [ [ minX , minY ] , [ maxX , maxY ] ] = bounds ;
@@ -110,18 +109,15 @@ function appReducer(state: AppState, action: AppAction) {
110
109
formattedArea,
111
110
stats,
112
111
geojson,
113
- timestamps,
114
112
currentTimestamp,
115
- currentTimestampGeojson,
113
+ currentTimestampGeojson : currentTimestamp ,
116
114
mapStatus : MapStatus . READY ,
117
115
} ;
118
116
}
119
117
case AppActionTypes . SET_CURRENT_TIMESTAMP : {
120
118
const { currentTimestamp } = action . data ;
121
- const currentTimestampGeojson = applyTimestampFilter (
122
- state . geojson ,
123
- currentTimestamp ,
124
- ) ;
119
+
120
+ const currentTimestampGeojson = state . geojson ;
125
121
const stats = calculateStats ( currentTimestampGeojson ) ;
126
122
return {
127
123
...state ,
@@ -141,7 +137,7 @@ const asyncActionHandlers: any = {
141
137
( { dispatch, getState } : any ) =>
142
138
async ( ) => {
143
139
try {
144
- const { map, mapStatus } = getState ( ) ;
140
+ const { map, mapStatus, currentTimestamp } = getState ( ) ;
145
141
146
142
// Only update the view if the map is ready
147
143
if ( mapStatus !== MapStatus . READY ) {
@@ -152,7 +148,10 @@ const asyncActionHandlers: any = {
152
148
type : AppActionTypes . UPDATE_VIEW_START ,
153
149
} ) ;
154
150
155
- const { geojson, timestamps } = await getFgbData ( { map } ) ;
151
+ const { geojson, timestamps } = await getFgbData ( {
152
+ map,
153
+ timestamp : currentTimestamp ,
154
+ } ) ;
156
155
157
156
map . getSource ( "data" ) . setData ( geojson ) ;
158
157
0 commit comments