2022-09-29 07:05:05 -08:00
|
|
|
import {
|
|
|
|
|
applyMiddleware,
|
|
|
|
|
combineReducers,
|
|
|
|
|
compose,
|
|
|
|
|
legacy_createStore as createStore,
|
|
|
|
|
} from 'redux'
|
2020-05-15 07:04:48 -08:00
|
|
|
import { routerMiddleware, connectRouter } from 'connected-react-router'
|
2020-03-31 10:04:10 -08:00
|
|
|
import createSagaMiddleware from 'redux-saga'
|
|
|
|
|
import { all, fork } from 'redux-saga/effects'
|
|
|
|
|
import { adminReducer, adminSaga, USER_LOGOUT } from 'react-admin'
|
|
|
|
|
import throttle from 'lodash.throttle'
|
|
|
|
|
import { loadState, saveState } from './persistState'
|
|
|
|
|
|
2021-05-25 05:58:06 -08:00
|
|
|
const createAdminStore = ({
|
2020-03-31 10:04:10 -08:00
|
|
|
authProvider,
|
|
|
|
|
dataProvider,
|
|
|
|
|
history,
|
|
|
|
|
customReducers = {},
|
|
|
|
|
}) => {
|
|
|
|
|
const reducer = combineReducers({
|
|
|
|
|
admin: adminReducer,
|
|
|
|
|
router: connectRouter(history),
|
|
|
|
|
...customReducers,
|
|
|
|
|
})
|
|
|
|
|
const resettableAppReducer = (state, action) =>
|
|
|
|
|
reducer(action.type !== USER_LOGOUT ? state : undefined, action)
|
|
|
|
|
|
|
|
|
|
const saga = function* rootSaga() {
|
|
|
|
|
yield all([adminSaga(dataProvider, authProvider)].map(fork))
|
|
|
|
|
}
|
|
|
|
|
const sagaMiddleware = createSagaMiddleware()
|
|
|
|
|
|
|
|
|
|
const composeEnhancers =
|
|
|
|
|
(process.env.NODE_ENV === 'development' &&
|
|
|
|
|
typeof window !== 'undefined' &&
|
|
|
|
|
window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ &&
|
|
|
|
|
window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({
|
|
|
|
|
trace: true,
|
|
|
|
|
traceLimit: 25,
|
|
|
|
|
})) ||
|
|
|
|
|
compose
|
|
|
|
|
|
|
|
|
|
const persistedState = loadState()
|
2021-07-03 16:35:21 -08:00
|
|
|
if (persistedState?.player?.savedPlayIndex) {
|
|
|
|
|
persistedState.player.playIndex = persistedState.player.savedPlayIndex
|
|
|
|
|
}
|
2020-03-31 10:04:10 -08:00
|
|
|
const store = createStore(
|
|
|
|
|
resettableAppReducer,
|
|
|
|
|
persistedState,
|
2023-12-18 10:56:03 -09:00
|
|
|
composeEnhancers(
|
|
|
|
|
applyMiddleware(sagaMiddleware, routerMiddleware(history)),
|
|
|
|
|
),
|
2020-03-31 10:04:10 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
store.subscribe(
|
|
|
|
|
throttle(() => {
|
|
|
|
|
const state = store.getState()
|
|
|
|
|
saveState({
|
|
|
|
|
theme: state.theme,
|
2024-09-28 07:54:36 -08:00
|
|
|
player: (({ queue, volume, savedPlayIndex }) => ({
|
|
|
|
|
queue,
|
|
|
|
|
volume,
|
|
|
|
|
savedPlayIndex,
|
|
|
|
|
}))(state.player),
|
2020-03-31 10:04:10 -08:00
|
|
|
albumView: state.albumView,
|
2020-11-19 19:06:09 -09:00
|
|
|
settings: state.settings,
|
2020-03-31 10:04:10 -08:00
|
|
|
})
|
|
|
|
|
}),
|
2023-12-18 10:56:03 -09:00
|
|
|
1000,
|
2020-03-31 10:04:10 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
sagaMiddleware.run(saga)
|
|
|
|
|
return store
|
|
|
|
|
}
|
2021-05-25 05:58:06 -08:00
|
|
|
|
|
|
|
|
export default createAdminStore
|