tooot/src/store.ts

95 lines
2.6 KiB
TypeScript
Raw Normal View History

import createSecureStore from '@neverdull-agency/expo-unlimited-secure-store'
2021-01-07 19:13:09 +01:00
import AsyncStorage from '@react-native-async-storage/async-storage'
2021-08-29 15:25:38 +02:00
import { AnyAction, configureStore, Reducer } from '@reduxjs/toolkit'
2022-12-11 01:37:26 +01:00
import contextsMigration, { ContextsLatest } from '@utils/migrations/contexts/migration'
2021-03-17 15:30:28 +01:00
import instancesMigration from '@utils/migrations/instances/migration'
2022-12-23 18:19:14 +01:00
import settingsMigration, { SettingsLatest } from '@utils/migrations/settings/migration'
2022-11-22 22:00:33 +01:00
import appSlice, { AppState } from '@utils/slices/appSlice'
2022-12-11 01:37:26 +01:00
import contextsSlice from '@utils/slices/contextsSlice'
2021-08-29 15:25:38 +02:00
import instancesSlice, { InstancesState } from '@utils/slices/instancesSlice'
2022-12-23 18:19:14 +01:00
import settingsSlice from '@utils/slices/settingsSlice'
2022-05-19 00:46:25 +02:00
import { Platform } from 'react-native'
2022-04-30 23:47:52 +02:00
import { TypedUseSelectorHook, useDispatch, useSelector } from 'react-redux'
2022-05-19 00:46:25 +02:00
import {
createMigrate,
FLUSH,
PAUSE,
PERSIST,
persistReducer,
persistStore,
PURGE,
REGISTER,
REHYDRATE
} from 'redux-persist'
2020-11-21 13:32:29 +01:00
const secureStorage = createSecureStore()
2021-01-31 03:09:35 +01:00
const prefix = 'tooot'
2021-01-07 19:13:09 +01:00
2022-11-22 22:00:33 +01:00
const appPersistConfig = {
key: 'app',
prefix,
storage: AsyncStorage,
version: 0
}
2021-01-18 00:23:40 +01:00
const contextsPersistConfig = {
key: 'contexts',
prefix,
2021-11-15 23:43:35 +01:00
storage: AsyncStorage,
2022-12-11 01:37:26 +01:00
version: 3,
2021-11-15 23:43:35 +01:00
// @ts-ignore
migrate: createMigrate(contextsMigration)
2021-01-18 00:23:40 +01:00
}
2020-11-21 13:32:29 +01:00
const instancesPersistConfig = {
key: 'instances',
2021-01-07 19:13:09 +01:00
prefix,
2022-05-19 00:46:25 +02:00
storage: Platform.OS === 'ios' ? secureStorage : AsyncStorage,
2022-12-09 21:09:00 +01:00
version: 11,
2021-03-04 01:03:53 +01:00
// @ts-ignore
2021-05-09 21:59:03 +02:00
migrate: createMigrate(instancesMigration)
2020-11-21 13:32:29 +01:00
}
const settingsPersistConfig = {
key: 'settings',
2021-01-07 19:13:09 +01:00
prefix,
2022-02-12 14:51:01 +01:00
storage: AsyncStorage,
2022-11-29 23:44:11 +01:00
version: 3,
2022-02-12 14:51:01 +01:00
// @ts-ignore
migrate: createMigrate(settingsMigration)
}
2020-10-31 21:04:46 +01:00
const store = configureStore({
2021-05-09 21:59:03 +02:00
reducer: {
2022-11-22 22:00:33 +01:00
app: persistReducer(appPersistConfig, appSlice) as Reducer<AppState, AnyAction>,
2021-08-29 15:25:38 +02:00
contexts: persistReducer(contextsPersistConfig, contextsSlice) as Reducer<
2022-12-11 01:37:26 +01:00
ContextsLatest,
2021-08-29 15:25:38 +02:00
AnyAction
>,
2022-11-22 22:00:33 +01:00
instances: persistReducer(instancesPersistConfig, instancesSlice) as Reducer<
InstancesState,
AnyAction
>,
2021-08-29 15:25:38 +02:00
settings: persistReducer(settingsPersistConfig, settingsSlice) as Reducer<
2022-12-23 18:19:14 +01:00
SettingsLatest,
2021-08-29 15:25:38 +02:00
AnyAction
2022-11-22 22:00:33 +01:00
>
2021-05-09 21:59:03 +02:00
},
2021-08-29 15:25:38 +02:00
middleware: getDefaultMiddleware =>
getDefaultMiddleware({
serializableCheck: {
2022-05-19 00:46:25 +02:00
ignoredActions: [FLUSH, REHYDRATE, PAUSE, PERSIST, PURGE, REGISTER]
2021-08-29 15:25:38 +02:00
}
})
})
2020-10-31 21:04:46 +01:00
2020-11-21 13:32:29 +01:00
let persistor = persistStore(store)
2020-10-31 21:04:46 +01:00
2020-11-21 13:32:29 +01:00
export { store, persistor }
2020-10-31 21:04:46 +01:00
export type RootState = ReturnType<typeof store.getState>
2022-04-30 23:47:52 +02:00
type AppDispatch = typeof store.dispatch
export const useAppDispatch = () => useDispatch<AppDispatch>()
export const useAppSelector: TypedUseSelectorHook<RootState> = useSelector