import React, { useState, useEffect } from "react"; import { Form, Input, Select } from "antd"; import { Button, TextInput } from "@tremor/react"; import { KeyResponse } from "./key_team_helpers/key_list"; import { fetchTeamModels } from "../components/create_key_button"; import { modelAvailableCall } from "./networking"; import NumericalInput from "./shared/numerical_input"; interface KeyEditViewProps { keyData: KeyResponse; onCancel: () => void; onSubmit: (values: any) => Promise; teams?: any[] | null; accessToken: string | null; userID: string | null; userRole: string | null; } // Add this helper function const getAvailableModelsForKey = (keyData: KeyResponse, teams: any[] | null): string[] => { // If no teams data is available, return empty array console.log("getAvailableModelsForKey:", teams); if (!teams || !keyData.team_id) { return []; } // Find the team that matches the key's team_id const keyTeam = teams.find(team => team.team_id === keyData.team_id); // If team found and has models, return those models if (keyTeam?.models) { return keyTeam.models; } return []; }; export function KeyEditView({ keyData, onCancel, onSubmit, teams, accessToken, userID, userRole }: KeyEditViewProps) { const [form] = Form.useForm(); const [userModels, setUserModels] = useState([]); const team = teams?.find(team => team.team_id === keyData.team_id); const [availableModels, setAvailableModels] = useState([]); useEffect(() => { const fetchModels = async () => { if (!userID || !userRole || !accessToken) return; try { if (keyData.team_id === null) { // Fetch user models if no team const model_available = await modelAvailableCall( accessToken, userID, userRole ); const available_model_names = model_available["data"].map( (element: { id: string }) => element.id ); setAvailableModels(available_model_names); } else if (team?.team_id) { // Fetch team models if team exists const models = await fetchTeamModels(userID, userRole, accessToken, team.team_id); setAvailableModels(Array.from(new Set([...team.models, ...models]))); } } catch (error) { console.error("Error fetching models:", error); } }; fetchModels(); }, [userID, userRole, accessToken, team, keyData.team_id]); // Convert API budget duration to form format const getBudgetDuration = (duration: string | null) => { if (!duration) return null; const durationMap: Record = { "24h": "daily", "7d": "weekly", "30d": "monthly" }; return durationMap[duration] || null; }; // Set initial form values const initialValues = { ...keyData, budget_duration: getBudgetDuration(keyData.budget_duration), metadata: keyData.metadata ? JSON.stringify(keyData.metadata, null, 2) : "", guardrails: keyData.metadata?.guardrails || [] }; return (
); }