import { VideoTranscodeStrategy } from '@shumai/dtos' import { client } from '@/ui/api/client' import { AgentsSettings } from '@/ui/components/settings/AgentsSettings' import { ProvidersSettings } from '@/ui/components/settings/ProvidersSettings' import { SandboxSettings } from '@/ui/components/settings/SandboxSettings' import { SkillsConfigCard } from '@/ui/components/settings/SkillsConfigCard' import { NotificationSettings } from '@/ui/components/settings/NotificationSettings' import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/ui/components/ui/card' import { cn } from '@/ui/lib/utils' import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query' import { createFileRoute } from '@tanstack/react-router' import { Bot, Cpu, Film, Loader2, Puzzle, Shield, User, Bell } from 'lucide-react' import { useState, useRef, useEffect } from 'react' import { Avatar, AvatarFallback, AvatarImage } from '@/ui/components/ui/avatar' import { Input } from '@/ui/components/ui/input' import { Button } from '@/ui/components/ui/button' import { AvatarCropDialog } from '@/ui/components/settings/AvatarCropDialog' import { toast } from 'sonner' type SettingsTab = | 'general' | 'transcode' | 'skills' | 'providers' | 'agents' | 'sandbox' | 'notifications' function TeamSettingsPage() { const { teamId } = Route.useParams() const queryClient = useQueryClient() const [activeTab, setActiveTab] = useState('general') const [profileName, setProfileName] = useState('') const [isCropOpen, setIsCropOpen] = useState(false) const [cropImageSrc, setCropImageSrc] = useState(null) const fileInputRef = useRef(null) const [isUpdatingProfile, setIsUpdatingProfile] = useState(false) const { data: settings, isLoading: isSettingsLoading, error: settingsError, } = useQuery({ queryKey: ['teams', teamId, 'settings'], queryFn: async () => { const res = await client.api.teams[':teamId'].settings.$get({ param: { teamId: teamId }, }) if (!res.ok) throw new Error('Failed to fetch settings') return await res.json() }, }) const { data: me, isLoading: isMeLoading } = useQuery({ queryKey: ['teams', teamId, 'me'], queryFn: async () => { const res = await client.api.teams[':teamId'].me.$get({ param: { teamId: teamId }, }) if (!res.ok) throw new Error('Failed to fetch me') return await res.json() }, enabled: !!teamId, }) useEffect(() => { if (me?.name) { setProfileName(me.name) } }, [me]) const getInitials = (name?: string) => { if (!name) return 'U' const names = name.split(' ') if (names.length > 1) { return `${names[0][0]}${names[names.length - 1][0]}`.toUpperCase() } return name.substring(0, 2).toUpperCase() } const handleAvatarFileChange = (e: React.ChangeEvent) => { const file = e.target.files?.[0] if (file) { const reader = new FileReader() reader.onload = () => { setCropImageSrc(reader.result as string) setIsCropOpen(true) } reader.readAsDataURL(file) e.target.value = '' } } const handleConfirmCrop = async (blob: Blob) => { setIsUpdatingProfile(true) try { const fileToUpload = new File([blob], 'avatar.jpg', { type: 'image/jpeg' }) const uploadRes = await client.api.teams[':teamId'].files.$post({ param: { teamId }, form: { file: fileToUpload }, }) if (!uploadRes.ok) throw new Error('Failed to upload avatar') const uploadData = await uploadRes.json() const key = (uploadData as { key: string }).key const patchRes = await client.api.teams[':teamId'].me.$patch({ param: { teamId }, json: { imageKey: key }, }) if (!patchRes.ok) throw new Error('Failed to save profile') toast.success('Avatar updated successfully') queryClient.invalidateQueries({ queryKey: ['teams', teamId, 'me'] }) setIsCropOpen(false) } catch (err) { console.error(err) toast.error('Failed to update avatar') } finally { setIsUpdatingProfile(false) } } const handleRemoveAvatar = async () => { setIsUpdatingProfile(true) try { const patchRes = await client.api.teams[':teamId'].me.$patch({ param: { teamId }, json: { imageKey: null }, }) if (!patchRes.ok) throw new Error('Failed to remove avatar') toast.success('Avatar removed successfully') queryClient.invalidateQueries({ queryKey: ['teams', teamId, 'me'] }) } catch (err) { console.error(err) toast.error('Failed to remove avatar') } finally { setIsUpdatingProfile(false) } } const handleUpdateProfile = async () => { if (!profileName.trim()) return setIsUpdatingProfile(true) try { const patchRes = await client.api.teams[':teamId'].me.$patch({ param: { teamId }, json: { name: profileName.trim() }, }) if (!patchRes.ok) throw new Error('Failed to update name') toast.success('Profile updated successfully') queryClient.invalidateQueries({ queryKey: ['teams', teamId, 'me'] }) } catch (err) { console.error(err) toast.error('Failed to update profile') } finally { setIsUpdatingProfile(false) } } const { mutate: updateSettings } = useMutation({ // eslint-disable-next-line @typescript-eslint/no-explicit-any mutationFn: async (data: { teamId: string; data: { key: string; value: any } }) => { const res = await client.api.teams[':teamId'].settings.$patch({ param: { teamId: data.teamId }, json: { key: data.data.key, value: data.data.value }, }) if (!res.ok) throw new Error('Failed to update settings') return await res.json() }, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['teams', teamId, 'settings'], }) }, }) const handleVideoStrategyChange = (value: VideoTranscodeStrategy) => { updateSettings({ teamId, data: { key: 'transcode.videoStrategy', value: value, }, }) } if (isSettingsLoading || isMeLoading) { return (
) } if (settingsError) { return
Failed to load settings.
} let currentVideoStrategy = // eslint-disable-next-line @typescript-eslint/no-explicit-any (settings as any)?.transcode?.videoStrategy || VideoTranscodeStrategy.best_match if (currentVideoStrategy === 'single' || currentVideoStrategy === 'disable') { currentVideoStrategy = VideoTranscodeStrategy.best_match } else if (currentVideoStrategy === 'full') { currentVideoStrategy = VideoTranscodeStrategy.all } return (
{/* Sidebar */}
{/* Main Content Area */}

{activeTab === 'general' && 'General Settings'} {activeTab === 'transcode' && 'Media Processing'} {activeTab === 'skills' && 'Skills Management'} {activeTab === 'providers' && 'AI Providers'} {activeTab === 'agents' && 'AI Agents'} {activeTab === 'sandbox' && 'Agent Sandbox Settings'} {activeTab === 'notifications' && 'Notification Settings'}

{activeTab === 'general' && 'View your personal information and team role.'} {activeTab === 'transcode' && "Manage your team's media transcoding configurations."} {activeTab === 'skills' && 'Add, update and configure AI skills for the team.'} {activeTab === 'providers' && 'Configure AI providers and their models for this team.'} {activeTab === 'agents' && 'Manage AI agents and their personalities.'} {activeTab === 'sandbox' && 'Configure security and network restrictions for the AI agent.'} {activeTab === 'notifications' && 'Configure your personal notification preferences for this team.'}

{activeTab === 'general' && (
Personal Info Manage your profile name and avatar image.
{/* Avatar Column */}
Avatar
fileInputRef.current?.click()} > {me?.image && ( )} {getInitials(me?.name)}
Change
{me?.image && ( )}
{/* Details Column */}
setProfileName(e.target.value)} placeholder="Enter your name" className="max-w-md" />

Email address is managed by authentication provider.

setIsCropOpen(false)} imageSrc={cropImageSrc} onConfirm={handleConfirmCrop} />
)} {activeTab === 'transcode' && (
Transcode Settings Manage your team's media transcoding configurations. Strategy: We select the best resolution from your list that supports the input quality. Content is never upscaled. {/* Video Strategy */}

Video Strategy

handleVideoStrategyChange(VideoTranscodeStrategy.best_match) } >
Best match
Generates a single optimal resolution matching the source quality.
handleVideoStrategyChange(VideoTranscodeStrategy.all)} >
All resolutions
Generates all supported resolutions up to the source quality.
)} {activeTab === 'skills' && (
)} {activeTab === 'providers' && } {activeTab === 'agents' && (
)} {activeTab === 'sandbox' && (
)} {activeTab === 'notifications' && }
) } export const Route = createFileRoute('/teams/$teamId/settings')({ component: TeamSettingsPage, })