-
Notifications
You must be signed in to change notification settings - Fork 345
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Rework hiddenCollections to handle more easily displays (#8457)
- Loading branch information
1 parent
edb3910
commit d45e871
Showing
26 changed files
with
322 additions
and
120 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": minor | ||
"@ledgerhq/live-nft-react": minor | ||
"@ledgerhq/live-nft": minor | ||
--- | ||
|
||
Rework Hiddencollections |
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 |
---|---|---|
@@ -1,4 +1,7 @@ | ||
// Action types | ||
|
||
/** Settings --------- */ | ||
export const TOGGLE_MEMOTAG_INFO = "settings/toggleShouldDisplayMemoTagInfo"; | ||
export const TOGGLE_MEV = "settings/toggleMEV"; | ||
export const TOGGLE_MARKET_WIDGET = "settings/toggleMarketWidget"; | ||
export const UPDATE_NFT_COLLECTION_STATUS = "settings/updateNftCollectionStatus"; |
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/nfts/__tests__/useNftCollectionsStatus.test.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 { renderHook } from "tests/testUtils"; | ||
import { INITIAL_STATE } from "~/renderer/reducers/settings"; | ||
import { BlockchainEVM } from "@ledgerhq/live-nft/supported"; | ||
import { NftStatus } from "@ledgerhq/live-nft/types"; | ||
import { useNftCollectionsStatus } from "../useNftCollectionsStatus"; | ||
|
||
describe("useNftCollectionsStatus", () => { | ||
it("should returns only NFTs (contract) with NftStatus !== whitelisted when FF is ON", () => { | ||
const { result } = renderHook(() => useNftCollectionsStatus(), { | ||
initialState: { | ||
settings: { | ||
...INITIAL_STATE, | ||
overriddenFeatureFlags: { | ||
nftsFromSimplehash: { | ||
enabled: true, | ||
}, | ||
}, | ||
nftCollectionsStatusByNetwork: { | ||
[BlockchainEVM.Ethereum]: { | ||
collectionETHA: NftStatus.whitelisted, | ||
collectionETHB: NftStatus.blacklisted, | ||
collectionETHC: NftStatus.spam, | ||
collectionETHD: NftStatus.spam, | ||
}, | ||
[BlockchainEVM.Avalanche]: { | ||
collectionAVAX1: NftStatus.blacklisted, | ||
collectionAVAX2: NftStatus.spam, | ||
collectionAVAX3: NftStatus.blacklisted, | ||
}, | ||
[BlockchainEVM.Polygon]: { | ||
collectionP1: NftStatus.blacklisted, | ||
collectionP2: NftStatus.whitelisted, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
expect(result.current.hiddenNftCollections).toEqual([ | ||
"collectionETHB", | ||
"collectionETHC", | ||
"collectionETHD", | ||
"collectionAVAX1", | ||
"collectionAVAX2", | ||
"collectionAVAX3", | ||
"collectionP1", | ||
]); | ||
}); | ||
|
||
it("should returns only NFTs (contract) with NftStatus.blacklisted when FF is oFF ", () => { | ||
const { result } = renderHook(() => useNftCollectionsStatus(), { | ||
initialState: { | ||
settings: { | ||
...INITIAL_STATE, | ||
overriddenFeatureFlags: { | ||
nftsFromSimplehash: { | ||
enabled: false, | ||
}, | ||
}, | ||
nftCollectionsStatusByNetwork: { | ||
[BlockchainEVM.Ethereum]: { | ||
collectionETHA: NftStatus.whitelisted, | ||
collectionETHB: NftStatus.blacklisted, | ||
collectionETHC: NftStatus.spam, | ||
collectionETHD: NftStatus.spam, | ||
}, | ||
[BlockchainEVM.Avalanche]: { | ||
collectionAVAX1: NftStatus.blacklisted, | ||
collectionAVAX2: NftStatus.spam, | ||
collectionAVAX3: NftStatus.blacklisted, | ||
}, | ||
[BlockchainEVM.Polygon]: { | ||
collectionP1: NftStatus.blacklisted, | ||
collectionP2: NftStatus.whitelisted, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
expect(result.current.hiddenNftCollections).toEqual([ | ||
"collectionETHB", | ||
"collectionAVAX1", | ||
"collectionAVAX3", | ||
"collectionP1", | ||
]); | ||
}); | ||
}); |
25 changes: 17 additions & 8 deletions
25
apps/ledger-live-desktop/src/renderer/hooks/nfts/useHideSpamCollection.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 |
---|---|---|
@@ -1,25 +1,34 @@ | ||
import { useCallback } from "react"; | ||
import { useDispatch, useSelector } from "react-redux"; | ||
import { useFeature } from "@ledgerhq/live-common/featureFlags/index"; | ||
import { whitelistedNftCollectionsSelector } from "~/renderer/reducers/settings"; | ||
import { hideNftCollection } from "~/renderer/actions/settings"; | ||
import { nftCollectionsStatusByNetworkSelector } from "~/renderer/reducers/settings"; | ||
import { updateNftStatus } from "~/renderer/actions/settings"; | ||
import { BlockchainsType } from "@ledgerhq/live-nft/supported"; | ||
import { NftStatus } from "@ledgerhq/live-nft/types"; | ||
|
||
export function useHideSpamCollection() { | ||
const spamFilteringTxFeature = useFeature("spamFilteringTx"); | ||
const whitelistedNftCollections = useSelector(whitelistedNftCollectionsSelector); | ||
const nftsFromSimplehashFeature = useFeature("nftsFromSimplehash"); | ||
|
||
const nftCollectionsStatusByNetwork = useSelector(nftCollectionsStatusByNetworkSelector); | ||
|
||
const dispatch = useDispatch(); | ||
|
||
const hideSpamCollection = useCallback( | ||
(collection: string) => { | ||
if (!whitelistedNftCollections.includes(collection)) { | ||
dispatch(hideNftCollection(collection)); | ||
(collection: string, blockchain: BlockchainsType) => { | ||
const elem = Object.entries(nftCollectionsStatusByNetwork).find( | ||
([key]) => key === blockchain, | ||
)?.[1]; | ||
|
||
if (!elem) { | ||
dispatch(updateNftStatus(blockchain, collection, NftStatus.spam)); | ||
} | ||
}, | ||
[dispatch, whitelistedNftCollections], | ||
[dispatch, nftCollectionsStatusByNetwork], | ||
); | ||
|
||
return { | ||
hideSpamCollection, | ||
enabled: spamFilteringTxFeature?.enabled, | ||
enabled: spamFilteringTxFeature?.enabled && nftsFromSimplehashFeature?.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
41 changes: 41 additions & 0 deletions
41
apps/ledger-live-desktop/src/renderer/hooks/nfts/useNftCollectionsStatus.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,41 @@ | ||
import { useMemo } from "react"; | ||
import { useSelector } from "react-redux"; | ||
import { useFeature } from "@ledgerhq/live-common/featureFlags/index"; | ||
import { nftCollectionsStatusByNetworkSelector } from "~/renderer/reducers/settings"; | ||
import { NftStatus } from "@ledgerhq/live-nft/types"; | ||
import { BlockchainEVM } from "@ledgerhq/live-nft/supported"; | ||
|
||
export function useNftCollectionsStatus() { | ||
const nftsFromSimplehashFeature = useFeature("nftsFromSimplehash"); | ||
const nftCollectionsStatusByNetwork = useSelector(nftCollectionsStatusByNetworkSelector); | ||
|
||
const shouldDisplaySpams = !nftsFromSimplehashFeature?.enabled; | ||
|
||
const nftCollectionParser = ( | ||
nftCollection: Record<BlockchainEVM, Record<string, NftStatus>>, | ||
applyFilterFn: (arg0: [string, NftStatus]) => boolean, | ||
) => | ||
Object.values(nftCollection).flatMap(contracts => | ||
Object.entries(contracts) | ||
.filter(applyFilterFn) | ||
.map(([contract]) => contract), | ||
); | ||
|
||
const list = useMemo(() => { | ||
return nftCollectionParser(nftCollectionsStatusByNetwork, ([_, status]) => | ||
!shouldDisplaySpams ? status !== NftStatus.whitelisted : status === NftStatus.blacklisted, | ||
); | ||
}, [nftCollectionsStatusByNetwork, shouldDisplaySpams]); | ||
|
||
const whitelisted = useMemo(() => { | ||
return nftCollectionParser( | ||
nftCollectionsStatusByNetwork, | ||
([_, status]) => status === NftStatus.whitelisted, | ||
); | ||
}, [nftCollectionsStatusByNetwork]); | ||
|
||
return { | ||
hiddenNftCollections: list, | ||
whitelistedNftCollections: whitelisted, | ||
}; | ||
} |
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
Oops, something went wrong.