import React, { useState } from "react"; import useSWR from "swr";; // types import { IWorkspaceMemberInvitation } from "@plane/types"; // ui import { Button, Checkbox } from "@plane/ui"; // constants import { MEMBER_ACCEPTED } from "@/constants/event-tracker"; import { 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, useWorkspace } from "@/hooks/store"; // services import { WorkspaceService } from "@/services/workspace.service"; type Props = { handleNextStep: () => Promise; 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 { fetchWorkspaces } = useWorkspace(); const { data: invitations } = 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 submitInvitations = async () => { const invitation = invitations?.find((invitation) => invitation.id === invitationsRespond[0]); if (invitationsRespond.length <= 0 && !invitation?.role) return; setIsJoiningWorkspaces(true); try { await workspaceService.joinWorkspaces({ invitations: invitationsRespond }); 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 handleNextStep(); } 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", }); 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
); };