2021-06-23 05:37:08 -04:00
|
|
|
import { useCallback, useState } from 'react'
|
2022-05-18 09:46:10 -04:00
|
|
|
import { Button } from 'react-bootstrap'
|
2021-05-12 06:28:03 -04:00
|
|
|
import { Trans, useTranslation } from 'react-i18next'
|
2022-05-18 09:46:10 -04:00
|
|
|
import Tooltip from './tooltip'
|
2021-03-12 05:23:46 -05:00
|
|
|
import Icon from './icon'
|
|
|
|
|
2022-05-18 09:46:10 -04:00
|
|
|
type CopyLinkProps = {
|
|
|
|
link: string
|
|
|
|
tooltipId: string
|
|
|
|
}
|
|
|
|
|
|
|
|
function CopyLink({ link, tooltipId }: CopyLinkProps) {
|
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 (
|
2022-05-18 09:46:10 -04:00
|
|
|
<Tooltip
|
|
|
|
id={tooltipId}
|
|
|
|
description={copied ? 'Copied!' : <Trans i18nKey="copy" />}
|
|
|
|
overlayProps={{
|
|
|
|
delayHide: copied ? 1000 : 250,
|
|
|
|
shouldUpdatePosition: true,
|
|
|
|
}}
|
2021-03-12 05:23:46 -05:00
|
|
|
>
|
|
|
|
<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>
|
2022-05-18 09:46:10 -04:00
|
|
|
</Tooltip>
|
2021-03-12 05:23:46 -05:00
|
|
|
)
|
|
|
|
}
|
2022-05-18 09:46:10 -04:00
|
|
|
|
|
|
|
export default CopyLink
|