import { Middleware } from '@reduxjs/toolkit' ; import { defaultProjectState } from './slices/editingProject' ; import { RootState } from './store' ; const KEY = 'jb_redux_preloadedState' ; interface PreloadedState { reduxState : RootState ; } export const localStoragePreloadMiddleware = store => next => action => { if ( typeof localStorage === 'undefined' ) { return ; } localStorage . setItem ( KEY , JSON . stringify ( { reduxState : store . getState ( ) , } as PreloadedState ) ) ; return next ( action ) ; } ; export default function getLocalStoragePreloadedState ( ) { try { const stateString = localStorage . getItem ( KEY ) ; if ( ! stateString ) { return undefined ; } const parsedState = JSON . parse ( stateString ) ; if ( parsedState ?. reduxState ?. editingProject ?. version === defaultProjectState . version ) { return parsedState . reduxState ; } else { localStorage . removeItem ( stateString ) ; } } catch ( e ) { return undefined ; } }