2023-10-19 04:26:45 -04:00
|
|
|
import { FC, memo, MouseEventHandler, useCallback, useState } from 'react'
|
|
|
|
import { useTranslation } from 'react-i18next'
|
2024-05-30 11:09:57 -04:00
|
|
|
import OLButton from '@/features/ui/components/ol/ol-button'
|
|
|
|
import OLTooltip from '@/features/ui/components/ol/ol-tooltip'
|
|
|
|
import OLIconButton from '@/features/ui/components/ol/ol-icon-button'
|
|
|
|
import { bsVersion } from '@/features/utils/bootstrap-5'
|
2023-10-19 04:26:45 -04:00
|
|
|
|
|
|
|
export const CopyToClipboard = memo<{
|
|
|
|
content: string
|
|
|
|
tooltipId: string
|
|
|
|
kind?: 'text' | 'icon'
|
|
|
|
}>(({ content, tooltipId, kind = 'icon' }) => {
|
|
|
|
const { t } = useTranslation()
|
|
|
|
|
|
|
|
const [copied, setCopied] = useState(false)
|
|
|
|
|
|
|
|
const handleClick = useCallback(() => {
|
|
|
|
navigator.clipboard.writeText(content).then(() => {
|
|
|
|
setCopied(true)
|
|
|
|
window.setTimeout(() => {
|
|
|
|
setCopied(false)
|
|
|
|
}, 1500)
|
|
|
|
})
|
|
|
|
}, [content])
|
|
|
|
|
|
|
|
if (!navigator.clipboard?.writeText) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2024-05-30 11:09:57 -04:00
|
|
|
<OLTooltip
|
2023-10-19 04:26:45 -04:00
|
|
|
id={tooltipId}
|
|
|
|
description={copied ? `${t('copied')}!` : t('copy')}
|
2024-05-30 11:09:57 -04:00
|
|
|
overlayProps={{ delay: copied ? 1000 : 250 }}
|
2023-10-19 04:26:45 -04:00
|
|
|
>
|
|
|
|
<span>
|
|
|
|
{kind === 'text' ? (
|
|
|
|
<TextButton handleClick={handleClick} />
|
|
|
|
) : (
|
|
|
|
<IconButton handleClick={handleClick} copied={copied} />
|
|
|
|
)}
|
|
|
|
</span>
|
2024-05-30 11:09:57 -04:00
|
|
|
</OLTooltip>
|
2023-10-19 04:26:45 -04:00
|
|
|
)
|
|
|
|
})
|
|
|
|
CopyToClipboard.displayName = 'CopyToClipboard'
|
|
|
|
|
|
|
|
const TextButton: FC<{
|
2024-05-30 11:09:57 -04:00
|
|
|
handleClick: MouseEventHandler<HTMLButtonElement>
|
2023-10-19 04:26:45 -04:00
|
|
|
}> = ({ handleClick }) => {
|
|
|
|
const { t } = useTranslation()
|
|
|
|
|
|
|
|
return (
|
2024-05-30 11:09:57 -04:00
|
|
|
<OLButton
|
2023-10-19 04:26:45 -04:00
|
|
|
onClick={handleClick}
|
2024-10-08 10:22:13 -04:00
|
|
|
size="sm"
|
2024-05-30 11:09:57 -04:00
|
|
|
variant="secondary"
|
2024-06-06 11:37:47 -04:00
|
|
|
className="copy-button"
|
|
|
|
bs3Props={{ bsSize: 'xsmall' }}
|
2023-10-19 04:26:45 -04:00
|
|
|
>
|
|
|
|
{t('copy')}
|
2024-05-30 11:09:57 -04:00
|
|
|
</OLButton>
|
2023-10-19 04:26:45 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
const IconButton: FC<{
|
2024-05-30 11:09:57 -04:00
|
|
|
handleClick: MouseEventHandler<HTMLButtonElement>
|
2023-10-19 04:26:45 -04:00
|
|
|
copied: boolean
|
|
|
|
}> = ({ handleClick, copied }) => {
|
|
|
|
const { t } = useTranslation()
|
|
|
|
|
|
|
|
return (
|
2024-05-30 11:09:57 -04:00
|
|
|
<OLIconButton
|
2023-10-19 04:26:45 -04:00
|
|
|
onClick={handleClick}
|
2024-05-30 11:09:57 -04:00
|
|
|
variant="link"
|
2024-10-09 08:13:50 -04:00
|
|
|
size="sm"
|
2024-05-30 11:09:57 -04:00
|
|
|
accessibilityLabel={t('copy')}
|
2023-10-19 04:26:45 -04:00
|
|
|
className="copy-button"
|
2024-05-30 11:09:57 -04:00
|
|
|
bs3Props={{ bsSize: 'xsmall' }}
|
|
|
|
icon={
|
|
|
|
bsVersion({
|
|
|
|
bs5: copied ? 'check' : 'content_copy',
|
|
|
|
bs3: copied ? 'check' : 'clipboard',
|
|
|
|
}) || ''
|
|
|
|
}
|
|
|
|
/>
|
2023-10-19 04:26:45 -04:00
|
|
|
)
|
|
|
|
}
|