import { MembershipRole } from "@prisma/client"; import classNames from "classnames"; import { useState } from "react"; import { WEBAPP_URL } from "@calcom/lib/constants"; import { useLocale } from "@calcom/lib/hooks/useLocale"; import { inferQueryOutput, trpc } from "@calcom/trpc/react"; import useMeQuery from "@calcom/trpc/react/hooks/useMeQuery"; import { Icon } from "@calcom/ui/Icon"; import { Button, ButtonGroup, Avatar } from "@calcom/ui/components"; import { Dialog, DialogContent, DialogTrigger, Dropdown, DropdownItem, DropdownMenuContent, DropdownMenuItem, DropdownMenuSeparator, DropdownMenuTrigger, showToast, Tooltip, } from "@calcom/ui/v2/core"; import ConfirmationDialogContent from "@calcom/ui/v2/core/ConfirmationDialogContent"; import MemberChangeRoleModal from "./MemberChangeRoleModal"; import TeamPill, { TeamRole } from "./TeamPill"; import TeamAvailabilityModal from "./v2/TeamAvailabilityModal"; interface Props { team: inferQueryOutput<"viewer.teams.get">; member: inferQueryOutput<"viewer.teams.get">["members"][number]; } /** 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); const [showImpersonateModal, setShowImpersonateModal] = useState(false); const removeMemberMutation = trpc.useMutation("viewer.teams.removeMember", { async onSuccess() { await utils.invalidateQueries(["viewer.teams.get"]); 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); return (