Files
astuto/app/javascript/components/common/CopyToClipboardButton.tsx

49 lines
1.2 KiB
TypeScript
Raw Normal View History

import * as React from 'react';
import I18n from 'i18n-js';
import { useState } from 'react';
2022-08-22 10:38:03 +02:00
import ActionLink from './ActionLink';
import { CopyIcon, DoneIcon } from './Icons';
import { SuccessText } from './CustomTexts';
interface Props {
label: string;
textToCopy: string;
copiedLabel?: string;
}
const CopyToClipboardButton = ({
label,
textToCopy,
copiedLabel = I18n.t('common.copied')
}: Props) => {
const [ready, setReady] = useState(true);
const alertError = () =>
alert(`Error in automatically copying to clipboard. Please copy the callback url manually:\n\n${textToCopy}`);
return (
ready ?
2022-08-22 10:38:03 +02:00
<ActionLink
onClick={() => {
if (navigator.clipboard) {
navigator.clipboard.writeText(textToCopy).then(() => {
setReady(false);
setTimeout(() => setReady(true), 2000);
},
alertError);
} else {
alertError();
}
}}
2022-08-22 10:38:03 +02:00
icon={<CopyIcon />}
>
{label}
2022-08-22 10:38:03 +02:00
</ActionLink>
:
2022-08-22 10:38:03 +02:00
<span style={{display: 'flex', marginRight: 12}}>
<SuccessText>{copiedLabel}</SuccessText>
2022-08-22 10:38:03 +02:00
</span>
);
};
export default CopyToClipboardButton;