From c0ca48a6a09cba6ae1b8380d4aabe03a295db38e Mon Sep 17 00:00:00 2001 From: Canestin Date: Fri, 18 Oct 2024 16:15:18 +0200 Subject: [PATCH] remove useWeb3Hub db hook --- apps/ledger-live-mobile/package.json | 2 +- .../Disclaimer/useDisclaimerViewModel.ts | 26 ++++++------- .../src/newArch/features/Web3Hub/constants.ts | 11 +----- .../src/newArch/features/Web3Hub/db.ts | 39 +++++-------------- libs/ledger-live-common/package.json | 2 +- pnpm-lock.yaml | 16 ++++---- 6 files changed, 33 insertions(+), 63 deletions(-) diff --git a/apps/ledger-live-mobile/package.json b/apps/ledger-live-mobile/package.json index 38a899b9b72f..3fdcaf75d6b6 100644 --- a/apps/ledger-live-mobile/package.json +++ b/apps/ledger-live-mobile/package.json @@ -154,7 +154,7 @@ "hoist-non-react-statics": "3.3.2", "i18next": "20.6.1", "invariant": "2.2.4", - "jotai": "^2.7.0", + "jotai": "^2.10.1", "json-rpc-2.0": "^0.2.19", "lodash": "4.17.21", "lottie-react-native": "^6.7.0", diff --git a/apps/ledger-live-mobile/src/newArch/features/Web3Hub/components/Disclaimer/useDisclaimerViewModel.ts b/apps/ledger-live-mobile/src/newArch/features/Web3Hub/components/Disclaimer/useDisclaimerViewModel.ts index ed468be832f8..cef863baf556 100644 --- a/apps/ledger-live-mobile/src/newArch/features/Web3Hub/components/Disclaimer/useDisclaimerViewModel.ts +++ b/apps/ledger-live-mobile/src/newArch/features/Web3Hub/components/Disclaimer/useDisclaimerViewModel.ts @@ -1,19 +1,17 @@ import { useCallback, useEffect, useState } from "react"; import { AppManifest } from "@ledgerhq/live-common/wallet-api/types"; -import { Web3HubDB } from "LLM/features/Web3Hub/types"; -import { useWeb3HubDB } from "LLM/features/Web3Hub/db"; - -const dismissedManifestsSelector = (state: Web3HubDB) => state.dismissedManifests; +import { dismissedManifestsAtom } from "LLM/features/Web3Hub/db"; +import { useAtom } from "jotai"; export function useDismissedManifests() { - return useWeb3HubDB(dismissedManifestsSelector); + return useAtom(dismissedManifestsAtom); } export default function useDisclaimerViewModel(goToApp: (manifestId: string) => void) { const [isChecked, setIsChecked] = useState(false); const [disclaimerOpened, setDisclaimerOpened] = useState(false); const [disclaimerManifest, setDisclaimerManifest] = useState(); - const [dismissedManifests, setWeb3HubDB] = useDismissedManifests(); + const [dismissedManifests, setDismissedManifests] = useDismissedManifests(); useEffect(() => { if (disclaimerManifest && !!dismissedManifests[disclaimerManifest.id]) { @@ -47,18 +45,18 @@ export default function useDisclaimerViewModel(goToApp: (manifestId: string) => const onConfirm = useCallback(() => { if (disclaimerManifest) { if (isChecked) { - setWeb3HubDB(state => ({ - ...state, - dismissedManifests: { - ...state.dismissedManifests, - [disclaimerManifest.id]: !state.dismissedManifests[disclaimerManifest.id], - }, - })); + setDismissedManifests(async state => { + const s = await state; + return { + ...s, + [disclaimerManifest.id]: !s[disclaimerManifest.id], + }; + }); } goToApp(disclaimerManifest.id); } - }, [disclaimerManifest, goToApp, isChecked, setWeb3HubDB]); + }, [disclaimerManifest, goToApp, isChecked, setDismissedManifests]); const onClose = useCallback(() => { setDisclaimerOpened(false); diff --git a/apps/ledger-live-mobile/src/newArch/features/Web3Hub/constants.ts b/apps/ledger-live-mobile/src/newArch/features/Web3Hub/constants.ts index 06ec52d27a8c..3824e9840c90 100644 --- a/apps/ledger-live-mobile/src/newArch/features/Web3Hub/constants.ts +++ b/apps/ledger-live-mobile/src/newArch/features/Web3Hub/constants.ts @@ -1,11 +1,4 @@ -import type { Web3HubDB } from "./types"; - export const URL_ORIGIN = "https://live-app-catalog.ledger.com"; -export const WEB3HUB_STORE_KEY = "web3hub"; - -export const INITIAL_WEB3HUB_STATE: Web3HubDB = { - recentlyUsed: [], - dismissedManifests: {}, - // localLiveApp: [], -}; +export const RECENTLY_USED_KEY = "WEB3HUB_RECENTLY_USED"; +export const DISSMISSED_MANIFETS_KEY = "WEB3HUB_DISSMISSED_MANIFETS"; diff --git a/apps/ledger-live-mobile/src/newArch/features/Web3Hub/db.ts b/apps/ledger-live-mobile/src/newArch/features/Web3Hub/db.ts index ea809c828d11..ff20a989c877 100644 --- a/apps/ledger-live-mobile/src/newArch/features/Web3Hub/db.ts +++ b/apps/ledger-live-mobile/src/newArch/features/Web3Hub/db.ts @@ -1,36 +1,15 @@ 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"; +import { atomWithStorage, createJSONStorage } from "jotai/utils"; +import { DISSMISSED_MANIFETS_KEY, RECENTLY_USED_KEY } from "./constants"; -export const storage = createJSONStorage(() => AsyncStorage); +const emptyRecentlyUsed: Web3HubDB["recentlyUsed"] = []; +export const recentlyUseAtom = storedAtom(RECENTLY_USED_KEY, emptyRecentlyUsed); -export const web3hubAtom = atomWithStorage( - WEB3HUB_STORE_KEY, - INITIAL_WEB3HUB_STATE, - storage, -); +const emptyDismissedManifests: Web3HubDB["dismissedManifests"] = {}; +export const dismissedManifestsAtom = storedAtom(DISSMISSED_MANIFETS_KEY, emptyDismissedManifests); -type NewState = Web3HubDB | ((s: Web3HubDB) => Web3HubDB); - -export function useWeb3HubDB( - 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]; +export function storedAtom(key: string, defaultValue: T) { + const storage = createJSONStorage(() => AsyncStorage); + return atomWithStorage(key, defaultValue, storage); } diff --git a/libs/ledger-live-common/package.json b/libs/ledger-live-common/package.json index 98a6e7357171..8cf9c8db8b7f 100644 --- a/libs/ledger-live-common/package.json +++ b/libs/ledger-live-common/package.json @@ -224,7 +224,7 @@ "invariant": "^2.2.2", "iso-filecoin": "^4.1.0", "isomorphic-ws": "^4.0.1", - "jotai": "^2.7.0", + "jotai": "^2.10.1", "json-rpc-2.0": "^0.2.19", "lodash": "^4.17.21", "minimatch": "^5.1.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 7971c0525799..12aae479d8b5 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1053,8 +1053,8 @@ importers: specifier: 2.2.4 version: 2.2.4 jotai: - specifier: ^2.7.0 - version: 2.7.1(@types/react@18.2.73)(react@18.2.0) + specifier: ^2.10.1 + version: 2.10.1(@types/react@18.2.73)(react@18.2.0) json-rpc-2.0: specifier: ^0.2.19 version: 0.2.19 @@ -3773,8 +3773,8 @@ importers: specifier: ^4.0.1 version: 4.0.1(ws@7.5.10) jotai: - specifier: ^2.7.0 - version: 2.7.1(@types/react@18.2.73)(react@18.2.0) + specifier: ^2.10.1 + version: 2.10.1(@types/react@18.2.73)(react@18.2.0) json-rpc-2.0: specifier: ^0.2.19 version: 0.2.19 @@ -22620,8 +22620,8 @@ packages: join-component@1.1.0: resolution: {integrity: sha512-bF7vcQxbODoGK1imE2P9GS9aw4zD0Sd+Hni68IMZLj7zRnquH7dXUmMw9hDI5S/Jzt7q+IyTXN0rSg2GI0IKhQ==} - jotai@2.7.1: - resolution: {integrity: sha512-bsaTPn02nFgWNP6cBtg/htZhCu4s0wxqoklRHePp6l/vlsypR9eLn7diRliwXYWMXDpPvW/LLA2afI8vwgFFaw==} + jotai@2.10.1: + resolution: {integrity: sha512-4FycO+BOTl2auLyF2Chvi6KTDqdsdDDtpaL/WHQMs8f3KS1E3loiUShQzAzFA/sMU5cJ0hz/RT1xum9YbG/zaA==} engines: {node: '>=12.20.0'} peerDependencies: '@types/react': '>=17.0.0' @@ -43108,7 +43108,7 @@ snapshots: commander: 8.3.0 deepmerge: 4.3.1 glob: 7.2.3 - jotai: 2.7.1(@types/react@18.2.73)(react@18.2.0) + jotai: 2.10.1(@types/react@18.2.73)(react@18.2.0) prettier: 2.8.8 react: 18.2.0 react-native: 0.74.6(@babel/core@7.24.3)(@babel/preset-env@7.24.3(@babel/core@7.24.3))(@types/react@18.2.73)(metro-resolver@0.80.12)(metro-transform-worker@0.80.12)(react@18.2.0) @@ -55257,7 +55257,7 @@ snapshots: join-component@1.1.0: {} - jotai@2.7.1(@types/react@18.2.73)(react@18.2.0): + jotai@2.10.1(@types/react@18.2.73)(react@18.2.0): optionalDependencies: '@types/react': 18.2.73 react: 18.2.0