Build group URI and handle successful copying
This commit is contained in:
parent
2813b02329
commit
c87589be4e
|
@ -1,7 +1,8 @@
|
|||
import React from 'react';
|
||||
import { FormattedMessage } from 'react-intl';
|
||||
import { FormattedMessage, defineMessages, useIntl } from 'react-intl';
|
||||
|
||||
import { Avatar, Divider, HStack, Stack, Text, Button } from 'soapbox/components/ui';
|
||||
import toast from 'soapbox/toast';
|
||||
import copy from 'soapbox/utils/copy';
|
||||
|
||||
import type { Group } from 'soapbox/schemas';
|
||||
|
@ -10,8 +11,18 @@ interface IConfirmationStep {
|
|||
group: Group
|
||||
}
|
||||
|
||||
const messages = defineMessages({
|
||||
copied: { id: 'copy.success', defaultMessage: 'Copied to clipboard!' },
|
||||
});
|
||||
|
||||
const ConfirmationStep: React.FC<IConfirmationStep> = ({ group }) => {
|
||||
const handleCopyLink = () => copy(group.uri);
|
||||
const intl = useIntl();
|
||||
|
||||
const handleCopyLink = () => {
|
||||
copy(`${window.location.origin}/group/${group.slug}`, () => {
|
||||
toast.success(intl.formatMessage(messages.copied));
|
||||
});
|
||||
};
|
||||
|
||||
const handleShare = () => {
|
||||
navigator.share({
|
||||
|
@ -87,11 +98,9 @@ const ConfirmationStep: React.FC<IConfirmationStep> = ({ group }) => {
|
|||
</Button>
|
||||
)}
|
||||
|
||||
{group.uri && (
|
||||
<Button onClick={handleCopyLink} theme='transparent' icon={require('@tabler/icons/link.svg')} className='text-primary-600'>
|
||||
<FormattedMessage id='manage_group.confirmation.copy' defaultMessage='Copy link' />
|
||||
</Button>
|
||||
)}
|
||||
</HStack>
|
||||
</Stack>
|
||||
);
|
||||
|
|
|
@ -1,6 +1,10 @@
|
|||
const copy = (text: string) => {
|
||||
const copy = (text: string, onSuccess?: () => void) => {
|
||||
if (navigator.clipboard) {
|
||||
navigator.clipboard.writeText(text);
|
||||
|
||||
if (onSuccess) {
|
||||
onSuccess();
|
||||
}
|
||||
} else {
|
||||
const textarea = document.createElement('textarea');
|
||||
|
||||
|
@ -16,6 +20,10 @@ const copy = (text: string) => {
|
|||
// Do nothing
|
||||
} finally {
|
||||
document.body.removeChild(textarea);
|
||||
|
||||
if (onSuccess) {
|
||||
onSuccess();
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue