From 13a9d8059065a2175bcdfe256f79d310c21bf0a3 Mon Sep 17 00:00:00 2001
From: Aditya Choudhari <48932219+adityachoudhari26@users.noreply.github.com>
Date: Thu, 29 Aug 2024 23:06:53 -0700
Subject: [PATCH] fix: Invite link fix (#9)
---
.../members/WorkspaceMembersTable.tsx | 49 +-
packages/api/package.json | 1 +
packages/api/src/router/invite.ts | 33 +-
.../db/drizzle/0003_cloudy_jamie_braddock.sql | 1 +
packages/db/drizzle/meta/0003_snapshot.json | 2516 +++++++++++++++++
packages/db/drizzle/meta/_journal.json | 7 +
packages/db/src/schema/invite.ts | 3 +-
pnpm-lock.yaml | 3 +
8 files changed, 2584 insertions(+), 29 deletions(-)
create mode 100644 packages/db/drizzle/0003_cloudy_jamie_braddock.sql
create mode 100644 packages/db/drizzle/meta/0003_snapshot.json
diff --git a/apps/webservice/src/app/[workspaceSlug]/settings/(settings)/workspace/members/WorkspaceMembersTable.tsx b/apps/webservice/src/app/[workspaceSlug]/settings/(settings)/workspace/members/WorkspaceMembersTable.tsx
index eef34c9e..2bc341e2 100644
--- a/apps/webservice/src/app/[workspaceSlug]/settings/(settings)/workspace/members/WorkspaceMembersTable.tsx
+++ b/apps/webservice/src/app/[workspaceSlug]/settings/(settings)/workspace/members/WorkspaceMembersTable.tsx
@@ -2,7 +2,7 @@
import type { User, WorkspaceMember } from "@ctrlplane/db/schema";
import type { ColumnDef, ColumnFiltersState } from "@tanstack/react-table";
-import { useEffect, useState } from "react";
+import { useState } from "react";
import {
flexRender,
getCoreRowModel,
@@ -11,6 +11,7 @@ import {
} from "@tanstack/react-table";
import { capitalCase } from "change-case";
import { TbCheck, TbChevronDown, TbCopy, TbDots } from "react-icons/tb";
+import { v4 } from "uuid";
import { Avatar, AvatarFallback, AvatarImage } from "@ctrlplane/ui/avatar";
import { Button } from "@ctrlplane/ui/button";
@@ -41,32 +42,34 @@ interface Member {
const InviteLinkSection: React.FC<{
sessionMember?: Member;
-}> = ({ sessionMember }) => {
- const inviteLink = api.invite.workspace.link.byWorkspaceMemberId.useQuery(
- sessionMember?.workspace_member.id ?? "",
- { enabled: sessionMember != null },
- );
- const { mutateAsync } = api.invite.workspace.link.create.useMutation();
+ workspaceSlug: string;
+ inviteLink?: string;
+}> = ({ sessionMember, workspaceSlug, inviteLink }) => {
+ const workspace = api.workspace.bySlug.useQuery(workspaceSlug);
const utils = api.useUtils();
+ const { mutateAsync } = api.invite.workspace.link.create.useMutation({
+ onSuccess: () =>
+ utils.invite.workspace.link.byWorkspaceMemberId.invalidate(),
+ });
const [clickedCopy, setClickedCopy] = useState(false);
- useEffect(() => {
- if (inviteLink.isSuccess && inviteLink.data == null)
- mutateAsync(sessionMember?.workspace_member.id ?? "").then(() =>
- utils.invite.workspace.link.byWorkspaceMemberId.invalidate(
- sessionMember?.workspace_member.id ?? "",
- ),
- );
- }, [mutateAsync, inviteLink, sessionMember?.workspace_member.id, utils]);
-
- const link = `${env.NEXT_PUBLIC_BASE_URL}/join/${inviteLink.data?.token ?? ""}`;
+ const [token] = useState(inviteLink ?? v4());
+ const link = `${env.NEXT_PUBLIC_BASE_URL}/join/${token}`;
- const handleCopyClick = () =>
+ const handleCopyClick = () => {
navigator.clipboard.writeText(link).then(() => {
setClickedCopy(true);
setTimeout(() => setClickedCopy(false), 1000);
});
+ if (inviteLink == null && workspace.data != null && sessionMember != null)
+ mutateAsync({
+ workspaceId: workspace.data.id,
+ workspaceMemberId: sessionMember.workspace_member.id,
+ token,
+ });
+ };
+
return (
@@ -91,6 +94,10 @@ const AddMembersDialog: React.FC<{
sessionMember?: Member;
}> = ({ sessionMember, workspaceSlug }) => {
const [inviteMode, setInviteMode] = useState<"email" | "link">("email");
+ const inviteLink = api.invite.workspace.link.byWorkspaceMemberId.useQuery(
+ sessionMember?.workspace_member.id ?? "",
+ { enabled: sessionMember != null },
+ );
return (