import React, { useState, useEffect } from "react"; import Link from "next/link"; import { Typography } from "antd"; import { teamDeleteCall, teamUpdateCall, teamInfoCall } from "./networking"; import { InformationCircleIcon, PencilAltIcon, PencilIcon, StatusOnlineIcon, TrashIcon, } from "@heroicons/react/outline"; import { Button as Button2, Modal, Form, Input, Select as Select2, InputNumber, message, Tooltip } from "antd"; import { Select, SelectItem } from "@tremor/react"; import { Table, TableBody, TableCell, TableHead, TableHeaderCell, TableRow, TextInput, Card, Icon, Button, Badge, Col, Text, Grid, } from "@tremor/react"; import { CogIcon } from "@heroicons/react/outline"; const isLocal = process.env.NODE_ENV === "development"; const proxyBaseUrl = isLocal ? "http://localhost:4000" : null; if (isLocal != true) { console.log = function() {}; } interface TeamProps { teams: any[] | null; searchParams: any; accessToken: string | null; setTeams: React.Dispatch>; userID: string | null; userRole: string | null; } interface EditTeamModalProps { visible: boolean; onCancel: () => void; team: any; // Assuming TeamType is a type representing your team object onSubmit: (data: FormData) => void; // Assuming FormData is the type of data to be submitted } import { teamCreateCall, teamMemberAddCall, Member, modelAvailableCall, teamListCall } from "./networking"; const Team: React.FC = ({ teams, searchParams, accessToken, setTeams, userID, userRole, }) => { useEffect(() => { console.log(`inside useeffect - ${teams}`) if (teams === null && accessToken) { // Call your function here const fetchData = async () => { let givenTeams; if (userRole != "Admin" && userRole != "Admin Viewer") { givenTeams = await teamListCall(accessToken, userID) } else { givenTeams = await teamListCall(accessToken) } console.log(`givenTeams: ${givenTeams}`) setTeams(givenTeams) } fetchData() } }, [teams]); const [form] = Form.useForm(); const [memberForm] = Form.useForm(); const { Title, Paragraph } = Typography; const [value, setValue] = useState(""); const [editModalVisible, setEditModalVisible] = useState(false); const [selectedTeam, setSelectedTeam] = useState( teams ? teams[0] : null ); const [isTeamModalVisible, setIsTeamModalVisible] = useState(false); const [isAddMemberModalVisible, setIsAddMemberModalVisible] = useState(false); const [userModels, setUserModels] = useState([]); const [isDeleteModalOpen, setIsDeleteModalOpen] = useState(false); const [teamToDelete, setTeamToDelete] = useState(null); // store team info as {"team_id": team_info_object} const [perTeamInfo, setPerTeamInfo] = useState>({}); const EditTeamModal: React.FC = ({ visible, onCancel, team, onSubmit, }) => { const [form] = Form.useForm(); const handleOk = () => { form .validateFields() .then((values) => { const updatedValues = { ...values, team_id: team.team_id }; onSubmit(updatedValues); form.resetFields(); }) .catch((error) => { console.error("Validation failed:", error); }); }; return (
<> {"All Proxy Models"} {userModels && userModels.map((model) => ( {model} ))} daily weekly monthly
Edit Team
); }; const handleEditClick = (team: any) => { setSelectedTeam(team); setEditModalVisible(true); }; const handleEditCancel = () => { setEditModalVisible(false); setSelectedTeam(null); }; const handleEditSubmit = async (formValues: Record) => { // Call API to update team with teamId and values const teamId = formValues.team_id; // get team_id console.log("handleEditSubmit:", formValues); if (accessToken == null) { return; } let newTeamValues = await teamUpdateCall(accessToken, formValues); // Update the teams state with the updated team data if (teams) { const updatedTeams = teams.map((team) => team.team_id === teamId ? newTeamValues.data : team ); setTeams(updatedTeams); } message.success("Team updated successfully"); setEditModalVisible(false); setSelectedTeam(null); }; const handleOk = () => { setIsTeamModalVisible(false); form.resetFields(); }; const handleMemberOk = () => { setIsAddMemberModalVisible(false); memberForm.resetFields(); }; const handleCancel = () => { setIsTeamModalVisible(false); form.resetFields(); }; const handleMemberCancel = () => { setIsAddMemberModalVisible(false); memberForm.resetFields(); }; const handleDelete = async (team_id: string) => { // Set the team to delete and open the confirmation modal setTeamToDelete(team_id); setIsDeleteModalOpen(true); }; const confirmDelete = async () => { if (teamToDelete == null || teams == null || accessToken == null) { return; } try { await teamDeleteCall(accessToken, teamToDelete); // Successfully completed the deletion. Update the state to trigger a rerender. const filteredData = teams.filter( (item) => item.team_id !== teamToDelete ); setTeams(filteredData); } catch (error) { console.error("Error deleting the team:", error); // Handle any error situations, such as displaying an error message to the user. } // Close the confirmation modal and reset the teamToDelete setIsDeleteModalOpen(false); setTeamToDelete(null); }; const cancelDelete = () => { // Close the confirmation modal and reset the teamToDelete setIsDeleteModalOpen(false); setTeamToDelete(null); }; useEffect(() => { const fetchUserModels = async () => { try { if (userID === null || userRole === null) { return; } if (accessToken !== null) { const model_available = await modelAvailableCall( accessToken, userID, userRole ); let available_model_names = model_available["data"].map( (element: { id: string }) => element.id ); console.log("available_model_names:", available_model_names); setUserModels(available_model_names); } } catch (error) { console.error("Error fetching user models:", error); } }; const fetchTeamInfo = async () => { try { if (userID === null || userRole === null || accessToken === null) { return; } if (teams === null) { return; } let _team_id_to_info: Record = {}; let teamList; if (userRole != "Admin" && userRole != "Admin Viewer") { teamList = await teamListCall(accessToken, userID) } else { teamList = await teamListCall(accessToken) } for (let i = 0; i < teamList.length; i++) { let team = teamList[i]; let _team_id = team.team_id; // Use the team info directly from the teamList if (team !== null) { _team_id_to_info = { ..._team_id_to_info, [_team_id]: team }; } } setPerTeamInfo(_team_id_to_info); } catch (error) { console.error("Error fetching team info:", error); } }; fetchUserModels(); fetchTeamInfo(); }, [accessToken, userID, userRole, teams]); const handleCreate = async (formValues: Record) => { try { if (accessToken != null) { const newTeamAlias = formValues?.team_alias; const existingTeamAliases = teams?.map((t) => t.team_alias) ?? []; if (existingTeamAliases.includes(newTeamAlias)) { throw new Error( `Team alias ${newTeamAlias} already exists, please pick another alias` ); } message.info("Creating Team"); const response: any = await teamCreateCall(accessToken, formValues); if (teams !== null) { setTeams([...teams, response]); } else { setTeams([response]); } console.log(`response for team create call: ${response}`); message.success("Team created"); setIsTeamModalVisible(false); } } catch (error) { console.error("Error creating the team:", error); message.error("Error creating the team: " + error, 20); } }; const is_team_admin = (team: any) => { for (let i = 0; i < team.members_with_roles.length; i++) { let member = team.members_with_roles[i]; if (member.user_id == userID && member.role == "admin") { return true; } } return false; } const handleMemberCreate = async (formValues: Record) => { try { if (accessToken != null && teams != null) { message.info("Adding Member"); const user_role: Member = { role: formValues.role, user_email: formValues.user_email, user_id: formValues.user_id, }; const response: any = await teamMemberAddCall( accessToken, selectedTeam["team_id"], user_role ); message.success("Member added"); console.log(`response for team create call: ${response["data"]}`); // Checking if the team exists in the list and updating or adding accordingly const foundIndex = teams.findIndex((team) => { console.log( `team.team_id=${team.team_id}; response.data.team_id=${response.data.team_id}` ); return team.team_id === response.data.team_id; }); console.log(`foundIndex: ${foundIndex}`); if (foundIndex !== -1) { // If the team is found, update it const updatedTeams = [...teams]; // Copy the current state updatedTeams[foundIndex] = response.data; // Update the specific team setTeams(updatedTeams); // Set the new state setSelectedTeam(response.data); } setIsAddMemberModalVisible(false); } } catch (error) { console.error("Error creating the team:", error); } }; return (
All Teams Team Name Team ID Spend (USD) Budget (USD) Models TPM / RPM Limits Info {teams && teams.length > 0 ? teams.map((team: any) => ( {team["team_alias"]} {team.team_id} {team["spend"]} {team["max_budget"] !== null && team["max_budget"] !== undefined ? team["max_budget"] : "No limit"} {Array.isArray(team.models) ? (
{team.models.length === 0 ? ( All Proxy Models ) : ( team.models.map( (model: string, index: number) => model === "all-proxy-models" ? ( All Proxy Models ) : ( {model.length > 30 ? `${model.slice(0, 30)}...` : model} ) ) )}
) : null}
TPM: {team.tpm_limit ? team.tpm_limit : "Unlimited"}{" "}

RPM:{" "} {team.rpm_limit ? team.rpm_limit : "Unlimited"}
{perTeamInfo && team.team_id && perTeamInfo[team.team_id] && perTeamInfo[team.team_id].keys && perTeamInfo[team.team_id].keys.length}{" "} Keys {perTeamInfo && team.team_id && perTeamInfo[team.team_id] && perTeamInfo[team.team_id].members_with_roles && perTeamInfo[team.team_id].members_with_roles.length}{" "} Members {userRole == "Admin" ? ( <> handleEditClick(team)} /> handleDelete(team.team_id)} icon={TrashIcon} size="sm" /> ) : null}
)) : null}
{isDeleteModalOpen && (
{/* Modal Panel */} {/* Confirmation Modal Content */}

Delete Team

Are you sure you want to delete this team ?

)}
{userRole == "Admin"? (
<> All Proxy Models {userModels.map((model) => ( {model} ))} daily weekly monthly
Create Team
) : null} Team Members If you belong to multiple teams, this setting controls which teams members you see. {teams && teams.length > 0 ? ( ) : ( No team created. Defaulting to personal account. )} Member Name Role {selectedTeam ? selectedTeam["members_with_roles"].map( (member: any, index: number) => ( {member["user_email"] ? member["user_email"] : member["user_id"] ? member["user_id"] : null} {member["role"]} ) ) : null}
{selectedTeam && ( )} {userRole == "Admin" || (selectedTeam && is_team_admin(selectedTeam)) ? ( ) : null}
<>
OR
admin user
Add member
); }; export default Team;