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-05-30 11:09:57 -04:00
|
|
|
size="small"
|
|
|
|
variant="secondary"
|
|
|
|
bs3Props={{
|
|
|
|
bsStyle: null,
|
|
|
|
className: 'btn-secondary copy-button',
|
|
|
|
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"
|
|
|
|
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
|
|
|
)
|
|
|
|
}
|