2022-09-12 22:04:33 +00:00
|
|
|
import { MembershipRole } from "@prisma/client";
|
|
|
|
import classNames from "classnames";
|
2022-12-05 12:03:36 +00:00
|
|
|
import { signIn } from "next-auth/react";
|
2022-09-12 22:04:33 +00:00
|
|
|
import { useState } from "react";
|
|
|
|
|
|
|
|
import { WEBAPP_URL } from "@calcom/lib/constants";
|
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
2023-02-21 17:12:26 +00:00
|
|
|
import type { RouterOutputs } from "@calcom/trpc/react";
|
|
|
|
import { trpc } from "@calcom/trpc/react";
|
2022-09-12 22:04:33 +00:00
|
|
|
import useMeQuery from "@calcom/trpc/react/hooks/useMeQuery";
|
|
|
|
import {
|
2022-11-23 02:55:25 +00:00
|
|
|
Avatar,
|
|
|
|
Button,
|
|
|
|
ButtonGroup,
|
|
|
|
ConfirmationDialogContent,
|
2022-09-12 22:04:33 +00:00
|
|
|
Dialog,
|
2022-12-05 12:03:36 +00:00
|
|
|
DialogClose,
|
2022-09-12 22:04:33 +00:00
|
|
|
DialogContent,
|
2022-11-28 19:14:38 +00:00
|
|
|
DialogFooter,
|
2022-09-12 22:04:33 +00:00
|
|
|
Dropdown,
|
|
|
|
DropdownItem,
|
|
|
|
DropdownMenuContent,
|
|
|
|
DropdownMenuItem,
|
|
|
|
DropdownMenuSeparator,
|
|
|
|
DropdownMenuTrigger,
|
|
|
|
showToast,
|
|
|
|
Tooltip,
|
2022-11-23 02:55:25 +00:00
|
|
|
} from "@calcom/ui";
|
2023-04-12 15:26:31 +00:00
|
|
|
import { ExternalLink, MoreHorizontal, Edit2, Lock, Trash } from "@calcom/ui/components/icon";
|
2022-09-12 22:04:33 +00:00
|
|
|
|
|
|
|
import MemberChangeRoleModal from "./MemberChangeRoleModal";
|
2023-04-19 14:15:08 +00:00
|
|
|
import TeamAvailabilityModal from "./TeamAvailabilityModal";
|
2022-09-12 22:04:33 +00:00
|
|
|
import TeamPill, { TeamRole } from "./TeamPill";
|
|
|
|
|
|
|
|
interface Props {
|
2022-11-10 23:40:01 +00:00
|
|
|
team: RouterOutputs["viewer"]["teams"]["get"];
|
|
|
|
member: RouterOutputs["viewer"]["teams"]["get"]["members"][number];
|
2022-09-12 22:04:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** TODO: Migrate the one in apps/web to tRPC package */
|
|
|
|
const useCurrentUserId = () => {
|
|
|
|
const query = useMeQuery();
|
|
|
|
const user = query.data;
|
|
|
|
return user?.id;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default function MemberListItem(props: Props) {
|
|
|
|
const { t } = useLocale();
|
|
|
|
|
|
|
|
const utils = trpc.useContext();
|
|
|
|
const [showChangeMemberRoleModal, setShowChangeMemberRoleModal] = useState(false);
|
|
|
|
const [showTeamAvailabilityModal, setShowTeamAvailabilityModal] = useState(false);
|
2022-12-05 12:03:36 +00:00
|
|
|
const [showImpersonateModal, setShowImpersonateModal] = useState(false);
|
|
|
|
const [showDeleteModal, setShowDeleteModal] = useState(false);
|
2022-09-12 22:04:33 +00:00
|
|
|
|
2022-11-10 23:40:01 +00:00
|
|
|
const removeMemberMutation = trpc.viewer.teams.removeMember.useMutation({
|
2022-09-12 22:04:33 +00:00
|
|
|
async onSuccess() {
|
2022-11-10 23:40:01 +00:00
|
|
|
await utils.viewer.teams.get.invalidate();
|
2023-04-13 02:10:23 +00:00
|
|
|
await utils.viewer.eventTypes.invalidate();
|
2022-09-12 22:04:33 +00:00
|
|
|
showToast(t("success"), "success");
|
|
|
|
},
|
|
|
|
async onError(err) {
|
|
|
|
showToast(err.message, "error");
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const ownersInTeam = () => {
|
|
|
|
const { members } = props.team;
|
|
|
|
const owners = members.filter((member) => member["role"] === MembershipRole.OWNER && member["accepted"]);
|
|
|
|
return owners.length;
|
|
|
|
};
|
|
|
|
|
|
|
|
const currentUserId = useCurrentUserId();
|
|
|
|
|
|
|
|
const name =
|
|
|
|
props.member.name ||
|
|
|
|
(() => {
|
|
|
|
const emailName = props.member.email.split("@")[0];
|
|
|
|
return emailName.charAt(0).toUpperCase() + emailName.slice(1);
|
|
|
|
})();
|
|
|
|
|
|
|
|
const removeMember = () =>
|
|
|
|
removeMemberMutation.mutate({ teamId: props.team?.id, memberId: props.member.id });
|
|
|
|
|
|
|
|
const editMode =
|
|
|
|
(props.team.membership.role === MembershipRole.OWNER &&
|
|
|
|
(props.member.role !== MembershipRole.OWNER ||
|
|
|
|
ownersInTeam() > 1 ||
|
|
|
|
props.member.id !== currentUserId)) ||
|
|
|
|
(props.team.membership.role === MembershipRole.ADMIN && props.member.role !== MembershipRole.OWNER);
|
2022-12-05 12:03:36 +00:00
|
|
|
const impersonationMode =
|
|
|
|
editMode &&
|
|
|
|
!props.member.disableImpersonation &&
|
|
|
|
props.member.accepted &&
|
|
|
|
process.env.NEXT_PUBLIC_TEAM_IMPERSONATION === "true";
|
2022-09-12 22:04:33 +00:00
|
|
|
|
2023-04-05 09:54:25 +00:00
|
|
|
const urlWithoutProtocol = WEBAPP_URL.replace(/^https?:\/\//, "");
|
2023-04-13 07:04:52 +00:00
|
|
|
const bookingLink = !!props.member.username && `${urlWithoutProtocol}/${props.member.username}`;
|
2023-04-02 10:26:22 +00:00
|
|
|
|
2022-09-12 22:04:33 +00:00
|
|
|
return (
|
2023-04-11 12:15:10 +00:00
|
|
|
<li className="divide-subtle divide-y px-5">
|
2022-09-12 22:04:33 +00:00
|
|
|
<div className="my-4 flex justify-between">
|
|
|
|
<div className="flex w-full flex-col justify-between sm:flex-row">
|
|
|
|
<div className="flex">
|
|
|
|
<Avatar
|
|
|
|
size="sm"
|
|
|
|
imageSrc={WEBAPP_URL + "/" + props.member.username + "/avatar.png"}
|
|
|
|
alt={name || ""}
|
|
|
|
className="h-10 w-10 rounded-full"
|
|
|
|
/>
|
|
|
|
|
2023-04-09 09:13:57 +00:00
|
|
|
<div className="ms-3 inline-block">
|
2022-09-12 22:04:33 +00:00
|
|
|
<div className="mb-1 flex">
|
2023-04-05 18:14:46 +00:00
|
|
|
<span className="text-default mr-1 text-sm font-bold leading-4">{name}</span>
|
2022-09-12 22:04:33 +00:00
|
|
|
|
|
|
|
{!props.member.accepted && <TeamPill color="orange" text={t("pending")} />}
|
|
|
|
{props.member.role && <TeamRole role={props.member.role} />}
|
|
|
|
</div>
|
2023-04-05 18:14:46 +00:00
|
|
|
<div className="text-default flex items-center">
|
|
|
|
<span className=" block text-sm" data-testid="member-email" data-email={props.member.email}>
|
2023-04-02 10:26:22 +00:00
|
|
|
{props.member.email}
|
|
|
|
</span>
|
2023-04-03 12:08:17 +00:00
|
|
|
{bookingLink && (
|
|
|
|
<>
|
2023-04-09 09:13:57 +00:00
|
|
|
<span className="text-default mx-2 block">•</span>
|
2023-04-05 09:54:25 +00:00
|
|
|
<a
|
|
|
|
target="_blank"
|
|
|
|
href={`${WEBAPP_URL}/${props.member.username}`}
|
2023-04-09 09:13:57 +00:00
|
|
|
className="text-default block text-sm">
|
2023-04-05 09:54:25 +00:00
|
|
|
{bookingLink}
|
|
|
|
</a>
|
2023-04-03 12:08:17 +00:00
|
|
|
</>
|
|
|
|
)}
|
2023-04-02 10:26:22 +00:00
|
|
|
</div>
|
2022-09-12 22:04:33 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
{props.team.membership.accepted && (
|
|
|
|
<div className="flex items-center justify-center">
|
2023-04-05 18:14:46 +00:00
|
|
|
<ButtonGroup combined containerProps={{ className: "border-default hidden md:flex" }}>
|
2023-02-17 10:09:49 +00:00
|
|
|
{/* TODO: bring availability back. right now its ugly and broken
|
|
|
|
<Tooltip
|
2022-09-12 22:04:33 +00:00
|
|
|
content={
|
|
|
|
props.member.accepted
|
|
|
|
? t("team_view_user_availability")
|
|
|
|
: t("team_view_user_availability_disabled")
|
|
|
|
}>
|
|
|
|
<Button
|
|
|
|
disabled={!props.member.accepted}
|
|
|
|
onClick={() => (props.member.accepted ? setShowTeamAvailabilityModal(true) : null)}
|
|
|
|
color="secondary"
|
2023-01-19 14:55:32 +00:00
|
|
|
variant="icon"
|
2023-04-12 15:26:31 +00:00
|
|
|
StartIcon={Clock}
|
2022-09-12 22:04:33 +00:00
|
|
|
/>
|
2023-02-17 10:09:49 +00:00
|
|
|
</Tooltip> */}
|
2022-09-12 22:04:33 +00:00
|
|
|
<Tooltip content={t("view_public_page")}>
|
|
|
|
<Button
|
|
|
|
target="_blank"
|
|
|
|
href={"/" + props.member.username}
|
|
|
|
color="secondary"
|
|
|
|
className={classNames(!editMode ? "rounded-r-md" : "")}
|
2023-01-19 14:55:32 +00:00
|
|
|
variant="icon"
|
2023-04-12 15:26:31 +00:00
|
|
|
StartIcon={ExternalLink}
|
2022-09-12 22:04:33 +00:00
|
|
|
/>
|
|
|
|
</Tooltip>
|
|
|
|
{editMode && (
|
|
|
|
<Dropdown>
|
2023-01-22 00:03:59 +00:00
|
|
|
<DropdownMenuTrigger asChild>
|
2022-09-12 22:04:33 +00:00
|
|
|
<Button
|
2023-01-22 00:03:59 +00:00
|
|
|
className="radix-state-open:rounded-r-md"
|
2022-09-12 22:04:33 +00:00
|
|
|
color="secondary"
|
2023-01-19 14:55:32 +00:00
|
|
|
variant="icon"
|
2023-04-12 15:26:31 +00:00
|
|
|
StartIcon={MoreHorizontal}
|
2022-09-12 22:04:33 +00:00
|
|
|
/>
|
|
|
|
</DropdownMenuTrigger>
|
|
|
|
<DropdownMenuContent>
|
|
|
|
<DropdownMenuItem>
|
|
|
|
<DropdownItem
|
|
|
|
type="button"
|
|
|
|
onClick={() => setShowChangeMemberRoleModal(true)}
|
2023-04-12 15:26:31 +00:00
|
|
|
StartIcon={Edit2}>
|
2023-01-15 11:40:40 +00:00
|
|
|
{t("edit")}
|
2022-09-12 22:04:33 +00:00
|
|
|
</DropdownItem>
|
|
|
|
</DropdownMenuItem>
|
2022-12-05 12:03:36 +00:00
|
|
|
{impersonationMode && (
|
|
|
|
<>
|
|
|
|
<DropdownMenuItem>
|
2023-01-15 11:40:40 +00:00
|
|
|
<DropdownItem
|
|
|
|
type="button"
|
2022-12-05 12:03:36 +00:00
|
|
|
onClick={() => setShowImpersonateModal(true)}
|
2023-04-12 15:26:31 +00:00
|
|
|
StartIcon={Lock}>
|
2022-12-05 12:03:36 +00:00
|
|
|
{t("impersonate")}
|
2023-01-15 11:40:40 +00:00
|
|
|
</DropdownItem>
|
2022-12-05 12:03:36 +00:00
|
|
|
</DropdownMenuItem>
|
2023-01-15 11:40:40 +00:00
|
|
|
<DropdownMenuSeparator />
|
2022-12-05 12:03:36 +00:00
|
|
|
</>
|
|
|
|
)}
|
|
|
|
<DropdownMenuItem>
|
|
|
|
<DropdownItem
|
|
|
|
type="button"
|
|
|
|
onClick={() => setShowDeleteModal(true)}
|
|
|
|
color="destructive"
|
2023-04-12 15:26:31 +00:00
|
|
|
StartIcon={Trash}>
|
2022-12-05 12:03:36 +00:00
|
|
|
{t("delete")}
|
|
|
|
</DropdownItem>
|
2022-09-12 22:04:33 +00:00
|
|
|
</DropdownMenuItem>
|
|
|
|
</DropdownMenuContent>
|
|
|
|
</Dropdown>
|
|
|
|
)}
|
|
|
|
</ButtonGroup>
|
|
|
|
<div className="flex md:hidden">
|
|
|
|
<Dropdown>
|
|
|
|
<DropdownMenuTrigger asChild>
|
2023-04-12 15:26:31 +00:00
|
|
|
<Button type="button" variant="icon" color="minimal" StartIcon={MoreHorizontal} />
|
2022-09-12 22:04:33 +00:00
|
|
|
</DropdownMenuTrigger>
|
|
|
|
<DropdownMenuContent>
|
|
|
|
<DropdownMenuItem className="outline-none">
|
2023-04-12 15:26:31 +00:00
|
|
|
<DropdownItem type="button" StartIcon={ExternalLink}>
|
2022-09-12 22:04:33 +00:00
|
|
|
{t("view_public_page")}
|
|
|
|
</DropdownItem>
|
|
|
|
</DropdownMenuItem>
|
|
|
|
{editMode && (
|
|
|
|
<>
|
|
|
|
<DropdownMenuItem>
|
|
|
|
<DropdownItem
|
|
|
|
type="button"
|
|
|
|
onClick={() => setShowChangeMemberRoleModal(true)}
|
2023-04-12 15:26:31 +00:00
|
|
|
StartIcon={Edit2}>
|
2023-01-20 19:57:14 +00:00
|
|
|
{t("edit")}
|
2022-09-12 22:04:33 +00:00
|
|
|
</DropdownItem>
|
|
|
|
</DropdownMenuItem>
|
|
|
|
<DropdownMenuItem>
|
2022-12-05 12:03:36 +00:00
|
|
|
<DropdownItem
|
|
|
|
type="button"
|
|
|
|
color="destructive"
|
|
|
|
onClick={() => setShowDeleteModal(true)}
|
2023-04-12 15:26:31 +00:00
|
|
|
StartIcon={Trash}>
|
2023-01-20 19:57:14 +00:00
|
|
|
{t("delete")}
|
2022-12-05 12:03:36 +00:00
|
|
|
</DropdownItem>
|
2022-09-12 22:04:33 +00:00
|
|
|
</DropdownMenuItem>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</DropdownMenuContent>
|
|
|
|
</Dropdown>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
2022-12-05 12:03:36 +00:00
|
|
|
|
|
|
|
{editMode && (
|
|
|
|
<Dialog open={showDeleteModal} onOpenChange={() => setShowDeleteModal(false)}>
|
|
|
|
<ConfirmationDialogContent
|
|
|
|
variety="danger"
|
|
|
|
title={t("remove_member")}
|
|
|
|
confirmBtnText={t("confirm_remove_member")}
|
|
|
|
onConfirm={removeMember}>
|
|
|
|
{t("remove_member_confirmation_message")}
|
|
|
|
</ConfirmationDialogContent>
|
|
|
|
</Dialog>
|
|
|
|
)}
|
|
|
|
|
|
|
|
{showImpersonateModal && props.member.username && (
|
|
|
|
<Dialog open={showImpersonateModal} onOpenChange={() => setShowImpersonateModal(false)}>
|
|
|
|
<DialogContent type="creation" title={t("impersonate")} description={t("impersonation_user_tip")}>
|
|
|
|
<form
|
|
|
|
onSubmit={async (e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
await signIn("impersonation-auth", {
|
2023-02-21 17:12:26 +00:00
|
|
|
username: props.member.username || props.member.email,
|
2022-12-05 12:03:36 +00:00
|
|
|
teamId: props.team.id,
|
|
|
|
});
|
|
|
|
setShowImpersonateModal(false);
|
|
|
|
}}>
|
|
|
|
<DialogFooter>
|
|
|
|
<DialogClose color="secondary">{t("cancel")}</DialogClose>
|
|
|
|
<Button color="primary" type="submit">
|
|
|
|
{t("impersonate")}
|
|
|
|
</Button>
|
|
|
|
</DialogFooter>
|
|
|
|
</form>
|
|
|
|
</DialogContent>
|
|
|
|
</Dialog>
|
|
|
|
)}
|
|
|
|
|
2022-09-12 22:04:33 +00:00
|
|
|
{showChangeMemberRoleModal && (
|
|
|
|
<MemberChangeRoleModal
|
|
|
|
isOpen={showChangeMemberRoleModal}
|
|
|
|
currentMember={props.team.membership.role}
|
|
|
|
teamId={props.team?.id}
|
|
|
|
memberId={props.member.id}
|
|
|
|
initialRole={props.member.role as MembershipRole}
|
|
|
|
onExit={() => setShowChangeMemberRoleModal(false)}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
{showTeamAvailabilityModal && (
|
|
|
|
<Dialog open={showTeamAvailabilityModal} onOpenChange={() => setShowTeamAvailabilityModal(false)}>
|
2022-11-28 19:14:38 +00:00
|
|
|
<DialogContent type="creation" size="md">
|
2022-09-12 22:04:33 +00:00
|
|
|
<TeamAvailabilityModal team={props.team} member={props.member} />
|
2022-11-28 19:14:38 +00:00
|
|
|
<DialogFooter>
|
2022-09-12 22:04:33 +00:00
|
|
|
<Button onClick={() => setShowTeamAvailabilityModal(false)}>{t("done")}</Button>
|
2022-11-28 19:14:38 +00:00
|
|
|
</DialogFooter>
|
2022-09-12 22:04:33 +00:00
|
|
|
</DialogContent>
|
|
|
|
</Dialog>
|
|
|
|
)}
|
|
|
|
</li>
|
|
|
|
);
|
|
|
|
}
|