forked from parca-dev/parca
-
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.
- Loading branch information
1 parent
a575bd4
commit 504f7a3
Showing
6 changed files
with
132 additions
and
25 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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import Cookies from 'universal-cookie' | ||
import { StateStorage } from 'zustand/middleware' | ||
|
||
const cookie = new Cookies() | ||
|
||
const CookieStorage: StateStorage = { | ||
setItem: (key, value) => { | ||
cookie.set(key, value) | ||
}, | ||
getItem: key => { | ||
return cookie.get(key) | ||
} | ||
} | ||
|
||
export default CookieStorage |
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,62 @@ | ||
import { useLayoutEffect } from 'react' | ||
import create from 'zustand' | ||
import createContext from 'zustand/context' | ||
import { persist } from 'zustand/middleware' | ||
import CookieStorage from './cookie-storage' | ||
import NoopStorage from './noop-storage' | ||
import createUiState from './ui.state' | ||
|
||
let store | ||
|
||
const whitelistPersist = ['ui'] | ||
|
||
const stateSlices = (set, get) => ({ | ||
...createUiState(set, get) | ||
// add more slices | ||
}) | ||
|
||
type AppState = ReturnType<typeof stateSlices> | ||
|
||
const zustandContext = createContext<AppState>() | ||
|
||
export const StoreProvider = zustandContext.Provider | ||
|
||
export const useStore = zustandContext.useStore | ||
|
||
export const initializeStore = (initialState = {}) => { | ||
return create( | ||
persist( | ||
(set, get) => ({ | ||
...stateSlices(set, get), | ||
...initialState | ||
}), | ||
{ | ||
name: 'parca', | ||
whitelist: whitelistPersist, | ||
getStorage: () => (typeof document !== 'undefined' ? CookieStorage : NoopStorage) | ||
} | ||
) | ||
) | ||
} | ||
|
||
export function useCreateStore(initialState) { | ||
// For SSR & SSG, always use a new store. | ||
if (typeof window === 'undefined') { | ||
return () => initializeStore(initialState) | ||
} | ||
|
||
// For CSR, always re-use same store. | ||
store = store ?? initializeStore(initialState) | ||
// And if initialState changes, then merge states in the next render cycle. | ||
// @todo does initialState ever change? | ||
useLayoutEffect(() => { | ||
if (initialState && store) { | ||
store.setState({ | ||
...store.getState(), | ||
...initialState | ||
}) | ||
} | ||
}, [initialState]) | ||
|
||
return () => 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,8 @@ | ||
import { StateStorage } from 'zustand/middleware' | ||
|
||
const NoopStorage: StateStorage = { | ||
setItem: (_key, _value) => {}, | ||
getItem: key => key | ||
} | ||
|
||
export default NoopStorage |
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,22 @@ | ||
import produce from 'immer' | ||
|
||
export type UiState = ReturnType<typeof createSlice> | ||
|
||
export default function createSlice(set, _get) { | ||
return { | ||
// state | ||
ui: { | ||
darkMode: false | ||
}, | ||
// actions | ||
setDarkMode: (mode: boolean) => { | ||
set( | ||
produce<UiState>(state => { | ||
state.ui.darkMode = mode | ||
}) | ||
) | ||
} | ||
} | ||
} | ||
|
||
export const selectUi = (state: UiState) => state.ui |