-
Notifications
You must be signed in to change notification settings - Fork 324
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(web3Hub): fix state management with jotai
- Loading branch information
Showing
4 changed files
with
44 additions
and
14 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
36 changes: 36 additions & 0 deletions
36
apps/ledger-live-mobile/src/newArch/features/Web3Hub/db.ts
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,36 @@ | ||
import AsyncStorage from "@react-native-async-storage/async-storage"; | ||
import { useAtom } from "jotai"; | ||
import { createJSONStorage } from "jotai/utils"; | ||
import { useCallback, useMemo } from "react"; | ||
import { Web3HubDB } from "LLM/features/Web3Hub/types"; | ||
import { atomWithStorage } from "jotai/utils"; | ||
import { INITIAL_WEB3HUB_STATE, WEB3HUB_STORE_KEY } from "./constants"; | ||
|
||
export const storage = createJSONStorage<Web3HubDB>(() => AsyncStorage); | ||
|
||
export const web3hubAtom = atomWithStorage<Web3HubDB>( | ||
WEB3HUB_STORE_KEY, | ||
INITIAL_WEB3HUB_STATE, | ||
storage, | ||
); | ||
|
||
type NewState = Web3HubDB | ((s: Web3HubDB) => Web3HubDB); | ||
|
||
export function useWeb3HubDB<Selected>( | ||
selector: (state: Web3HubDB) => Selected, | ||
): [Selected, (v: NewState) => void] { | ||
const [state, setState] = useAtom(web3hubAtom); | ||
|
||
const setter = useCallback( | ||
(newState: NewState) => { | ||
const val = typeof newState === "function" ? newState(state) : newState; | ||
|
||
setState(val); | ||
}, | ||
// eslint-disable-next-line | ||
[state], | ||
); | ||
|
||
const result = useMemo(() => selector(state), [state, selector]); | ||
return [result, setter]; | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.