import React, { useState, useEffect } from "react"; import Link from "next/link"; import { Typography } from "antd"; import { teamDeleteCall, teamUpdateCall } from "./networking"; import { InformationCircleIcon, PencilAltIcon, PencilIcon, StatusOnlineIcon, TrashIcon } from "@heroicons/react/outline"; import { Button as Button2, Modal, Form, Input, Select as Select2, InputNumber, message, } from "antd"; import { Select, SelectItem } from "@tremor/react"; import { Table, TableBody, TableCell, TableHead, TableHeaderCell, TableRow, Card, Icon, Button, Badge, Col, Text, Grid, } from "@tremor/react"; import { CogIcon } from "@heroicons/react/outline"; 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 } from "./networking"; const Team: React.FC = ({ teams, searchParams, accessToken, setTeams, userID, userRole, }) => { 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); 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} ))}
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); } }; fetchUserModels(); }, [accessToken, userID, userRole]); const handleCreate = async (formValues: Record) => { try { if (accessToken != null) { 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 handleMemberCreate = async (formValues: Record) => { try { if (accessToken != null && teams != null) { message.info("Adding Member"); const user_role: Member = { role: "user", user_email: formValues.user_email, user_id: formValues.user_id, }; const response: any = await teamMemberAddCall( accessToken, selectedTeam["team_id"], user_role ); 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); } }; console.log(`received teams ${teams}`); return (
All Teams Team Name Spend (USD) Budget (USD) Models TPM / RPM Limits {teams && teams.length > 0 ? teams.map((team: any) => ( {team["team_alias"]} {team["spend"]} {team["max_budget"] ? 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"}
handleEditClick(team)} /> handleDelete(team.team_id)} icon={TrashIcon} size="sm" />
)) : null}
{isDeleteModalOpen && (
{/* Modal Panel */} {/* Confirmation Modal Content */}

Delete Team

Are you sure you want to delete this team ?

)}
<> All Proxy Models {userModels.map((model) => ( {model} ))}
Create Team
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 && ( )}
<>
OR
Add member
); }; export default Team;