import React, { useEffect, useState } from 'react'; import { useSpring } from 'react-spring'; import { useTheme } from 'styled-components'; import { getImageSource } from '../../helpers/getImageSource'; import { usePrevious } from '../../hooks'; import { ItemMiniCardProps } from '../../interfaces'; import FavoriteButton from '../FavoriteButton'; import FavoriteToast from '../FavoriteToast'; import Typography from '../Typography'; import { BoxWrapper, InnerItemContainer, Image, ItemInformationCard, ItemNameContainer, } from './styles'; const ItemMiniCard: React.FC = ({ itemSrc, onClick, itemName, itemUsername, isFavorite, onFavoriteClick, showFavorite = true, favoriteCount = 0, }) => { const [hover, setHover] = useState(); const { colors } = useTheme(); const prevIsFavorite = usePrevious(isFavorite); const [showFavoriteToast, setShowFavoriteToast] = useState(false); useEffect(() => { if (isFavorite && prevIsFavorite === false) { // show favorite toast for 3 seconds setShowFavoriteToast(true); setTimeout(() => { setShowFavoriteToast(false); }, 3000); } }, [isFavorite]); const expand = useSpring({ opacity: hover ? 1 : 0, }); return ( setHover(true)} onMouseLeave={() => setHover(false)} onClick={onClick} > {hover && showFavorite && ( { e.preventDefault(); e.stopPropagation(); onFavoriteClick?.(); }} size="small" top={4} right={4} favoriteCount={favoriteCount} /> )} {hover && itemUsername && ( {itemName} @{itemUsername} )} ); }; export default ItemMiniCard;