tooot/src/store.ts

88 lines
2.4 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'
2021-11-15 23:43:35 +01:00
import contextsMigration from '@utils/migrations/contexts/migration'
2021-03-17 15:30:28 +01:00
import instancesMigration from '@utils/migrations/instances/migration'
2022-02-12 14:51:01 +01:00
import settingsMigration from '@utils/migrations/settings/migration'
2022-06-03 21:25:20 +02:00
import appSlice from '@utils/slices/appSlice'
2021-08-29 15:25:38 +02:00
import contextsSlice, { ContextsState } from '@utils/slices/contextsSlice'
import instancesSlice, { InstancesState } from '@utils/slices/instancesSlice'
import settingsSlice, { SettingsState } 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
2021-01-18 00:23:40 +01:00
const contextsPersistConfig = {
key: 'contexts',
prefix,
2021-11-15 23:43:35 +01:00
storage: AsyncStorage,
2022-02-12 14:51:01 +01:00
version: 2,
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,
version: 10,
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,
version: 1,
// @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: {
2021-08-29 15:25:38 +02:00
contexts: persistReducer(contextsPersistConfig, contextsSlice) as Reducer<
ContextsState,
AnyAction
>,
instances: persistReducer(
instancesPersistConfig,
instancesSlice
) as Reducer<InstancesState, AnyAction>,
settings: persistReducer(settingsPersistConfig, settingsSlice) as Reducer<
SettingsState,
AnyAction
>,
2022-06-03 21:25:20 +02:00
app: appSlice
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