-
Notifications
You must be signed in to change notification settings - Fork 348
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ✨ Add spamFilteringTx hook in global
feat: ✨ Add spamFilteringTx hook in global review: Enums
- Loading branch information
1 parent
19109d6
commit 4febbaa
Showing
12 changed files
with
143 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
"@ledgerhq/types-live": patch | ||
"ledger-live-desktop": patch | ||
"@ledgerhq/live-common": patch | ||
"@ledgerhq/live-nft-react": patch | ||
--- | ||
|
||
Add useCheckNftAccount Hook |
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,7 @@ | ||
--- | ||
"ledger-live-desktop": patch | ||
"@ledgerhq/live-common": patch | ||
"@ledgerhq/live-nft-react": patch | ||
--- | ||
|
||
use Hook CheckNft in Default and handle global sync of NFTs every 12 hours |
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
88 changes: 88 additions & 0 deletions
88
apps/ledger-live-desktop/src/renderer/hooks/useSyncNFTsWithAccounts.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,88 @@ | ||
import { useEffect, useMemo, useState } from "react"; | ||
import { useHideSpamCollection } from "./useHideSpamCollection"; | ||
import { isThresholdValid, supportedChains, useCheckNftAccount } from "@ledgerhq/live-nft-react"; | ||
import { useFeature } from "@ledgerhq/live-common/featureFlags/index"; | ||
import { useSelector } from "react-redux"; | ||
import { accountsSelector, orderedVisibleNftsSelector } from "../reducers/accounts"; | ||
import isEqual from "lodash/isEqual"; | ||
|
||
/** | ||
* Represents the size of groups for batching address fetching. | ||
* @constant {number} | ||
*/ | ||
const GROUP_SIZE = 20; | ||
|
||
/** | ||
* Represents the timer duration for updating address groups. | ||
* 5 hours = 18,000,000 ms. | ||
* @constant {number} | ||
*/ | ||
const TIMER = 5 * 60 * 60 * 1000; // 5 hours = 18000000 ms | ||
|
||
/** | ||
* A React hook that synchronizes NFT accounts by fetching their data in groups. | ||
* It utilizes address batching and manages updates based on a timer. | ||
* | ||
* @returns {void} | ||
* | ||
* @example | ||
* import { useSyncNFTsWithAccounts } from './path/to/hook'; | ||
* | ||
* const MyComponent = () => { | ||
* useSyncNFTsWithAccounts(); | ||
* return <div>Syncing NFT Accounts...</div>; | ||
* }; | ||
*/ | ||
|
||
export function useSyncNFTsWithAccounts() { | ||
const nftsFromSimplehashFeature = useFeature("nftsFromSimplehash"); | ||
const threshold = isThresholdValid(Number(nftsFromSimplehashFeature?.params?.threshold)) | ||
? Number(nftsFromSimplehashFeature?.params?.threshold) | ||
: 75; | ||
|
||
const { enabled, hideSpamCollection } = useHideSpamCollection(); | ||
|
||
const accounts = useSelector(accountsSelector); | ||
const nftsOwned = useSelector(orderedVisibleNftsSelector, isEqual); | ||
|
||
const addressGroups = useMemo(() => { | ||
const uniqueAddresses = [ | ||
...new Set( | ||
accounts.map(account => account.freshAddress).filter(addr => addr.startsWith("0x")), | ||
), | ||
]; | ||
|
||
return uniqueAddresses.reduce<string[][]>((acc, _, i, arr) => { | ||
if (i % GROUP_SIZE === 0) { | ||
acc.push(arr.slice(i, i + GROUP_SIZE)); | ||
} | ||
return acc; | ||
}, []); | ||
}, [accounts]); | ||
|
||
const [groupToFetch, setGroupToFetch] = useState(addressGroups[0]); | ||
const [, setCurrentIndex] = useState(0); | ||
|
||
useEffect(() => { | ||
if (!enabled) return; | ||
const interval = setInterval(() => { | ||
setCurrentIndex(prevIndex => { | ||
const nextIndex = (prevIndex + 1) % addressGroups.length; | ||
setGroupToFetch(addressGroups[nextIndex]); | ||
|
||
return nextIndex; | ||
}); | ||
}, TIMER); | ||
|
||
return () => clearInterval(interval); | ||
}, [addressGroups, enabled]); | ||
|
||
useCheckNftAccount({ | ||
addresses: groupToFetch.join(","), | ||
nftsOwned, | ||
chains: supportedChains, | ||
threshold, | ||
action: hideSpamCollection, | ||
enabled, | ||
}); | ||
} |
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
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,6 @@ | ||
export enum Chain { | ||
ETHEREUM = "ethereum", | ||
POLYGON = "polygon", | ||
} | ||
|
||
export const supportedChains = [Chain.ETHEREUM, Chain.POLYGON]; |