Skip to content

Commit

Permalink
[FEAT] - [LLM] - Remove Spams NFT in Accounts Page (#6648)
Browse files Browse the repository at this point in the history
* Remove Spams NFT in Accounts

* Chnageset
  • Loading branch information
mcayuelas-ledger committed Apr 15, 2024
1 parent f411a5b commit 65754a6
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changeset/four-yaks-float.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"live-mobile": patch
---

Remove Spams NFTs from Account Page
25 changes: 19 additions & 6 deletions apps/ledger-live-mobile/src/screens/Account/NftCollectionsList.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useCallback, useMemo, useState } from "react";

import { useSelector } from "react-redux";
import { Box, Text } from "@ledgerhq/native-ui";
import { Box, InfiniteLoader, Text } from "@ledgerhq/native-ui";
import { Trans, useTranslation } from "react-i18next";
import { StyleSheet, View, FlatList } from "react-native";
import useEnv from "@ledgerhq/live-common/hooks/useEnv";
Expand All @@ -16,6 +16,8 @@ import { NavigatorName, ScreenName } from "~/const";
import Button from "~/components/wrappedUi/Button";
import Touchable from "~/components/Touchable";
import SectionTitle from "../WalletCentricSections/SectionTitle";
import useFeature from "@ledgerhq/live-common/featureFlags/useFeature";
import { useNftGalleryFilter, isThresholdValid } from "@ledgerhq/live-nft-react";

const MAX_COLLECTIONS_TO_SHOW = 3;

