tooot/src/store.ts

92 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'
import {
combineReducers,
configureStore,
getDefaultMiddleware
} from '@reduxjs/toolkit'
2021-03-04 01:03:53 +01:00
import { InstancesV3 } from '@utils/migrations/instances/v3'
2021-01-18 00:23:40 +01:00
import contextsSlice from '@utils/slices/contextsSlice'
2021-02-20 19:12:44 +01:00
import instancesSlice from '@utils/slices/instancesSlice'
2020-12-13 14:04:25 +01:00
import settingsSlice from '@utils/slices/settingsSlice'
2021-03-04 01:03:53 +01:00
import { createMigrate, persistReducer, persistStore } 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,
storage: AsyncStorage
}
2021-03-04 01:03:53 +01:00
const instancesMigration = {
4: (state: InstancesV3) => {
return {
instances: state.local.instances.map((instance, index) => {
// @ts-ignore
delete instance.notification
return {
...instance,
active: state.local.activeIndex === index,
push: {
global: { loading: false, value: false },
decode: { loading: false, value: false },
alerts: {
follow: { loading: false, value: true },
favourite: { loading: false, value: true },
reblog: { loading: false, value: true },
mention: { loading: false, value: true },
poll: { loading: false, value: true }
},
keys: undefined
}
}
})
}
}
}
2020-11-21 13:32:29 +01:00
const instancesPersistConfig = {
key: 'instances',
2021-01-07 19:13:09 +01:00
prefix,
2021-03-04 01:03:53 +01:00
storage: secureStorage,
version: 4,
// @ts-ignore
migrate: createMigrate(instancesMigration)
2020-11-21 13:32:29 +01:00
}
const settingsPersistConfig = {
key: 'settings',
2021-01-07 19:13:09 +01:00
prefix,
storage: AsyncStorage
}
2021-01-07 19:13:09 +01:00
const rootPersistConfig = {
key: 'root',
prefix,
version: 0,
storage: AsyncStorage
}
const rootReducer = combineReducers({
2021-01-18 00:23:40 +01:00
contexts: persistReducer(contextsPersistConfig, contextsSlice),
2021-01-07 19:13:09 +01:00
instances: persistReducer(instancesPersistConfig, instancesSlice),
settings: persistReducer(settingsPersistConfig, settingsSlice)
})
2020-10-31 21:04:46 +01:00
const store = configureStore({
2021-01-07 19:13:09 +01:00
reducer: persistReducer(rootPersistConfig, rootReducer),
2020-11-22 00:46:23 +01:00
middleware: getDefaultMiddleware({
serializableCheck: {
ignoredActions: ['persist/PERSIST']
}
})
})
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>