-
Notifications
You must be signed in to change notification settings - Fork 333
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
- Loading branch information
1 parent
46a6479
commit fe927b7
Showing
6 changed files
with
124 additions
and
7 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,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
89 changes: 89 additions & 0 deletions
89
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,89 @@ | ||
import { useEffect, useMemo, useState } from "react"; | ||
import { useHideSpamCollection } from "./useHideSpamCollection"; | ||
import { isThresholdValid, 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 chains = ["ethereum", "polygon"]; | ||
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, | ||
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