Expand All @@ -25,19 +27,27 @@ type Props = {

export default function NftCollectionsList({ account }: Props) {
useEnv("HIDE_EMPTY_TOKEN_ACCOUNTS");

const nftsFromSimplehashFeature = useFeature("nftsFromSimplehash");
const thresold = nftsFromSimplehashFeature?.params?.threshold;
const nftsFromSimplehashEnabled = nftsFromSimplehashFeature?.enabled;
const { colors } = useTheme();
const { t } = useTranslation();
const navigation = useNavigation();
const { nfts } = account;

const { nfts, isLoading } = useNftGalleryFilter({
nftsOwned: account.nfts || [],
addresses: account.freshAddress,
chains: [account.currency.id],
threshold: isThresholdValid(thresold) ? Number(thresold) : 75,
});

const hiddenNftCollections = useSelector(hiddenNftCollectionsSelector);
const nftCollections = useMemo(
() =>
Object.entries(nftsByCollections(nfts)).filter(
Object.entries(nftsByCollections(nftsFromSimplehashEnabled ? nfts : account.nfts)).filter(
([contract]) => !hiddenNftCollections.includes(`${account.id}|${contract}`),
),
[account.id, hiddenNftCollections, nfts],
[account.id, account.nfts, hiddenNftCollections, nfts, nftsFromSimplehashEnabled],
) as [string, ProtoNFT[]][];

const [isCollectionMenuOpen, setIsCollectionMenuOpen] = useState(false);
Expand Down Expand Up @@ -112,7 +122,9 @@ export default function NftCollectionsList({ account }: Props) {

const renderFooter = useCallback(
() =>
data.length ? (
isLoading ? (
<InfiniteLoader />
) : data.length ? (
<Button
type={"shade"}
size={"small"}
Expand Down Expand Up @@ -153,6 +165,7 @@ export default function NftCollectionsList({ account }: Props) {
account.currency.family,
colors.fog,
data.length,
isLoading,
navigateToGallery,
navigateToReceiveConnectDevice,
],
Expand Down
21 changes: 18 additions & 3 deletions apps/ledger-live-mobile/src/screens/Nft/NftGallery/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { AccountsNavigatorParamList } from "~/components/RootNavigator/types/Acc
import InfoModal from "~/modals/Info";
import { notAvailableModalInfo } from "../NftInfoNotAvailable";
import invariant from "invariant";
import { useNftGalleryFilter, isThresholdValid } from "@ledgerhq/live-nft-react";

const MAX_COLLECTIONS_FIRST_RENDER = 12;
const COLLECTIONS_TO_ADD_ON_LIST_END_REACHED = 6;
Expand All @@ -30,6 +31,9 @@ type NavigationProps = BaseComposite<
>;

const NftGallery = () => {
const nftsFromSimplehashFeature = useFeature("nftsFromSimplehash");
const thresold = nftsFromSimplehashFeature?.params?.threshold;
const nftsFromSimplehashEnabled = nftsFromSimplehashFeature?.enabled;
const navigation = useNavigation<NavigationProps["navigation"]>();
const { t } = useTranslation();
const { params } = useRoute<NavigationProps["route"]>();
Expand All @@ -47,14 +51,22 @@ const NftGallery = () => {
setOpen(false);
}, []);

const { nfts, fetchNextPage, hasNextPage } = useNftGalleryFilter({
nftsOwned: account.nfts || [],
addresses: account.freshAddress,
chains: [account.currency.id],
threshold: isThresholdValid(thresold) ? Number(thresold) : 75,
});

const hiddenNftCollections = useSelector(hiddenNftCollectionsSelector);
const collections = useMemo(
() =>
Object.entries(nftsByCollections(account.nfts)).filter(
Object.entries(nftsByCollections(nftsFromSimplehashEnabled ? nfts : account.nfts)).filter(
([contract]) => !hiddenNftCollections.includes(`${account.id}|${contract}`),
),
[account, hiddenNftCollections],
[account.id, account.nfts, hiddenNftCollections, nfts, nftsFromSimplehashEnabled],
) as [string, ProtoNFT[]][];

const [collectionsCount, setCollectionsCount] = useState(MAX_COLLECTIONS_FIRST_RENDER);
const collectionsSlice: Array<ProtoNFT[]> = useMemo(
() => collections.slice(0, collectionsCount).map(([, collection]) => collection),
Expand All @@ -76,7 +88,10 @@ const NftGallery = () => {

const onEndReached = useCallback(() => {
setCollectionsCount(collectionsCount + COLLECTIONS_TO_ADD_ON_LIST_END_REACHED);
}, [collectionsCount]);
if (hasNextPage) {
fetchNextPage();
}
}, [collectionsCount, fetchNextPage, hasNextPage]);

const goToCollectionSelection = useCallback(
() =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import React, { useCallback, useMemo, useState, memo } from "react";

import { View, StyleSheet, FlatList, TouchableOpacity, Platform } from "react-native";
import { nftsByCollections } from "@ledgerhq/live-nft";
import { useNftCollectionMetadata, useNftMetadata } from "@ledgerhq/live-nft-react";
import {
isThresholdValid,
useNftCollectionMetadata,
useNftGalleryFilter,
useNftMetadata,
} from "@ledgerhq/live-nft-react";
import type { Account, ProtoNFT } from "@ledgerhq/types-live";
import { useSelector } from "react-redux";
import { useNavigation, useTheme } from "@react-navigation/native";
Expand All @@ -19,6 +24,7 @@ import {
StackNavigatorProps,
} from "~/components/RootNavigator/types/helpers";
import { SendFundsNavigatorStackParamList } from "~/components/RootNavigator/types/SendFundsNavigator";
import useFeature from "@ledgerhq/live-common/featureFlags/useFeature";

const MAX_COLLECTIONS_FIRST_RENDER = 8;
const COLLECTIONS_TO_ADD_ON_LIST_END_REACHED = 8;
Expand Down Expand Up @@ -84,26 +90,38 @@ const SendFundsSelectCollection = ({ route }: Props) => {
const { params } = route;
const { account } = params;
const { colors } = useTheme();

const nftsFromSimplehashFeature = useFeature("nftsFromSimplehash");
const thresold = nftsFromSimplehashFeature?.params?.threshold;
const nftsFromSimplehashEnabled = nftsFromSimplehashFeature?.enabled;
const hiddenNftCollections = useSelector(hiddenNftCollectionsSelector);

const { nfts, fetchNextPage, hasNextPage } = useNftGalleryFilter({
nftsOwned: account.nfts || [],
addresses: account.freshAddress,
chains: [account.currency.id],
threshold: isThresholdValid(thresold) ? Number(thresold) : 75,
});

const [collectionsCount, setCollectionsCount] = useState(MAX_COLLECTIONS_FIRST_RENDER);
const collections = useMemo(
() =>
Object.entries(nftsByCollections(account.nfts)).filter(
Object.entries(nftsByCollections(nftsFromSimplehashEnabled ? nfts : account.nfts)).filter(
([contract]) => !hiddenNftCollections.includes(`${account.id}|${contract}`),
),
[account.id, account.nfts, hiddenNftCollections],
[account.id, account.nfts, hiddenNftCollections, nfts, nftsFromSimplehashEnabled],
) as [string, ProtoNFT[]][];

const collectionsSlice: Array<ProtoNFT[]> = useMemo(
() => collections.slice(0, collectionsCount).map(([, collection]) => collection),
[collections, collectionsCount],
);
const onEndReached = useCallback(
() => setCollectionsCount(collectionsCount + COLLECTIONS_TO_ADD_ON_LIST_END_REACHED),
[collectionsCount, setCollectionsCount],
);
const onEndReached = useCallback(() => {
setCollectionsCount(collectionsCount + COLLECTIONS_TO_ADD_ON_LIST_END_REACHED);

if (hasNextPage) {
fetchNextPage();
}
}, [collectionsCount, fetchNextPage, hasNextPage]);

const renderItem = useCallback(
({ item }: { item: ProtoNFT[] }) => <CollectionRow account={account} collection={item} />,
Expand Down

0 comments on commit 65754a6

Please sign in to comment.