Skip to content

Commit

Permalink
fix: Copy invite link on safari
Browse files Browse the repository at this point in the history
  • Loading branch information
hariombalhara committed Dec 19, 2023
1 parent 5fb1857 commit 61d15ee
Showing 1 changed file with 30 additions and 12 deletions.
42 changes: 30 additions & 12 deletions packages/features/ee/teams/components/MemberInvitationModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ export default function MemberInvitationModal(props: MemberInvitationModalProps)

const createInviteMutation = trpc.viewer.teams.createInvite.useMutation({
async onSuccess({ inviteLink }) {
await copyInviteLinkToClipboard(inviteLink);
trpcContext.viewer.teams.get.invalidate();
trpcContext.viewer.teams.list.invalidate();
},
Expand All @@ -92,15 +91,6 @@ export default function MemberInvitationModal(props: MemberInvitationModalProps)
},
});

const copyInviteLinkToClipboard = async (inviteLink: string) => {
try {
await navigator.clipboard.writeText(inviteLink);
showToast(t("invite_link_copied"), "success");
} catch (e) {
console.error(e);
}
};

const options: MembershipRoleOption[] = useMemo(() => {
const options: MembershipRoleOption[] = [
{ value: MembershipRole.MEMBER, label: t("member") },
Expand Down Expand Up @@ -389,8 +379,36 @@ export default function MemberInvitationModal(props: MemberInvitationModalProps)
type="button"
color="minimal"
variant="icon"
onClick={() => {
createInviteMutation.mutate({ teamId: props.teamId, token: props.token });
onClick={async function () {
try {
// Required for Safari but also works on Chrome
// Credits to https://wolfgangrittner.dev/how-to-use-clipboard-api-in-firefox/
if (typeof ClipboardItem) {
const inviteLinkClipbardItem = new ClipboardItem({
"text/plain": new Promise(async (resolve) => {
// Instead of doing async work and then writing to clipboard, do async work in clipboard API itself
const { inviteLink } = await createInviteMutation.mutateAsync({
teamId: props.teamId,
token: props.token,
});
showToast(t("invite_link_copied"), "success");
resolve(new Blob([inviteLink], { type: "text/plain" }));
}),
});
await navigator.clipboard.write([inviteLinkClipbardItem]);
} else {
// Fallback for browsers that don't support ClipboardItem e.g. Firefox
const { inviteLink } = await createInviteMutation.mutateAsync({
teamId: props.teamId,
token: props.token,
});
await navigator.clipboard.writeText(inviteLink);
showToast(t("invite_link_copied"), "success");
}
} catch (e) {
showToast(t("something_went_wrong_on_our_end"), "error");
console.error(e);
}
}}
className={classNames("gap-2", props.token && "opacity-50")}
data-testid="copy-invite-link-button">
Expand Down

0 comments on commit 61d15ee

Please sign in to comment.