import React, { useState, useEffect } from "react"; import { Card, Title, Text, Divider, Button, TextInput } from "@tremor/react"; import { Typography, Spin, message, Switch, Select, Form } from "antd"; import { getDefaultTeamSettings, updateDefaultTeamSettings, modelAvailableCall } from "./networking"; import BudgetDurationDropdown, { getBudgetDurationLabel } from "./common_components/budget_duration_dropdown"; import { getModelDisplayName } from "./key_team_helpers/fetch_available_models_team_key"; interface TeamSSOSettingsProps { accessToken: string | null; userID: string; userRole: string; } const TeamSSOSettings: React.FC = ({ accessToken, userID, userRole }) => { const [loading, setLoading] = useState(true); const [settings, setSettings] = useState(null); const [isEditing, setIsEditing] = useState(false); const [editedValues, setEditedValues] = useState({}); const [saving, setSaving] = useState(false); const [availableModels, setAvailableModels] = useState([]); const { Paragraph } = Typography; const { Option } = Select; useEffect(() => { const fetchTeamSSOSettings = async () => { if (!accessToken) { setLoading(false); return; } try { const data = await getDefaultTeamSettings(accessToken); setSettings(data); setEditedValues(data.values || {}); // Fetch available models if (accessToken) { try { const modelResponse = await modelAvailableCall(accessToken, userID, userRole); if (modelResponse && modelResponse.data) { const modelNames = modelResponse.data.map((model: { id: string }) => model.id); setAvailableModels(modelNames); } } catch (error) { console.error("Error fetching available models:", error); } } } catch (error) { console.error("Error fetching team SSO settings:", error); message.error("Failed to fetch team settings"); } finally { setLoading(false); } }; fetchTeamSSOSettings(); }, [accessToken]); const handleSaveSettings = async () => { if (!accessToken) return; setSaving(true); try { const updatedSettings = await updateDefaultTeamSettings(accessToken, editedValues); setSettings({...settings, values: updatedSettings.settings}); setIsEditing(false); message.success("Default team settings updated successfully"); } catch (error) { console.error("Error updating team settings:", error); message.error("Failed to update team settings"); } finally { setSaving(false); } }; const handleTextInputChange = (key: string, value: any) => { setEditedValues((prev: Record) => ({ ...prev, [key]: value })); }; const renderEditableField = (key: string, property: any, value: any) => { const type = property.type; if (key === "budget_duration") { return ( handleTextInputChange(key, value)} className="mt-2" /> ); } else if (type === "boolean") { return (
handleTextInputChange(key, checked)} />
); } else if (type === "array" && property.items?.enum) { return ( ); } else if (key === "models") { return ( ); } else if (type === "string" && property.enum) { return ( ); } else { return ( handleTextInputChange(key, e.target.value)} placeholder={property.description || ""} className="mt-2" /> ); } }; const renderValue = (key: string, value: any): JSX.Element => { if (value === null || value === undefined) return Not set; if (key === "budget_duration") { return {getBudgetDurationLabel(value)}; } if (typeof value === "boolean") { return {value ? "Enabled" : "Disabled"}; } if (key === "models" && Array.isArray(value)) { if (value.length === 0) return None; return (
{value.map((model, index) => ( {getModelDisplayName(model)} ))}
); } if (typeof value === "object") { if (Array.isArray(value)) { if (value.length === 0) return None; return (
{value.map((item, index) => ( {typeof item === "object" ? JSON.stringify(item) : String(item)} ))}
); } return (
          {JSON.stringify(value, null, 2)}
        
); } return {String(value)}; }; if (loading) { return (
); } if (!settings) { return ( No team settings available or you do not have permission to view them. ); } // Dynamically render settings based on the schema const renderSettings = () => { const { values, schema } = settings; if (!schema || !schema.properties) { return No schema information available; } return Object.entries(schema.properties).map(([key, property]: [string, any]) => { const value = values[key]; const displayName = key.replace(/_/g, ' ').replace(/\b\w/g, l => l.toUpperCase()); return (
{displayName} {property.description || "No description available"} {isEditing ? (
{renderEditableField(key, property, value)}
) : (
{renderValue(key, value)}
)}
); }); }; return (
Default Team Settings {!loading && settings && ( isEditing ? (
) : ( ) )}
These settings will be applied by default when creating new teams. {settings?.schema?.description && ( {settings.schema.description} )}
{renderSettings()}
); }; export default TeamSSOSettings;