2021-06-23 05:37:08 -04:00
|
|
|
import { useCallback, useState } from 'react'
|
2021-03-12 05:23:46 -05:00
|
|
|
import { Button, OverlayTrigger, Tooltip } from 'react-bootstrap'
|
|
|
|
import PropTypes from 'prop-types'
|
2021-05-12 06:28:03 -04:00
|
|
|
import { Trans, useTranslation } from 'react-i18next'
|
2021-03-12 05:23:46 -05:00
|
|
|
import Icon from './icon'
|
|
|
|
|
|
|
|
export default function CopyLink({ link, tooltipId }) {
|
2021-05-12 06:28:03 -04:00
|
|
|
const { t } = useTranslation()
|
|
|
|
|
2021-03-12 05:23:46 -05:00
|
|
|
const [copied, setCopied] = useState(false)
|
|
|
|
|
|
|
|
const handleClick = useCallback(() => {
|
|
|
|
navigator.clipboard.writeText(link).then(() => {
|
|
|
|
setCopied(true)
|
|
|
|
window.setTimeout(() => {
|
|
|
|
setCopied(false)
|
|
|
|
}, 1500)
|
|
|
|
})
|
|
|
|
}, [link])
|
|
|
|
|
|
|
|
if (!navigator.clipboard?.writeText) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<OverlayTrigger
|
|
|
|
placement="top"
|
|
|
|
delayHide={copied ? 1000 : 250}
|
|
|
|
shouldUpdatePosition
|
|
|
|
overlay={
|
|
|
|
<Tooltip id={tooltipId}>
|
|
|
|
{copied ? 'Copied!' : <Trans i18nKey="copy" />}
|
|
|
|
</Tooltip>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<Button
|
|
|
|
onClick={handleClick}
|
|
|
|
bsSize="xsmall"
|
|
|
|
bsStyle="link"
|
|
|
|
className="copy-button"
|
2021-05-12 06:28:03 -04:00
|
|
|
aria-label={t('copy')}
|
2021-03-12 05:23:46 -05:00
|
|
|
>
|
|
|
|
{copied ? <Icon type="check" /> : <Icon type="clipboard" />}
|
|
|
|
</Button>
|
|
|
|
</OverlayTrigger>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
CopyLink.propTypes = {
|
|
|
|
link: PropTypes.string.isRequired,
|
2021-04-27 03:52:58 -04:00
|
|
|
tooltipId: PropTypes.string.isRequired,
|
2021-03-12 05:23:46 -05:00
|
|
|
}
|