2022-09-12 22:04:33 +00:00
|
|
|
import { useSession } from "next-auth/react";
|
|
|
|
import { useRouter } from "next/router";
|
|
|
|
import { useState } from "react";
|
|
|
|
|
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
2023-05-02 11:44:05 +00:00
|
|
|
import { MembershipRole } from "@calcom/prisma/enums";
|
2022-09-12 22:04:33 +00:00
|
|
|
import { trpc } from "@calcom/trpc/react";
|
2023-05-05 09:35:27 +00:00
|
|
|
import type { RouterOutputs } from "@calcom/trpc/react";
|
|
|
|
import { Button, Meta, TextField, showToast } from "@calcom/ui";
|
2023-04-12 15:26:31 +00:00
|
|
|
import { Plus } from "@calcom/ui/components/icon";
|
2022-09-12 22:04:33 +00:00
|
|
|
|
2023-01-05 17:00:16 +00:00
|
|
|
import { getLayout } from "../../../settings/layouts/SettingsLayout";
|
2022-09-12 22:04:33 +00:00
|
|
|
import DisableTeamImpersonation from "../components/DisableTeamImpersonation";
|
2023-06-06 23:34:14 +00:00
|
|
|
import InviteLinkSettingsModal from "../components/InviteLinkSettingsModal";
|
2022-09-12 22:04:33 +00:00
|
|
|
import MemberInvitationModal from "../components/MemberInvitationModal";
|
|
|
|
import MemberListItem from "../components/MemberListItem";
|
|
|
|
import TeamInviteList from "../components/TeamInviteList";
|
|
|
|
|
2023-05-05 09:35:27 +00:00
|
|
|
type Team = RouterOutputs["viewer"]["teams"]["get"];
|
|
|
|
|
|
|
|
interface MembersListProps {
|
|
|
|
team: Team | undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
const checkIfExist = (comp: string, query: string) =>
|
|
|
|
comp.toLowerCase().replace(/\s+/g, "").includes(query.toLowerCase().replace(/\s+/g, ""));
|
|
|
|
|
|
|
|
function MembersList(props: MembersListProps) {
|
|
|
|
const { team } = props;
|
|
|
|
const { t } = useLocale();
|
|
|
|
const [query, setQuery] = useState<string>("");
|
|
|
|
|
|
|
|
const members = team?.members;
|
|
|
|
const membersList = members
|
|
|
|
? members && query === ""
|
|
|
|
? members
|
|
|
|
: members.filter((member) => {
|
|
|
|
const email = member.email ? checkIfExist(member.email, query) : false;
|
|
|
|
const username = member.username ? checkIfExist(member.username, query) : false;
|
|
|
|
const name = member.name ? checkIfExist(member.name, query) : false;
|
|
|
|
|
|
|
|
return email || username || name;
|
|
|
|
})
|
|
|
|
: undefined;
|
|
|
|
return (
|
|
|
|
<div className="flex flex-col gap-y-3">
|
|
|
|
<TextField
|
|
|
|
type="search"
|
|
|
|
autoComplete="false"
|
|
|
|
onChange={(e) => setQuery(e.target.value)}
|
|
|
|
value={query}
|
|
|
|
placeholder={`${t("search")}...`}
|
|
|
|
/>
|
|
|
|
{membersList?.length && team ? (
|
|
|
|
<ul className="divide-subtle border-subtle divide-y rounded-md border ">
|
|
|
|
{membersList.map((member) => {
|
|
|
|
return <MemberListItem key={member.id} team={team} member={member} />;
|
|
|
|
})}
|
|
|
|
</ul>
|
|
|
|
) : null}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-09-12 22:04:33 +00:00
|
|
|
const MembersView = () => {
|
2022-11-10 20:23:56 +00:00
|
|
|
const { t, i18n } = useLocale();
|
2023-06-14 09:22:44 +00:00
|
|
|
|
2022-09-12 22:04:33 +00:00
|
|
|
const router = useRouter();
|
|
|
|
const session = useSession();
|
2022-11-10 20:23:56 +00:00
|
|
|
const utils = trpc.useContext();
|
2023-06-06 23:34:14 +00:00
|
|
|
const teamId = Number(router.query.id);
|
|
|
|
|
2023-05-24 01:01:31 +00:00
|
|
|
const showDialog = router.query.inviteModal === "true";
|
|
|
|
const [showMemberInvitationModal, setShowMemberInvitationModal] = useState(showDialog);
|
2023-06-06 23:34:14 +00:00
|
|
|
const [showInviteLinkSettingsModal, setInviteLinkSettingsModal] = useState(false);
|
2022-09-12 22:04:33 +00:00
|
|
|
|
2022-11-10 23:40:01 +00:00
|
|
|
const { data: team, isLoading } = trpc.viewer.teams.get.useQuery(
|
|
|
|
{ teamId },
|
|
|
|
{
|
|
|
|
onError: () => {
|
|
|
|
router.push("/settings");
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
2022-09-12 22:04:33 +00:00
|
|
|
|
2023-06-16 12:45:20 +00:00
|
|
|
const inviteMemberMutation = trpc.viewer.teams.inviteMember.useMutation();
|
2022-09-12 22:04:33 +00:00
|
|
|
|
|
|
|
const isInviteOpen = !team?.membership.accepted;
|
|
|
|
|
|
|
|
const isAdmin =
|
|
|
|
team && (team.membership.role === MembershipRole.OWNER || team.membership.role === MembershipRole.ADMIN);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2022-12-19 17:53:46 +00:00
|
|
|
<Meta
|
|
|
|
title={t("team_members")}
|
|
|
|
description={t("members_team_description")}
|
|
|
|
CTA={
|
|
|
|
isAdmin ? (
|
|
|
|
<Button
|
|
|
|
type="button"
|
|
|
|
color="primary"
|
2023-04-12 15:26:31 +00:00
|
|
|
StartIcon={Plus}
|
2022-12-19 17:53:46 +00:00
|
|
|
className="ml-auto"
|
|
|
|
onClick={() => setShowMemberInvitationModal(true)}
|
|
|
|
data-testid="new-member-button">
|
|
|
|
{t("add")}
|
|
|
|
</Button>
|
|
|
|
) : (
|
|
|
|
<></>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
/>
|
2022-09-12 22:04:33 +00:00
|
|
|
{!isLoading && (
|
|
|
|
<>
|
|
|
|
<div>
|
|
|
|
{team && (
|
|
|
|
<>
|
|
|
|
{isInviteOpen && (
|
|
|
|
<TeamInviteList
|
|
|
|
teams={[
|
|
|
|
{
|
|
|
|
id: team.id,
|
|
|
|
accepted: team.membership.accepted || false,
|
|
|
|
logo: team.logo,
|
|
|
|
name: team.name,
|
|
|
|
slug: team.slug,
|
|
|
|
role: team.membership.role,
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
)}
|
2023-05-05 09:35:27 +00:00
|
|
|
<MembersList team={team} />
|
2023-04-05 18:14:46 +00:00
|
|
|
<hr className="border-subtle my-8" />
|
2022-09-12 22:04:33 +00:00
|
|
|
|
|
|
|
{team && session.data && (
|
|
|
|
<DisableTeamImpersonation
|
|
|
|
teamId={team.id}
|
|
|
|
memberId={session.data.user.id}
|
|
|
|
disabled={isInviteOpen}
|
|
|
|
/>
|
|
|
|
)}
|
2023-04-05 18:14:46 +00:00
|
|
|
<hr className="border-subtle my-8" />
|
2022-09-12 22:04:33 +00:00
|
|
|
</div>
|
|
|
|
{showMemberInvitationModal && team && (
|
|
|
|
<MemberInvitationModal
|
|
|
|
isOpen={showMemberInvitationModal}
|
2022-11-10 20:23:56 +00:00
|
|
|
members={team.members}
|
2023-06-06 23:34:14 +00:00
|
|
|
teamId={team.id}
|
|
|
|
token={team.inviteToken?.token}
|
2022-09-12 22:04:33 +00:00
|
|
|
onExit={() => setShowMemberInvitationModal(false)}
|
2023-06-16 12:45:20 +00:00
|
|
|
onSubmit={(values, resetFields) => {
|
|
|
|
inviteMemberMutation.mutate(
|
|
|
|
{
|
|
|
|
teamId,
|
|
|
|
language: i18n.language,
|
|
|
|
role: values.role,
|
|
|
|
usernameOrEmail: values.emailOrUsername,
|
|
|
|
sendEmailInvitation: values.sendInviteEmail,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
onSuccess: async (data) => {
|
|
|
|
await utils.viewer.teams.get.invalidate();
|
|
|
|
setShowMemberInvitationModal(false);
|
|
|
|
if (data.sendEmailInvitation) {
|
|
|
|
if (Array.isArray(data.usernameOrEmail)) {
|
|
|
|
showToast(
|
|
|
|
t("email_invite_team_bulk", {
|
|
|
|
userCount: data.usernameOrEmail.length,
|
|
|
|
}),
|
|
|
|
"success"
|
|
|
|
);
|
|
|
|
resetFields();
|
|
|
|
} else {
|
|
|
|
showToast(
|
|
|
|
t("email_invite_team", {
|
|
|
|
email: data.usernameOrEmail,
|
|
|
|
}),
|
|
|
|
"success"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
onError: (error) => {
|
|
|
|
showToast(error.message, "error");
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
2022-11-10 20:23:56 +00:00
|
|
|
}}
|
2023-06-06 23:34:14 +00:00
|
|
|
onSettingsOpen={() => {
|
|
|
|
setShowMemberInvitationModal(false);
|
|
|
|
setInviteLinkSettingsModal(true);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
{showInviteLinkSettingsModal && team?.inviteToken && (
|
|
|
|
<InviteLinkSettingsModal
|
|
|
|
isOpen={showInviteLinkSettingsModal}
|
|
|
|
teamId={team.id}
|
|
|
|
token={team.inviteToken.token}
|
|
|
|
expiresInDays={team.inviteToken.expiresInDays || undefined}
|
|
|
|
onExit={() => {
|
|
|
|
setInviteLinkSettingsModal(false);
|
|
|
|
setShowMemberInvitationModal(true);
|
|
|
|
}}
|
2022-09-12 22:04:33 +00:00
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
MembersView.getLayout = getLayout;
|
|
|
|
|
|
|
|
export default MembersView;
|