Skip to content

Commit 555e461

Browse files
committed
persistence
1 parent de76fed commit 555e461

File tree

6 files changed

+73
-19
lines changed

6 files changed

+73
-19
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
"react": "19.1.0",
4848
"react-dom": "19.1.0",
4949
"react-redux": "9.2.0",
50+
"redux": "5.0.1",
51+
"redux-persist": "6.0.0",
5052
"tslib": "2.8.1"
5153
},
5254
"browserslist": [

src/Providers.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import * as React from 'react';
22
import { Provider as ReduxProvider } from 'react-redux';
3-
import { store } from './store';
3+
import { PersistGate } from 'redux-persist/integration/react';
4+
import { persistor, store } from './store';
45

56
const Providers: React.FC<{ children: React.ReactNode }> = ({ children }) => (
6-
<ReduxProvider store={store}>{children}</ReduxProvider>
7+
<ReduxProvider store={store}>
8+
<PersistGate persistor={persistor}>{children}</PersistGate>
9+
</ReduxProvider>
710
);
811

912
export default Providers;

src/Results.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ const Results: React.FC = () => {
8080
return null;
8181
}
8282
}, [game?.mode, game?.target, photos]);
83-
if (results) {
83+
if (results && results.features.length > 0) {
8484
return (
8585
<>
8686
<h2>Results</h2>

src/computation.ts

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
import { Coord, featureCollection, point } from '@turf/helpers';
22
import nearestPointOnLine from '@turf/nearest-point-on-line';
3-
import type { FeatureCollection, Point } from 'geojson';
3+
import type { Feature, FeatureCollection, LineString, Point } from 'geojson';
44
import { GameConfig, GameMode } from './game-modes';
55
import { useAppSelector } from './store';
66
import { distance } from './util';
77

8+
const isDegenerate = (line: Feature<LineString>) =>
9+
line.geometry.coordinates.length !== 2 ||
10+
line.geometry.coordinates[0].join(',') ===
11+
line.geometry.coordinates[1].join(',');
12+
813
export const useGameConfig = (): GameConfig | null => {
914
const { mode, lineTarget, basicTarget, multiTarget, error } = useAppSelector(
1015
({ game }) => game
@@ -13,7 +18,7 @@ export const useGameConfig = (): GameConfig | null => {
1318
return null;
1419
} else if (mode === GameMode.BASIC) {
1520
return { mode, target: basicTarget };
16-
} else if (mode === GameMode.LINE) {
21+
} else if (mode === GameMode.LINE && !isDegenerate(lineTarget)) {
1722
return { mode, target: lineTarget };
1823
} else if (mode === GameMode.MULTI && multiTarget.features.length > 0) {
1924
return { mode, target: multiTarget };
@@ -39,12 +44,17 @@ export const decorate = <P extends {}>(
3944
photos: FeatureCollection<Point, P>
4045
): FeatureCollection<Point, P & { distance: number }> => {
4146
const calculator = distanceCalc(game);
42-
return featureCollection(
43-
photos.features
44-
.map((photo) => ({
45-
...photo,
46-
properties: { ...photo.properties, distance: calculator(photo) },
47-
}))
48-
.sort((a, b) => a.properties.distance - b.properties.distance)
49-
);
47+
try {
48+
return featureCollection(
49+
photos.features
50+
.map((photo) => ({
51+
...photo,
52+
properties: { ...photo.properties, distance: calculator(photo) },
53+
}))
54+
.sort((a, b) => a.properties.distance - b.properties.distance)
55+
);
56+
} catch (error) {
57+
console.error(error);
58+
return featureCollection([]);
59+
}
5060
};

src/store.ts

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,44 @@
11
import { useDispatch, useSelector } from 'react-redux';
2-
import { configureStore } from '@reduxjs/toolkit';
2+
import { combineReducers, configureStore } from '@reduxjs/toolkit';
3+
import {
4+
FLUSH,
5+
PAUSE,
6+
PERSIST,
7+
PURGE,
8+
REGISTER,
9+
REHYDRATE,
10+
persistReducer,
11+
persistStore,
12+
} from 'redux-persist';
13+
import storage from 'redux-persist/lib/storage';
314
import dataReducer from './dataSlice';
415
import gameReducer from './gameSlice';
516

17+
const rootReducer = combineReducers({
18+
game: gameReducer,
19+
data: dataReducer,
20+
});
21+
22+
const persistConfig = {
23+
key: 'root',
24+
storage,
25+
};
26+
27+
const persistedReducer = persistReducer(persistConfig, rootReducer);
28+
629
export const store = configureStore({
7-
reducer: {
8-
game: gameReducer,
9-
data: dataReducer,
10-
},
30+
reducer: persistedReducer,
1131
devTools: DEBUG,
32+
middleware: (getDefaultMiddleware) =>
33+
getDefaultMiddleware({
34+
serializableCheck: {
35+
ignoredActions: [FLUSH, REHYDRATE, PAUSE, PERSIST, PURGE, REGISTER],
36+
},
37+
}),
1238
});
1339

40+
export const persistor = persistStore(store);
41+
1442
export type RootState = ReturnType<typeof store.getState>;
1543
export type AppDispatch = typeof store.dispatch;
1644

yarn.lock

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4774,6 +4774,15 @@ __metadata:
47744774
languageName: node
47754775
linkType: hard
47764776

4777+
"redux-persist@npm:6.0.0":
4778+
version: 6.0.0
4779+
resolution: "redux-persist@npm:6.0.0"
4780+
peerDependencies:
4781+
redux: ">4.0.0"
4782+
checksum: 10c0/8242d265ab8d28bbc95cf2dc2a05b869eb67aa309b1ed08163c926f3af56dd8eb1ea62118286083461b8ef2024d3b349fd264e5a62a70eb2e74d068c832d5bf2
4783+
languageName: node
4784+
linkType: hard
4785+
47774786
"redux-thunk@npm:^3.1.0":
47784787
version: 3.1.0
47794788
resolution: "redux-thunk@npm:3.1.0"
@@ -4783,7 +4792,7 @@ __metadata:
47834792
languageName: node
47844793
linkType: hard
47854794

4786-
"redux@npm:^5.0.1":
4795+
"redux@npm:5.0.1, redux@npm:^5.0.1":
47874796
version: 5.0.1
47884797
resolution: "redux@npm:5.0.1"
47894798
checksum: 10c0/b10c28357194f38e7d53b760ed5e64faa317cc63de1fb95bc5d9e127fab956392344368c357b8e7a9bedb0c35b111e7efa522210cfdc3b3c75e5074718e9069c
@@ -5155,6 +5164,8 @@ __metadata:
51555164
react: "npm:19.1.0"
51565165
react-dom: "npm:19.1.0"
51575166
react-redux: "npm:9.2.0"
5167+
redux: "npm:5.0.1"
5168+
redux-persist: "npm:6.0.0"
51585169
svgo: "npm:3.3.2"
51595170
ts-node: "npm:10.9.2"
51605171
tslib: "npm:2.8.1"

0 commit comments

Comments
 (0)