import React, { useState } from "react"; import useSWR, { mutate } from "swr"; // icons import { CheckCircle2 } from "lucide-react"; // types import { IWorkspaceMemberInvitation } from "@plane/types"; // ui import { Button } from "@plane/ui"; // constants import { MEMBER_ACCEPTED } from "@/constants/event-tracker"; import { USER_WORKSPACES, USER_WORKSPACE_INVITATIONS } from "@/constants/fetch-keys"; import { ROLE } from "@/constants/workspace"; // helpers import { truncateText } from "@/helpers/string.helper"; import { getUserRole } from "@/helpers/user.helper"; // hooks import { useEventTracker, useUser, useWorkspace } from "@/hooks/store"; // services import { WorkspaceService } from "@/services/workspace.service"; type Props = { handleNextStep: () => void; handleCurrentViewChange: () => void; }; const workspaceService = new WorkspaceService(); export const Invitations: React.FC = (props) => { const { handleNextStep, handleCurrentViewChange } = props; // states const [isJoiningWorkspaces, setIsJoiningWorkspaces] = useState(false); const [invitationsRespond, setInvitationsRespond] = useState([]); // store hooks const { captureEvent } = useEventTracker(); const { updateCurrentUser } = useUser(); const { workspaces, fetchWorkspaces } = useWorkspace(); const workspacesList = Object.values(workspaces); const { data: invitations, mutate: mutateInvitations } = useSWR(USER_WORKSPACE_INVITATIONS, () => workspaceService.userWorkspaceInvitations() ); const handleInvitation = (workspace_invitation: IWorkspaceMemberInvitation, action: "accepted" | "withdraw") => { if (action === "accepted") { setInvitationsRespond((prevData) => [...prevData, workspace_invitation.id]); } else if (action === "withdraw") { setInvitationsRespond((prevData) => prevData.filter((item: string) => item !== workspace_invitation.id)); } }; const updateLastWorkspace = async () => { if (!workspacesList) return; await updateCurrentUser({ last_workspace_id: workspacesList[0]?.id, }); }; const submitInvitations = async () => { const invitation = invitations?.find((invitation) => invitation.id === invitationsRespond[0]); if (invitationsRespond.length <= 0 && !invitation?.role) return; setIsJoiningWorkspaces(true); await workspaceService .joinWorkspaces({ invitations: invitationsRespond }) .then(async () => { captureEvent(MEMBER_ACCEPTED, { member_id: invitation?.id, role: getUserRole(invitation?.role as any), project_id: undefined, accepted_from: "App", state: "SUCCESS", element: "Workspace invitations page", }); await fetchWorkspaces(); await mutate(USER_WORKSPACES); await updateLastWorkspace(); await handleNextStep(); await mutateInvitations(); }) .catch((error) => { console.error(error); captureEvent(MEMBER_ACCEPTED, { member_id: invitation?.id, role: getUserRole(invitation?.role as any), project_id: undefined, accepted_from: "App", state: "FAILED", element: "Workspace invitations page", }); }) .finally(() => setIsJoiningWorkspaces(false)); }; return invitations && invitations.length > 0 ? (

You are invited!

Accept the invites to collaborate with your team!

{invitations && invitations.length > 0 && invitations.map((invitation) => { const isSelected = invitationsRespond.includes(invitation.id); const invitedWorkspace = invitation.workspace; return (
handleInvitation(invitation, isSelected ? "withdraw" : "accepted")} >
{invitedWorkspace?.logo && invitedWorkspace.logo !== "" ? ( {invitedWorkspace.name} ) : ( {invitedWorkspace?.name[0]} )}
{truncateText(invitedWorkspace?.name, 30)}

{ROLE[invitation.role]}

); })}

or


) : (
No Invitations found
); };