2023-11-01 13:52:10 +00:00
|
|
|
import { useState, FC } from "react";
|
2023-10-30 15:08:50 +00:00
|
|
|
import Link from "next/link";
|
|
|
|
import { useRouter } from "next/router";
|
2023-11-28 11:35:42 +00:00
|
|
|
import { observer } from "mobx-react-lite";
|
2023-11-13 08:04:05 +00:00
|
|
|
import { mutate } from "swr";
|
2023-11-28 11:35:42 +00:00
|
|
|
import { ChevronDown, Dot, XCircle } from "lucide-react";
|
2023-10-30 15:08:50 +00:00
|
|
|
// mobx store
|
|
|
|
import { useMobxStore } from "lib/mobx/store-provider";
|
|
|
|
// hooks
|
|
|
|
import useToast from "hooks/use-toast";
|
|
|
|
// components
|
|
|
|
import { ConfirmWorkspaceMemberRemove } from "components/workspace";
|
|
|
|
// ui
|
|
|
|
import { CustomSelect, Tooltip } from "@plane/ui";
|
2023-11-23 09:14:06 +00:00
|
|
|
// types
|
|
|
|
import { TUserWorkspaceRole } from "types";
|
2023-10-30 15:08:50 +00:00
|
|
|
// constants
|
2023-11-28 11:35:42 +00:00
|
|
|
import { EUserWorkspaceRoles, ROLE } from "constants/workspace";
|
2023-10-30 15:08:50 +00:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
member: {
|
|
|
|
id: string;
|
|
|
|
memberId: string;
|
|
|
|
avatar: string;
|
|
|
|
first_name: string;
|
|
|
|
last_name: string;
|
|
|
|
email: string | undefined;
|
|
|
|
display_name: string;
|
2023-11-02 11:41:33 +00:00
|
|
|
role: TUserWorkspaceRole;
|
2023-10-30 15:08:50 +00:00
|
|
|
status: boolean;
|
|
|
|
member: boolean;
|
|
|
|
accountCreated: boolean;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2023-11-28 11:35:42 +00:00
|
|
|
export const WorkspaceMembersListItem: FC<Props> = observer((props) => {
|
2023-10-30 15:08:50 +00:00
|
|
|
const { member } = props;
|
2023-11-01 13:52:10 +00:00
|
|
|
// router
|
2023-10-30 15:08:50 +00:00
|
|
|
const router = useRouter();
|
|
|
|
const { workspaceSlug } = router.query;
|
2023-11-01 13:52:10 +00:00
|
|
|
// store
|
2023-11-08 19:05:12 +00:00
|
|
|
const {
|
|
|
|
workspaceMember: { removeMember, updateMember, deleteWorkspaceInvitation },
|
2023-11-23 09:14:06 +00:00
|
|
|
user: { currentWorkspaceMemberInfo, currentWorkspaceRole, currentUser, currentUserSettings, leaveWorkspace },
|
2023-11-08 19:05:12 +00:00
|
|
|
} = useMobxStore();
|
2023-11-01 13:52:10 +00:00
|
|
|
// states
|
|
|
|
const [removeMemberModal, setRemoveMemberModal] = useState(false);
|
|
|
|
// hooks
|
|
|
|
const { setToastAlert } = useToast();
|
2023-10-30 15:08:50 +00:00
|
|
|
|
2023-11-23 09:14:06 +00:00
|
|
|
const handleLeaveWorkspace = async () => {
|
|
|
|
if (!workspaceSlug || !currentUserSettings) return;
|
|
|
|
|
|
|
|
await leaveWorkspace(workspaceSlug.toString())
|
2023-11-28 11:35:42 +00:00
|
|
|
.then(() => router.push("/profile"))
|
2023-11-23 09:14:06 +00:00
|
|
|
.catch((err) =>
|
|
|
|
setToastAlert({
|
|
|
|
type: "error",
|
|
|
|
title: "Error",
|
|
|
|
message: err?.error || "Something went wrong. Please try again.",
|
|
|
|
})
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2023-10-30 15:08:50 +00:00
|
|
|
const handleRemoveMember = async () => {
|
|
|
|
if (!workspaceSlug) return;
|
|
|
|
|
2023-11-23 09:14:06 +00:00
|
|
|
await removeMember(workspaceSlug.toString(), member.id).catch((err) =>
|
|
|
|
setToastAlert({
|
|
|
|
type: "error",
|
|
|
|
title: "Error",
|
|
|
|
message: err?.error || "Something went wrong. Please try again.",
|
|
|
|
})
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleRemoveInvitation = async () => {
|
|
|
|
if (!workspaceSlug) return;
|
2023-11-13 08:04:05 +00:00
|
|
|
|
2023-11-23 09:14:06 +00:00
|
|
|
await deleteWorkspaceInvitation(workspaceSlug.toString(), member.id)
|
|
|
|
.then(() =>
|
|
|
|
setToastAlert({
|
|
|
|
type: "success",
|
|
|
|
title: "Success",
|
|
|
|
message: "Invitation removed successfully.",
|
2023-11-13 08:04:05 +00:00
|
|
|
})
|
2023-11-23 09:14:06 +00:00
|
|
|
)
|
|
|
|
.catch((err) =>
|
|
|
|
setToastAlert({
|
|
|
|
type: "error",
|
|
|
|
title: "Error",
|
|
|
|
message: err?.error || "Something went wrong. Please try again.",
|
2023-10-30 15:08:50 +00:00
|
|
|
})
|
2023-11-23 09:14:06 +00:00
|
|
|
)
|
|
|
|
.finally(() =>
|
|
|
|
mutate(`WORKSPACE_INVITATIONS_${workspaceSlug.toString()}`, (prevData: any) => {
|
|
|
|
if (!prevData) return prevData;
|
|
|
|
|
|
|
|
return prevData.filter((item: any) => item.id !== member.id);
|
2023-11-13 08:04:05 +00:00
|
|
|
})
|
2023-11-23 09:14:06 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleRemove = async () => {
|
|
|
|
if (member.member) {
|
|
|
|
const memberId = member.memberId;
|
2023-11-13 08:04:05 +00:00
|
|
|
|
2023-11-23 09:14:06 +00:00
|
|
|
if (memberId === currentUser?.id) await handleLeaveWorkspace();
|
|
|
|
else await handleRemoveMember();
|
|
|
|
} else await handleRemoveInvitation();
|
2023-10-30 15:08:50 +00:00
|
|
|
};
|
|
|
|
|
2023-11-28 11:35:42 +00:00
|
|
|
// is the member current logged in user
|
|
|
|
const isCurrentUser = member.memberId === currentWorkspaceMemberInfo?.member;
|
|
|
|
// is the current logged in user admin
|
|
|
|
const isAdmin = currentWorkspaceRole === EUserWorkspaceRoles.ADMIN;
|
|
|
|
// role change access-
|
|
|
|
// 1. user cannot change their own role
|
|
|
|
// 2. only admin or member can change role
|
|
|
|
// 3. user cannot change role of higher role
|
|
|
|
const hasRoleChangeAccess =
|
|
|
|
currentWorkspaceRole &&
|
|
|
|
!isCurrentUser &&
|
|
|
|
[EUserWorkspaceRoles.ADMIN, EUserWorkspaceRoles.MEMBER].includes(currentWorkspaceRole) &&
|
|
|
|
member.role <= currentWorkspaceRole;
|
|
|
|
|
2023-11-01 13:52:10 +00:00
|
|
|
if (!currentWorkspaceMemberInfo) return null;
|
2023-10-30 15:08:50 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<ConfirmWorkspaceMemberRemove
|
|
|
|
isOpen={removeMemberModal}
|
|
|
|
onClose={() => setRemoveMemberModal(false)}
|
|
|
|
data={member}
|
2023-11-23 09:14:06 +00:00
|
|
|
onSubmit={handleRemove}
|
2023-10-30 15:08:50 +00:00
|
|
|
/>
|
|
|
|
<div className="group flex items-center justify-between px-3 py-4 hover:bg-custom-background-90">
|
|
|
|
<div className="flex items-center gap-x-4 gap-y-2">
|
|
|
|
{member.avatar && member.avatar !== "" ? (
|
|
|
|
<Link href={`/${workspaceSlug}/profile/${member.memberId}`}>
|
2023-11-29 15:02:10 +00:00
|
|
|
<span className="relative flex h-10 w-10 items-center justify-center rounded p-4 capitalize text-white">
|
2023-10-30 15:08:50 +00:00
|
|
|
<img
|
|
|
|
src={member.avatar}
|
|
|
|
className="absolute top-0 left-0 h-full w-full object-cover rounded"
|
|
|
|
alt={member.display_name || member.email}
|
|
|
|
/>
|
2023-11-29 15:02:10 +00:00
|
|
|
</span>
|
2023-10-30 15:08:50 +00:00
|
|
|
</Link>
|
|
|
|
) : (
|
|
|
|
<Link href={`/${workspaceSlug}/profile/${member.memberId}`}>
|
2023-11-29 15:02:10 +00:00
|
|
|
<span className="relative flex h-10 w-10 items-center justify-center rounded p-4 capitalize bg-gray-700 text-white">
|
2023-10-30 15:08:50 +00:00
|
|
|
{(member.email ?? member.display_name ?? "?")[0]}
|
2023-11-29 15:02:10 +00:00
|
|
|
</span>
|
2023-10-30 15:08:50 +00:00
|
|
|
</Link>
|
|
|
|
)}
|
|
|
|
<div>
|
|
|
|
{member.member ? (
|
|
|
|
<Link href={`/${workspaceSlug}/profile/${member.memberId}`}>
|
2023-11-29 15:02:10 +00:00
|
|
|
<span className="text-sm font-medium">
|
2023-10-30 15:08:50 +00:00
|
|
|
{member.first_name} {member.last_name}
|
2023-11-29 15:02:10 +00:00
|
|
|
</span>
|
2023-10-30 15:08:50 +00:00
|
|
|
</Link>
|
|
|
|
) : (
|
|
|
|
<h4 className="text-sm cursor-default">{member.display_name || member.email}</h4>
|
|
|
|
)}
|
2023-11-22 07:28:55 +00:00
|
|
|
<div className="flex items-center">
|
|
|
|
<p className="text-xs text-custom-text-300">{member.display_name}</p>
|
|
|
|
{isAdmin && (
|
|
|
|
<>
|
|
|
|
<Dot height={16} width={16} className="text-custom-text-300" />
|
|
|
|
<p className="text-xs text-custom-text-300">{member.email}</p>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</div>
|
2023-10-30 15:08:50 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div className="flex items-center gap-2 text-xs">
|
|
|
|
{!member?.status && (
|
|
|
|
<div className="flex items-center justify-center rounded bg-yellow-500/20 px-2.5 py-1 text-center text-xs text-yellow-500 font-medium">
|
|
|
|
<p>Pending</p>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
{member?.status && !member?.accountCreated && (
|
|
|
|
<div className="flex items-center justify-center rounded bg-blue-500/20 px-2.5 py-1 text-center text-xs text-blue-500 font-medium">
|
|
|
|
<p>Account not created</p>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
<CustomSelect
|
|
|
|
customButton={
|
|
|
|
<div className="flex item-center gap-1 px-2 py-0.5 rounded">
|
|
|
|
<span
|
|
|
|
className={`flex items-center text-xs font-medium rounded ${
|
2023-11-28 11:35:42 +00:00
|
|
|
hasRoleChangeAccess ? "" : "text-custom-sidebar-text-400"
|
2023-10-30 15:08:50 +00:00
|
|
|
}`}
|
|
|
|
>
|
|
|
|
{ROLE[member.role as keyof typeof ROLE]}
|
|
|
|
</span>
|
2023-11-28 11:35:42 +00:00
|
|
|
{hasRoleChangeAccess && (
|
2023-10-30 15:08:50 +00:00
|
|
|
<span className="grid place-items-center">
|
|
|
|
<ChevronDown className="h-3 w-3" />
|
|
|
|
</span>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
value={member.role}
|
2023-11-02 11:41:33 +00:00
|
|
|
onChange={(value: TUserWorkspaceRole | undefined) => {
|
2023-11-01 13:52:10 +00:00
|
|
|
if (!workspaceSlug || !value) return;
|
2023-10-30 15:08:50 +00:00
|
|
|
|
2023-11-08 19:05:12 +00:00
|
|
|
updateMember(workspaceSlug.toString(), member.id, {
|
|
|
|
role: value,
|
|
|
|
}).catch(() => {
|
|
|
|
setToastAlert({
|
|
|
|
type: "error",
|
|
|
|
title: "Error!",
|
|
|
|
message: "An error occurred while updating member role. Please try again.",
|
2023-10-30 15:08:50 +00:00
|
|
|
});
|
2023-11-08 19:05:12 +00:00
|
|
|
});
|
2023-10-30 15:08:50 +00:00
|
|
|
}}
|
2023-11-28 11:35:42 +00:00
|
|
|
disabled={!hasRoleChangeAccess}
|
2023-10-30 15:08:50 +00:00
|
|
|
placement="bottom-end"
|
|
|
|
>
|
|
|
|
{Object.keys(ROLE).map((key) => {
|
2023-11-01 13:52:10 +00:00
|
|
|
if (currentWorkspaceRole && currentWorkspaceRole !== 20 && currentWorkspaceRole < parseInt(key))
|
|
|
|
return null;
|
2023-10-30 15:08:50 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<CustomSelect.Option key={key} value={parseInt(key, 10)}>
|
|
|
|
<>{ROLE[parseInt(key) as keyof typeof ROLE]}</>
|
|
|
|
</CustomSelect.Option>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</CustomSelect>
|
2023-11-28 11:35:42 +00:00
|
|
|
<Tooltip
|
|
|
|
tooltipContent={isCurrentUser ? "Leave workspace" : "Remove member"}
|
|
|
|
disabled={!isAdmin && !isCurrentUser}
|
|
|
|
>
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
onClick={() => setRemoveMemberModal(true)}
|
|
|
|
className={
|
|
|
|
isAdmin || isCurrentUser
|
|
|
|
? "opacity-0 pointer-events-none group-hover:opacity-100 group-hover:pointer-events-auto"
|
|
|
|
: "opacity-0 pointer-events-none"
|
2023-11-01 13:52:10 +00:00
|
|
|
}
|
|
|
|
>
|
2023-12-05 08:14:01 +00:00
|
|
|
<XCircle className="h-3.5 w-3.5 text-red-500" strokeWidth={2} />
|
2023-11-28 11:35:42 +00:00
|
|
|
</button>
|
|
|
|
</Tooltip>
|
2023-10-30 15:08:50 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
);
|
2023-11-28 11:35:42 +00:00
|
|
|
});
|