Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Copy invite link on safari #12873

Merged
merged 1 commit into from
Dec 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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({
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spent quite some time figuring out two promises to work together with closures going on. In the end discovered that there is mutateAsync to make it simpler 😅

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
Loading