forked from aeharding/voyager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore.tsx
87 lines (77 loc) · 2.63 KB
/
store.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import { ReactNode, useEffect } from "react";
import { ActionCreatorWithPayload, configureStore } from "@reduxjs/toolkit";
import postSlice from "./features/post/postSlice";
import {
Provider,
TypedUseSelectorHook,
useDispatch,
useSelector,
} from "react-redux";
import authSlice, { handleSelector } from "./features/auth/authSlice";
import commentSlice from "./features/comment/commentSlice";
import communitySlice, {
getFavoriteCommunities,
} from "./features/community/communitySlice";
import userSlice from "./features/user/userSlice";
import inboxSlice from "./features/inbox/inboxSlice";
import settingsSlice, {
fetchSettingsFromDatabase,
getBlurNsfw,
getFilteredKeywords,
} from "./features/settings/settingsSlice";
import gestureSlice, {
fetchGesturesFromDatabase,
} from "./features/settings/gestures/gestureSlice";
import appIconSlice, {
fetchAppIcon,
} from "./features/settings/app-icon/appIconSlice";
const store = configureStore({
reducer: {
post: postSlice,
comment: commentSlice,
auth: authSlice,
community: communitySlice,
user: userSlice,
inbox: inboxSlice,
settings: settingsSlice,
gesture: gestureSlice,
appIcon: appIconSlice,
},
});
export type RootState = ReturnType<typeof store.getState>;
export type AppDispatch = typeof store.dispatch;
export const useAppDispatch: () => AppDispatch = useDispatch;
export const useAppSelector: TypedUseSelectorHook<RootState> = useSelector;
export type Dispatchable<T> =
| ((val: T) => (dispatch: AppDispatch, getState: () => RootState) => void)
| ActionCreatorWithPayload<T>;
export default store;
let lastActiveHandle: string | undefined = undefined;
const activeHandleChange = () => {
const state = store.getState();
const activeHandle = handleSelector(state);
if (activeHandle === lastActiveHandle) return;
lastActiveHandle = activeHandle;
store.dispatch(getFavoriteCommunities());
store.dispatch(getBlurNsfw());
store.dispatch(getFilteredKeywords());
};
export function StoreProvider({ children }: { children: ReactNode }) {
useEffect(() => {
(async () => {
try {
// Load initial settings from DB into the store
await Promise.all([
store.dispatch(fetchSettingsFromDatabase()),
store.dispatch(fetchGesturesFromDatabase()),
store.dispatch(fetchAppIcon()),
]);
} finally {
// Subscribe to actions to handle handle changes, this can be used to react to other changes as well
// to coordinate side effects between slices.
store.subscribe(activeHandleChange);
}
})();
}, []);
return <Provider store={store}>{children}</Provider>;
}