import { useEffect, useState } from 'react' import { toast } from 'sonner' import { Button } from '@/components/ui/button' import { Input } from '@/components/ui/input' import { Label } from '@/components/ui/label' import { Switch } from '@/components/ui/switch' import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card' import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '@/components/ui/select' import { Separator } from '@/components/ui/separator' import { Trash2, Plus } from 'lucide-react' import api from '@/api/client' import { useBrand } from '@/brand/BrandContext' import { PageHeader } from '@/components/page-header' type Quality = { label: string; width: number; height: number; bitrate: string } type Profiles = Record const STRATEGIES = [ { value: 'subnet', label: '子网优先(推荐)' }, { value: 'weighted', label: '权重均衡' }, { value: 'round-robin', label: '轮询' }, { value: 'least-conn', label: '最少连接' }, ] export default function SettingsPage() { const { brand, refresh } = useBrand() const [strategy, setStrategy] = useState('subnet') const [multicastEnabled, setMulticastEnabled] = useState(true) const [multicastAvailable, setMulticastAvailable] = useState(false) const [brandName, setBrandName] = useState(brand.brand_name) const [brandTagline, setBrandTagline] = useState(brand.brand_tagline) const [brandLogoUrl, setBrandLogoUrl] = useState(brand.brand_logo_url) const [profiles, setProfiles] = useState({}) const [saving, setSaving] = useState(false) useEffect(() => { void Promise.all([api.get('/nodes/strategy'), api.get('/settings/transcode-profiles')]) .then(([nodes, transcode]) => { setStrategy(nodes.data.strategy) setMulticastEnabled(nodes.data.multicast_enabled) setMulticastAvailable(nodes.data.multicast_available) setProfiles(transcode.data.profiles || {}) }) .catch(() => toast.error('系统设置加载失败')) }, []) useEffect(() => { setBrandName(brand.brand_name) setBrandTagline(brand.brand_tagline) setBrandLogoUrl(brand.brand_logo_url) }, [brand]) function updateQuality(profile: string, index: number, patch: Partial) { setProfiles((current) => ({ ...current, [profile]: current[profile].map((quality, i) => (i === index ? { ...quality, ...patch } : quality)), })) } function addQuality(profile: string) { setProfiles((current) => ({ ...current, [profile]: [...current[profile], { label: '新清晰度', width: 1280, height: 720, bitrate: '2000k' }], })) } function removeQuality(profile: string, index: number) { setProfiles((current) => ({ ...current, [profile]: current[profile].filter((_, i) => i !== index), })) } async function saveAll() { setSaving(true) try { await Promise.all([ api.put('/nodes/strategy', { strategy, multicast_enabled: multicastEnabled }), api.put('/settings/brand', { brand_name: brandName, brand_tagline: brandTagline, brand_logo_url: brandLogoUrl }), api.put('/settings/transcode-profiles', { profiles }), ]) await refresh() toast.success('设置已保存') } catch { toast.error('保存失败,请检查转码参数') } finally { setSaving(false) } } return (
负载均衡

{multicastAvailable ? '可用' : '不可用,将回退至单播'}

自定义转码配置 {Object.entries(profiles).map(([profile, qualities]) => (
{profile}
{qualities.map((quality, index) => (
updateQuality(profile, index, { label: e.target.value })} className="w-32" /> updateQuality(profile, index, { width: Number(e.target.value || 160) })} className="w-24" /> updateQuality(profile, index, { height: Number(e.target.value || 90) })} className="w-24" /> updateQuality(profile, index, { bitrate: e.target.value })} className="w-28" />
))}
))}
品牌设置
setBrandName(e.target.value)} />
setBrandTagline(e.target.value)} />
setBrandLogoUrl(e.target.value)} />
) }