mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-11-26 12:29:06 +01:00
33cc8d587a
* replaced all the instances of collectibles with nfts * updated actions * updated e2e seeder * updated confirm Approve test * updated test dapp change * updated test dapp change * nit fix * nit fix * updated casing and snapshots * updated casinG * added migrations * updated ,igration * updated 078.test * updated tests for 078 migration * updated migration * updated 078 index.js
75 lines
2.4 KiB
JavaScript
75 lines
2.4 KiB
JavaScript
import { useEffect, useState } from 'react';
|
|
import { useSelector } from 'react-redux';
|
|
import { isEqual } from 'lodash';
|
|
import { getNfts, getNftContracts } from '../ducks/metamask/metamask';
|
|
import { getCurrentChainId, getSelectedAddress } from '../selectors';
|
|
import { usePrevious } from './usePrevious';
|
|
|
|
export function useNftsCollections() {
|
|
const [collections, setCollections] = useState({});
|
|
const [previouslyOwnedCollection, setPreviouslyOwnedCollection] = useState({
|
|
collectionName: 'Previously Owned',
|
|
nfts: [],
|
|
});
|
|
const nfts = useSelector(getNfts);
|
|
const [nftsLoading, setNftsLoading] = useState(() => nfts?.length >= 0);
|
|
const selectedAddress = useSelector(getSelectedAddress);
|
|
const chainId = useSelector(getCurrentChainId);
|
|
const nftContracts = useSelector(getNftContracts);
|
|
const prevNfts = usePrevious(nfts);
|
|
const prevChainId = usePrevious(chainId);
|
|
const prevSelectedAddress = usePrevious(selectedAddress);
|
|
useEffect(() => {
|
|
const getCollections = () => {
|
|
setNftsLoading(true);
|
|
if (selectedAddress === undefined || chainId === undefined) {
|
|
return;
|
|
}
|
|
const newCollections = {};
|
|
const newPreviouslyOwnedCollections = {
|
|
collectionName: 'Previously Owned',
|
|
nfts: [],
|
|
};
|
|
|
|
nfts.forEach((nft) => {
|
|
if (nft?.isCurrentlyOwned === false) {
|
|
newPreviouslyOwnedCollections.nfts.push(nft);
|
|
} else if (newCollections[nft.address]) {
|
|
newCollections[nft.address].nfts.push(nft);
|
|
} else {
|
|
const collectionContract = nftContracts.find(
|
|
({ address }) => address === nft.address,
|
|
);
|
|
newCollections[nft.address] = {
|
|
collectionName: collectionContract?.name || nft.name,
|
|
collectionImage: collectionContract?.logo || nft.image,
|
|
nfts: [nft],
|
|
};
|
|
}
|
|
});
|
|
setCollections(newCollections);
|
|
setPreviouslyOwnedCollection(newPreviouslyOwnedCollections);
|
|
setNftsLoading(false);
|
|
};
|
|
|
|
if (
|
|
!isEqual(prevNfts, nfts) ||
|
|
!isEqual(prevSelectedAddress, selectedAddress) ||
|
|
!isEqual(prevChainId, chainId)
|
|
) {
|
|
getCollections();
|
|
}
|
|
}, [
|
|
nfts,
|
|
prevNfts,
|
|
nftContracts,
|
|
setNftsLoading,
|
|
chainId,
|
|
prevChainId,
|
|
selectedAddress,
|
|
prevSelectedAddress,
|
|
]);
|
|
|
|
return { nftsLoading, collections, previouslyOwnedCollection };
|
|
}
|