-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from rarimo/feature/valtio
Replace redux with Valtio
- Loading branch information
Showing
23 changed files
with
543 additions
and
734 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './error-handler' | ||
export * from './event-bus' | ||
export * from './promise' | ||
export * from './store' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { INTERNAL_Snapshot, proxy, subscribe, useSnapshot } from 'valtio' | ||
|
||
export const createStore = <S extends object, A>( | ||
storeName: string, | ||
initialState: S, | ||
actions: (state: S) => A, | ||
): [Readonly<S> & A, () => INTERNAL_Snapshot<S>] => { | ||
const storageState = localStorage.getItem(storeName) | ||
|
||
let parsedStorageState: S = {} as S | ||
|
||
try { | ||
parsedStorageState = JSON.parse(storageState!) | ||
} catch (e) { | ||
/* empty */ | ||
} | ||
|
||
const state = proxy<S>({ | ||
...initialState, | ||
...parsedStorageState, | ||
}) | ||
|
||
subscribe(state, () => { | ||
localStorage.setItem(storeName, JSON.stringify(state)) | ||
}) | ||
|
||
return [Object.assign(state, actions(state)), () => useSnapshot(state)] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1 @@ | ||
import { configureStore } from '@reduxjs/toolkit' | ||
|
||
import { uiStorage, web3Storage } from '@/store/storages' | ||
|
||
import uiReducer from './ui' | ||
import web3Reducer from './web3' | ||
|
||
export const store = configureStore({ | ||
reducer: { | ||
ui: uiReducer, | ||
web3: web3Reducer, | ||
}, | ||
preloadedState: { | ||
ui: uiStorage.getStorage(), | ||
web3: web3Storage.getStorage(), | ||
}, | ||
}) | ||
|
||
// Infer the `RootState` and `AppDispatch` types from the store itself | ||
export type RootState = ReturnType<typeof store.getState> | ||
// Inferred type: {posts: PostsState, comments: CommentsState, users: UsersState} | ||
export type AppDispatch = typeof store.dispatch | ||
|
||
export * from './ui' | ||
export * from './web3' | ||
export * from './modules' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './ui.module' | ||
export * from './web3.module' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { ThemeMode } from '@/enums' | ||
import { createStore } from '@/helpers' | ||
|
||
interface UiStore { | ||
viewportWidth: number | ||
themeMode?: ThemeMode | ||
} | ||
|
||
export const [uiStore, useUiState] = createStore( | ||
'ui', | ||
{ | ||
viewportWidth: 0, | ||
themeMode: ThemeMode.Light, | ||
} as UiStore, | ||
state => ({ | ||
setViewportWidth: (width: number) => { | ||
state.viewportWidth = width | ||
}, | ||
setThemeMode: (mode: ThemeMode) => { | ||
state.themeMode = mode | ||
}, | ||
clearUiStorage: () => { | ||
state.themeMode = '' as ThemeMode | ||
state.viewportWidth = 0 | ||
}, | ||
}), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { createStore } from '@/helpers' | ||
import { SUPPORTED_PROVIDERS } from '@/types' | ||
|
||
interface Web3State { | ||
providerType?: SUPPORTED_PROVIDERS | ||
} | ||
|
||
const [web3Store, useWeb3State] = createStore( | ||
'web3', | ||
{ | ||
providerType: undefined, | ||
} as Web3State, | ||
state => ({ | ||
setProviderType: (providerType: SUPPORTED_PROVIDERS | undefined) => { | ||
state.providerType = providerType | ||
}, | ||
}), | ||
) | ||
|
||
export { useWeb3State, web3Store } |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.