import React, { useRef, useEffect, useState, useCallback, } from 'react'; import { CSSTransition } from 'react-transition-group'; import { motion } from 'framer-motion'; import { theme, useHover, Popover, Tooltip, } from '@veeqo/ui'; import generateId from 'utils/generateId'; import ConditonalWrapper from 'components/common/ConditionalWrapper'; import { IStoreIcon, StoreIconProps, StoreIconsProps, StoreIconState, } from './types'; import { Container, Integration, IconWrap, ShowMore, Text, ItemsCard, Item, InactiveCover, iconOffset, } from './styled'; const iconWidth = 21.8; const StoreIcon = ({ index, icon, onClick = () => { }, animated = true, hasTooltip = true, }: StoreIconProps) => { const [isHovered, handleEnter, handleLeave] = useHover(); const shouldShowInactive = icon.state === StoreIconState.inactive; const handleIconClick = useCallback( () => onClick(icon), [icon, onClick], ); return ( {children}} > {shouldShowInactive && } ); }; const StoreIcons = ({ icons, onClick = () => { }, numberOfShownIcons = 3, disableInteraction = false, }: StoreIconsProps) => { const [isShowMoreHovered, showMoreEnter, showMoreLeave] = useHover(); const [isPopoverHovered, onPopoverEnter, onPopoverLeave] = useHover(); const [isClicked, setIsClicked] = useState(false); const shownIconsWidth = numberOfShownIcons * iconWidth - (numberOfShownIcons - 1) * iconOffset; const [totalWidth, setTotalWidth] = useState(shownIconsWidth); const visibleIcons = icons.slice(0, numberOfShownIcons); const remainingIcons = icons.slice(numberOfShownIcons); const showMoreRef = useRef(null); const shouldShowMore = remainingIcons.length > 0; const recievedInteraction = isClicked || isShowMoreHovered || isPopoverHovered; const shouldShowPopover = recievedInteraction && !disableInteraction; // Required due to issue with popover backdrop, read more: // https://github.com/veeqo/veeqo-frontend/pull/390/files#r970534798 const onShouldClose = isClicked ? (() => setIsClicked(false)) : undefined; const handleClick = disableInteraction ? undefined : () => setIsClicked(!isClicked); const handleIconClick = (icon: IStoreIcon) => () => onClick(icon); useEffect(() => { if (showMoreRef.current) { setTotalWidth(totalWidth + showMoreRef.current.clientWidth - 10); } }, [setTotalWidth]); return ( {visibleIcons.map((icon, index) => ( 1 && !disableInteraction} hasTooltip={!disableInteraction} /> ))} { shouldShowMore && ( {`+${remainingIcons.length}`} ) } { shouldShowPopover && ( {remainingIcons.map((icon) => ( {icon.name} ))} ) } ); }; export default StoreIcons;