Build group URI and handle successful copying

This commit is contained in:
Chewbacca 2023-04-19 16:51:07 -04:00
parent 2813b02329
commit c87589be4e
2 changed files with 25 additions and 8 deletions

View File

@ -1,7 +1,8 @@
import React from 'react'; 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 { Avatar, Divider, HStack, Stack, Text, Button } from 'soapbox/components/ui';
import toast from 'soapbox/toast';
import copy from 'soapbox/utils/copy'; import copy from 'soapbox/utils/copy';
import type { Group } from 'soapbox/schemas'; import type { Group } from 'soapbox/schemas';
@ -10,8 +11,18 @@ interface IConfirmationStep {
group: Group group: Group
} }
const messages = defineMessages({
copied: { id: 'copy.success', defaultMessage: 'Copied to clipboard!' },
});
const ConfirmationStep: React.FC<IConfirmationStep> = ({ group }) => { 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 = () => { const handleShare = () => {
navigator.share({ navigator.share({
@ -87,11 +98,9 @@ const ConfirmationStep: React.FC<IConfirmationStep> = ({ group }) => {
</Button> </Button>
)} )}
{group.uri && ( <Button onClick={handleCopyLink} theme='transparent' icon={require('@tabler/icons/link.svg')} className='text-primary-600'>
<Button onClick={handleCopyLink} theme='transparent' icon={require('@tabler/icons/link.svg')} className='text-primary-600'> <FormattedMessage id='manage_group.confirmation.copy' defaultMessage='Copy link' />
<FormattedMessage id='manage_group.confirmation.copy' defaultMessage='Copy link' /> </Button>
</Button>
)}
</HStack> </HStack>
</Stack> </Stack>
); );

View File

@ -1,6 +1,10 @@
const copy = (text: string) => { const copy = (text: string, onSuccess?: () => void) => {
if (navigator.clipboard) { if (navigator.clipboard) {
navigator.clipboard.writeText(text); navigator.clipboard.writeText(text);
if (onSuccess) {
onSuccess();
}
} else { } else {
const textarea = document.createElement('textarea'); const textarea = document.createElement('textarea');
@ -16,6 +20,10 @@ const copy = (text: string) => {
// Do nothing // Do nothing
} finally { } finally {
document.body.removeChild(textarea); document.body.removeChild(textarea);
if (onSuccess) {
onSuccess();
}
} }
} }
}; };