2023-11-01 08:12:51 +00:00
|
|
|
import { useState } from "react";
|
|
|
|
import { useRouter } from "next/router";
|
|
|
|
import Link from "next/link";
|
|
|
|
import useSWR, { mutate } from "swr";
|
|
|
|
import { observer } from "mobx-react-lite";
|
|
|
|
import { useMobxStore } from "lib/mobx/store-provider";
|
|
|
|
// hooks
|
|
|
|
import useToast from "hooks/use-toast";
|
|
|
|
// components
|
|
|
|
import { ConfirmProjectMemberRemove } from "components/project";
|
|
|
|
// ui
|
2023-11-03 11:50:49 +00:00
|
|
|
import { CustomSelect, Tooltip } from "@plane/ui";
|
2023-11-01 08:12:51 +00:00
|
|
|
// icons
|
2023-11-22 07:28:55 +00:00
|
|
|
import { ChevronDown, Dot, XCircle } from "lucide-react";
|
2023-11-01 08:12:51 +00:00
|
|
|
// constants
|
|
|
|
import { ROLE } from "constants/workspace";
|
2023-11-09 12:26:55 +00:00
|
|
|
// types
|
2023-11-02 11:41:33 +00:00
|
|
|
import { TUserProjectRole } from "types";
|
2023-11-01 08:12:51 +00:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
member: any;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const ProjectMemberListItem: React.FC<Props> = observer((props) => {
|
|
|
|
const { member } = props;
|
|
|
|
// router
|
|
|
|
const router = useRouter();
|
|
|
|
const { workspaceSlug, projectId } = router.query;
|
|
|
|
// states
|
|
|
|
const [selectedRemoveMember, setSelectedRemoveMember] = useState<any | null>(null);
|
|
|
|
const [selectedInviteRemoveMember, setSelectedInviteRemoveMember] = useState<any | null>(null);
|
|
|
|
// store
|
2023-11-09 12:26:55 +00:00
|
|
|
const {
|
|
|
|
user: userStore,
|
|
|
|
projectMember: {
|
|
|
|
projectMembers,
|
|
|
|
fetchProjectMembers,
|
|
|
|
removeMemberFromProject,
|
|
|
|
updateMember,
|
|
|
|
deleteProjectInvitation,
|
|
|
|
},
|
|
|
|
} = useMobxStore();
|
2023-11-01 13:52:10 +00:00
|
|
|
// hooks
|
2023-11-01 08:12:51 +00:00
|
|
|
const { setToastAlert } = useToast();
|
2023-11-01 13:52:10 +00:00
|
|
|
// fetching project members
|
2023-11-01 08:12:51 +00:00
|
|
|
useSWR(
|
|
|
|
workspaceSlug && projectId ? `PROJECT_MEMBERS_${projectId.toString().toUpperCase()}` : null,
|
2023-11-09 12:26:55 +00:00
|
|
|
workspaceSlug && projectId ? () => fetchProjectMembers(workspaceSlug.toString(), projectId.toString()) : null
|
2023-11-01 08:12:51 +00:00
|
|
|
);
|
|
|
|
// derived values
|
|
|
|
const user = userStore.currentUser;
|
2023-11-03 11:50:49 +00:00
|
|
|
const { currentProjectMemberInfo, currentProjectRole } = userStore;
|
2023-11-01 13:52:10 +00:00
|
|
|
const isAdmin = currentProjectRole === 20;
|
2023-11-01 08:12:51 +00:00
|
|
|
const currentUser = projectMembers?.find((item) => item.member.id === user?.id);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<ConfirmProjectMemberRemove
|
|
|
|
isOpen={Boolean(selectedRemoveMember) || Boolean(selectedInviteRemoveMember)}
|
|
|
|
onClose={() => {
|
|
|
|
setSelectedRemoveMember(null);
|
|
|
|
setSelectedInviteRemoveMember(null);
|
|
|
|
}}
|
|
|
|
data={selectedRemoveMember ?? selectedInviteRemoveMember}
|
|
|
|
handleDelete={async () => {
|
|
|
|
if (!workspaceSlug || !projectId) return;
|
|
|
|
|
|
|
|
// if the user is a member
|
|
|
|
if (selectedRemoveMember) {
|
2023-11-09 12:26:55 +00:00
|
|
|
await removeMemberFromProject(workspaceSlug.toString(), projectId.toString(), selectedRemoveMember.id);
|
2023-11-01 08:12:51 +00:00
|
|
|
}
|
|
|
|
// if the user is an invite
|
|
|
|
if (selectedInviteRemoveMember) {
|
2023-11-09 12:26:55 +00:00
|
|
|
await deleteProjectInvitation(
|
2023-11-01 08:12:51 +00:00
|
|
|
workspaceSlug.toString(),
|
|
|
|
projectId.toString(),
|
2023-11-03 11:50:49 +00:00
|
|
|
selectedInviteRemoveMember.id
|
2023-11-01 08:12:51 +00:00
|
|
|
);
|
|
|
|
mutate(`PROJECT_INVITATIONS_${projectId.toString()}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
setToastAlert({
|
|
|
|
type: "success",
|
|
|
|
message: "Member removed successfully",
|
|
|
|
title: "Success",
|
|
|
|
});
|
|
|
|
}}
|
|
|
|
/>
|
2023-11-03 11:50:49 +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">
|
2023-11-01 08:12:51 +00:00
|
|
|
{member.avatar && member.avatar !== "" ? (
|
2023-11-03 11:50:49 +00:00
|
|
|
<Link href={`/${workspaceSlug}/profile/${member.memberId}`}>
|
|
|
|
<a className="relative flex h-10 w-10 items-center justify-center rounded p-4 capitalize text-white">
|
|
|
|
<img
|
|
|
|
src={member.avatar}
|
|
|
|
alt={member.display_name || member.email}
|
|
|
|
className="absolute top-0 left-0 h-full w-full object-cover rounded"
|
|
|
|
/>
|
|
|
|
</a>
|
|
|
|
</Link>
|
2023-11-01 08:12:51 +00:00
|
|
|
) : (
|
2023-11-03 11:50:49 +00:00
|
|
|
<Link href={`/${workspaceSlug}/profile/${member.memberId}`}>
|
|
|
|
<a className="relative flex h-10 w-10 items-center justify-center rounded p-4 capitalize bg-gray-700 text-white">
|
|
|
|
{(member.display_name ?? member.email ?? "?")[0]}
|
|
|
|
</a>
|
|
|
|
</Link>
|
2023-11-01 08:12:51 +00:00
|
|
|
)}
|
2023-11-03 11:50:49 +00:00
|
|
|
|
2023-11-01 08:12:51 +00:00
|
|
|
<div>
|
|
|
|
{member.member ? (
|
|
|
|
<Link href={`/${workspaceSlug}/profile/${member.memberId}`}>
|
2023-11-03 11:50:49 +00:00
|
|
|
<a className="text-sm font-medium">
|
|
|
|
{member.first_name} {member.last_name}
|
2023-11-01 08:12:51 +00:00
|
|
|
</a>
|
|
|
|
</Link>
|
|
|
|
) : (
|
2023-11-03 11:50:49 +00:00
|
|
|
<h4 className="text-sm cursor-default">{member.display_name || member.email}</h4>
|
2023-11-01 08:12:51 +00:00
|
|
|
)}
|
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-11-01 08:12:51 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
2023-11-03 11:50:49 +00:00
|
|
|
|
|
|
|
<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>
|
2023-11-01 08:12:51 +00:00
|
|
|
</div>
|
|
|
|
)}
|
2023-11-03 11:50:49 +00:00
|
|
|
|
2023-11-01 08:12:51 +00:00
|
|
|
<CustomSelect
|
|
|
|
customButton={
|
2023-11-03 11:50:49 +00:00
|
|
|
<div className="flex item-center gap-1 px-2 py-0.5 rounded">
|
2023-11-01 08:12:51 +00:00
|
|
|
<span
|
2023-11-03 11:50:49 +00:00
|
|
|
className={`flex items-center text-xs font-medium rounded ${
|
|
|
|
member.memberId !== currentProjectMemberInfo?.id ? "" : "text-custom-sidebar-text-400"
|
2023-11-01 08:12:51 +00:00
|
|
|
}`}
|
|
|
|
>
|
|
|
|
{ROLE[member.role as keyof typeof ROLE]}
|
|
|
|
</span>
|
2023-11-03 11:50:49 +00:00
|
|
|
{member.memberId !== currentProjectMemberInfo?.id && (
|
|
|
|
<span className="grid place-items-center">
|
|
|
|
<ChevronDown className="h-3 w-3" />
|
|
|
|
</span>
|
|
|
|
)}
|
2023-11-01 08:12:51 +00:00
|
|
|
</div>
|
|
|
|
}
|
|
|
|
value={member.role}
|
2023-11-02 11:41:33 +00:00
|
|
|
onChange={(value: TUserProjectRole | undefined) => {
|
2023-11-01 08:12:51 +00:00
|
|
|
if (!workspaceSlug || !projectId) return;
|
|
|
|
|
2023-11-09 12:26:55 +00:00
|
|
|
updateMember(workspaceSlug.toString(), projectId.toString(), member.id, {
|
|
|
|
role: value,
|
|
|
|
}).catch((err) => {
|
|
|
|
const error = err.error;
|
|
|
|
const errorString = Array.isArray(error) ? error[0] : error;
|
2023-11-01 08:12:51 +00:00
|
|
|
|
2023-11-09 12:26:55 +00:00
|
|
|
setToastAlert({
|
|
|
|
type: "error",
|
|
|
|
title: "Error!",
|
|
|
|
message: errorString ?? "An error occurred while updating member role. Please try again.",
|
2023-11-01 08:12:51 +00:00
|
|
|
});
|
2023-11-09 12:26:55 +00:00
|
|
|
});
|
2023-11-01 08:12:51 +00:00
|
|
|
}}
|
|
|
|
disabled={
|
|
|
|
member.memberId === user?.id ||
|
|
|
|
!member.member ||
|
|
|
|
(currentUser && currentUser.role !== 20 && currentUser.role < member.role)
|
|
|
|
}
|
2023-11-03 11:50:49 +00:00
|
|
|
placement="bottom-end"
|
2023-11-01 08:12:51 +00:00
|
|
|
>
|
|
|
|
{Object.keys(ROLE).map((key) => {
|
2023-11-03 11:50:49 +00:00
|
|
|
if (currentProjectRole && currentProjectRole !== 20 && currentProjectRole < parseInt(key)) return null;
|
2023-11-01 08:12:51 +00:00
|
|
|
|
|
|
|
return (
|
2023-11-03 11:50:49 +00:00
|
|
|
<CustomSelect.Option key={key} value={parseInt(key, 10)}>
|
2023-11-01 08:12:51 +00:00
|
|
|
<>{ROLE[parseInt(key) as keyof typeof ROLE]}</>
|
|
|
|
</CustomSelect.Option>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</CustomSelect>
|
2023-11-03 11:50:49 +00:00
|
|
|
{isAdmin && (
|
|
|
|
<Tooltip
|
|
|
|
tooltipContent={member.memberId === currentProjectMemberInfo?.member ? "Leave project" : "Remove member"}
|
2023-11-01 08:12:51 +00:00
|
|
|
>
|
2023-11-03 11:50:49 +00:00
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
onClick={() => {
|
|
|
|
if (member.member) setSelectedRemoveMember(member);
|
|
|
|
else setSelectedInviteRemoveMember(member);
|
|
|
|
}}
|
|
|
|
className="opacity-0 pointer-events-none group-hover:opacity-100 group-hover:pointer-events-auto"
|
|
|
|
>
|
|
|
|
<XCircle className="h-3.5 w-3.5 text-custom-text-400" strokeWidth={2} />
|
|
|
|
</button>
|
|
|
|
</Tooltip>
|
|
|
|
)}
|
2023-11-01 08:12:51 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
});
|