Skip to content

Commit

Permalink
remove useWeb3Hub db hook
Browse files Browse the repository at this point in the history
  • Loading branch information
Canestin committed Oct 18, 2024
1 parent 07ee26c commit c0ca48a
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 63 deletions.
2 changes: 1 addition & 1 deletion apps/ledger-live-mobile/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Web3HubDB["dismissedManifests"]>(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<AppManifest>();
const [dismissedManifests, setWeb3HubDB] = useDismissedManifests();
const [dismissedManifests, setDismissedManifests] = useDismissedManifests();

useEffect(() => {
if (disclaimerManifest && !!dismissedManifests[disclaimerManifest.id]) {
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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";
39 changes: 9 additions & 30 deletions apps/ledger-live-mobile/src/newArch/features/Web3Hub/db.ts
Original file line number Diff line number Diff line change
@@ -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<Web3HubDB>(() => AsyncStorage);
const emptyRecentlyUsed: Web3HubDB["recentlyUsed"] = [];
export const recentlyUseAtom = storedAtom(RECENTLY_USED_KEY, emptyRecentlyUsed);

export const web3hubAtom = atomWithStorage<Web3HubDB>(
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<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];
export function storedAtom<T = unknown>(key: string, defaultValue: T) {
const storage = createJSONStorage<T>(() => AsyncStorage);
return atomWithStorage<T>(key, defaultValue, storage);
}
2 changes: 1 addition & 1 deletion libs/ledger-live-common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
16 changes: 8 additions & 8 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c0ca48a

Please sign in to comment.