import { useState } from "react"; import { classNames } from "@calcom/lib"; import { useLocale } from "@calcom/lib/hooks/useLocale"; import { trpc } from "@calcom/trpc/react"; import { Badge, Icon, showToast, TextField } from "@calcom/ui"; interface GroupNameCellProps { groupNames: string[]; teamId: number; directoryId: string; } const GroupNameCell = ({ groupNames, teamId, directoryId }: GroupNameCellProps) => { const [showTextInput, setShowTextInput] = useState(false); const [newGroupName, setNewGroupName] = useState(""); const { t } = useLocale(); const utils = trpc.useUtils(); const createMutation = trpc.viewer.dsync.teamGroupMapping.create.useMutation({ onSuccess: (data) => { utils.viewer.dsync.teamGroupMapping.get.setData(undefined, (prev) => { if (prev) { const teamIndex = prev.teamGroupMapping.findIndex((team) => team.id === teamId); prev.teamGroupMapping[teamIndex].groupNames.push(data.newGroupName); } return prev; }); setShowTextInput(false); setNewGroupName(""); showToast(`Group added`, "success"); }, onError: (error) => { showToast(`Error adding group name${error.message}`, "error"); }, }); const deleteMutation = trpc.viewer.dsync.teamGroupMapping.delete.useMutation({ onSuccess: (data) => { utils.viewer.dsync.teamGroupMapping.get.setData(undefined, (prev) => { if (prev) { const teamIndex = prev.teamGroupMapping.findIndex((team) => team.id === teamId); const indexToRemove = prev.teamGroupMapping[teamIndex].groupNames.indexOf(data.deletedGroupName); prev.teamGroupMapping[teamIndex].groupNames.splice(indexToRemove, 1); } return prev; }); showToast(`Group removed`, "success"); }, onError: (error) => { showToast(`Error removing group name${error.message}`, "error"); }, }); const addGroupName = (groupName: string) => { if (groupNames.some((name: string) => name === groupName)) { showToast(`Group name already added`, "error"); return; } createMutation.mutate({ teamId: teamId, name: groupName, directoryId: directoryId }); }; const removeGroupName = (groupName: string) => { deleteMutation.mutate({ teamId: teamId, groupName: groupName, }); }; return (
{name}
{t("add_group_name")}
)}