import React, { useEffect } from 'react';
import PropTypes from 'prop-types';
import { useDispatch, useSelector } from 'react-redux';
import { useHistory } from 'react-router-dom';
import { isEqual } from 'lodash';
import Box from '../../ui/box';
import Typography from '../../ui/typography/typography';
import Card from '../../ui/card';
import {
COLORS,
TYPOGRAPHY,
JUSTIFY_CONTENT,
FLEX_DIRECTION,
ALIGN_ITEMS,
DISPLAY,
BLOCK_SIZES,
FLEX_WRAP,
} from '../../../helpers/constants/design-system';
import { ENVIRONMENT_TYPE_POPUP } from '../../../../shared/constants/app';
import { getEnvironmentType } from '../../../../app/scripts/lib/util';
import {
getCurrentChainId,
getIpfsGateway,
getSelectedAddress,
} from '../../../selectors';
import { ASSET_ROUTE } from '../../../helpers/constants/routes';
import { getAssetImageURL } from '../../../helpers/utils/util';
import { updateCollectibleDropDownState } from '../../../store/actions';
import { usePrevious } from '../../../hooks/usePrevious';
import { getCollectiblesDropdownState } from '../../../ducks/metamask/metamask';
import { useI18nContext } from '../../../hooks/useI18nContext';
import CollectibleDefaultImage from '../collectible-default-image';
const width =
getEnvironmentType() === ENVIRONMENT_TYPE_POPUP
? BLOCK_SIZES.ONE_THIRD
: BLOCK_SIZES.ONE_SIXTH;
const PREVIOUSLY_OWNED_KEY = 'previouslyOwned';
export default function CollectiblesItems({
collections = {},
previouslyOwnedCollection = {},
}) {
const dispatch = useDispatch();
const collectionsKeys = Object.keys(collections);
const collectiblesDropdownState = useSelector(getCollectiblesDropdownState);
const previousCollectionKeys = usePrevious(collectionsKeys);
const selectedAddress = useSelector(getSelectedAddress);
const chainId = useSelector(getCurrentChainId);
const t = useI18nContext();
useEffect(() => {
if (
chainId !== undefined &&
selectedAddress !== undefined &&
!isEqual(previousCollectionKeys, collectionsKeys) &&
(collectiblesDropdownState?.[selectedAddress]?.[chainId] === undefined ||
Object.keys(collectiblesDropdownState?.[selectedAddress]?.[chainId])
.length === 0)
) {
const initState = {};
collectionsKeys.forEach((key) => {
initState[key] = true;
});
const newCollectibleDropdownState = {
...collectiblesDropdownState,
[selectedAddress]: {
...collectiblesDropdownState?.[selectedAddress],
[chainId]: initState,
},
};
dispatch(updateCollectibleDropDownState(newCollectibleDropdownState));
}
}, [
collectionsKeys,
previousCollectionKeys,
collectiblesDropdownState,
selectedAddress,
chainId,
dispatch,
]);
const ipfsGateway = useSelector(getIpfsGateway);
const history = useHistory();
const renderCollectionImage = (
isPreviouslyOwnedCollection,
collectionImage,
collectionName,
) => {
if (isPreviouslyOwnedCollection) {
return null;
}
if (collectionImage) {
return (
);
}
return (