Commit f294adcc authored by Max Alekseenko's avatar Max Alekseenko

reuse CopyToClipboard component

parent d13bc9f7
import { IconButton, Image, Link, LinkBox, Skeleton, useColorModeValue, chakra, Flex, Tooltip, useClipboard, useDisclosure } from '@chakra-ui/react'; import { IconButton, Image, Link, LinkBox, Skeleton, useColorModeValue, chakra, Flex } from '@chakra-ui/react';
import type { MouseEvent } from 'react'; import type { MouseEvent } from 'react';
import React, { useCallback, useState, useEffect } from 'react'; import React, { useCallback } from 'react';
import type { MarketplaceAppWithSecurityReport, ContractListTypes, AppRating } from 'types/client/marketplace'; import type { MarketplaceAppWithSecurityReport, ContractListTypes, AppRating } from 'types/client/marketplace';
import useIsMobile from 'lib/hooks/useIsMobile'; import useIsMobile from 'lib/hooks/useIsMobile';
import isBrowser from 'lib/isBrowser'; import isBrowser from 'lib/isBrowser';
import IconSvg from 'ui/shared/IconSvg'; import CopyToClipboard from 'ui/shared/CopyToClipboard';
import AppSecurityReport from './AppSecurityReport'; import AppSecurityReport from './AppSecurityReport';
import FavoriteIcon from './FavoriteIcon'; import FavoriteIcon from './FavoriteIcon';
...@@ -58,20 +58,6 @@ const MarketplaceAppCard = ({ ...@@ -58,20 +58,6 @@ const MarketplaceAppCard = ({
const isMobile = useIsMobile(); const isMobile = useIsMobile();
const categoriesLabel = categories.join(', '); const categoriesLabel = categories.join(', ');
const linkToApp = isBrowser() ? window.location.origin + `/apps/${ id }` : '';
const { hasCopied, onCopy } = useClipboard(linkToApp, 1000);
const [ copied, setCopied ] = useState(false);
// have to implement controlled tooltip because of the issue - https://github.com/chakra-ui/chakra-ui/issues/7107
const { isOpen, onOpen, onClose } = useDisclosure();
useEffect(() => {
if (hasCopied) {
setCopied(true);
} else {
setCopied(false);
}
}, [ hasCopied ]);
const handleInfoClick = useCallback((event: MouseEvent) => { const handleInfoClick = useCallback((event: MouseEvent) => {
event.preventDefault(); event.preventDefault();
onInfoClick(id); onInfoClick(id);
...@@ -199,27 +185,25 @@ const MarketplaceAppCard = ({ ...@@ -199,27 +185,25 @@ const MarketplaceAppCard = ({
aria-label="Mark as favorite" aria-label="Mark as favorite"
title="Mark as favorite" title="Mark as favorite"
variant="ghost" variant="ghost"
colorScheme="gray"
w={{ base: 6, md: '30px' }} w={{ base: 6, md: '30px' }}
h={{ base: 6, md: '30px' }} h={{ base: 6, md: '30px' }}
onClick={ handleFavoriteClick } onClick={ handleFavoriteClick }
icon={ <FavoriteIcon isFavorite={ isFavorite }/> } icon={ <FavoriteIcon isFavorite={ isFavorite }/> }
ml={ 2 } ml={ 2 }
/> />
<Tooltip label={ copied ? 'Copied' : 'Copy link to clipboard' } isOpen={ isOpen || copied }> <CopyToClipboard
<IconButton text={ isBrowser() ? window.location.origin + `/apps/${ id }` : '' }
aria-label="Copy link" icon="share"
variant="ghost" size={ 4 }
colorScheme="gray" variant="ghost"
w={{ base: 6, md: '30px' }} w={{ base: 6, md: '30px' }}
h={{ base: 6, md: '30px' }} h={{ base: 6, md: '30px' }}
onClick={ onCopy } color="gray.400"
onMouseEnter={ onOpen } _hover={{ color: 'gray.400' }}
onMouseLeave={ onClose } ml={{ base: 1, md: 0 }}
icon={ <IconSvg name="share" boxSize={ 4 } color="gray.400"/> } display="inline-flex"
ml={{ base: 1, md: 0 }} borderRadius="base"
/> />
</Tooltip>
</Flex> </Flex>
</Flex> </Flex>
) } ) }
......
import { IconButton, Tooltip, useClipboard, chakra, useDisclosure, Skeleton, useColorModeValue } from '@chakra-ui/react'; import { IconButton, Tooltip, useClipboard, chakra, useDisclosure, Skeleton, useColorModeValue } from '@chakra-ui/react';
import React, { useEffect, useState } from 'react'; import React, { useEffect, useState } from 'react';
import type { IconName } from 'ui/shared/IconSvg';
import IconSvg from 'ui/shared/IconSvg'; import IconSvg from 'ui/shared/IconSvg';
export interface Props { export interface Props {
...@@ -10,14 +11,17 @@ export interface Props { ...@@ -10,14 +11,17 @@ export interface Props {
onClick?: (event: React.MouseEvent) => void; onClick?: (event: React.MouseEvent) => void;
size?: number; size?: number;
type?: 'link'; type?: 'link';
icon?: IconName;
variant?: 'simple';
} }
const CopyToClipboard = ({ text, className, isLoading, onClick, size = 5, type }: Props) => { const CopyToClipboard = ({ text, className, isLoading, onClick, size = 5, type, icon, variant }: Props) => {
const { hasCopied, onCopy } = useClipboard(text, 1000); const { hasCopied, onCopy } = useClipboard(text, 1000);
const [ copied, setCopied ] = useState(false); const [ copied, setCopied ] = useState(false);
// have to implement controlled tooltip because of the issue - https://github.com/chakra-ui/chakra-ui/issues/7107 // have to implement controlled tooltip because of the issue - https://github.com/chakra-ui/chakra-ui/issues/7107
const { isOpen, onOpen, onClose } = useDisclosure(); const { isOpen, onOpen, onClose } = useDisclosure();
const iconColor = useColorModeValue('gray.400', 'gray.500'); const iconColor = useColorModeValue('gray.400', 'gray.500');
const iconName = icon || (type === 'link' ? 'link' : 'copy');
useEffect(() => { useEffect(() => {
if (hasCopied) { if (hasCopied) {
...@@ -40,10 +44,10 @@ const CopyToClipboard = ({ text, className, isLoading, onClick, size = 5, type } ...@@ -40,10 +44,10 @@ const CopyToClipboard = ({ text, className, isLoading, onClick, size = 5, type }
<Tooltip label={ copied ? 'Copied' : `Copy${ type === 'link' ? ' link ' : ' ' }to clipboard` } isOpen={ isOpen || copied }> <Tooltip label={ copied ? 'Copied' : `Copy${ type === 'link' ? ' link ' : ' ' }to clipboard` } isOpen={ isOpen || copied }>
<IconButton <IconButton
aria-label="copy" aria-label="copy"
icon={ <IconSvg name={ type === 'link' ? 'link' : 'copy' } boxSize={ size }/> } icon={ <IconSvg name={ iconName } boxSize={ size }/> }
boxSize={ size } boxSize={ size }
color={ iconColor } color={ iconColor }
variant="simple" variant={ variant }
display="inline-block" display="inline-block"
flexShrink={ 0 } flexShrink={ 0 }
onClick={ handleClick } onClick={ handleClick }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment