diff --git a/packages/webui/src/components/pages/settings-page.tsx b/packages/webui/src/components/pages/settings-page.tsx index 2ee74e48..79ea4c72 100644 --- a/packages/webui/src/components/pages/settings-page.tsx +++ b/packages/webui/src/components/pages/settings-page.tsx @@ -2,7 +2,7 @@ import { createContext, useContext, useId, useRef, useState, type ReactNode } fr import { motion } from 'motion/react'; import { Accessibility, AlertTriangle, Bell, Bug, Check, Clock, Code2, Download, ExternalLink, Github, Image as ImageIcon, - Info, KeyRound, Loader2, Monitor, Moon, Palette, PanelTop, Plus, RefreshCw, RotateCcw, ShieldCheck, + Info, KeyRound, Loader2, Monitor, Moon, Palette, PanelTop, Plus, RefreshCw, RotateCcw, Server, ShieldCheck, Sparkles, Star, Sun, Tag, Upload, Trash2, } from 'lucide-react'; import { Card, CardContent, CardHeader, CardTitle, CardDescription } from '@/components/ui/card'; @@ -41,12 +41,14 @@ import { useAppState } from '@/contexts/AppStateContext'; import { cn } from '@/lib/utils'; import { settingsRoute, type SettingsTab } from '@/router'; import { NotificationsPanel } from '@/components/settings/notifications-panel'; +import { SystemPanel } from '@/components/settings/system-panel'; const TABS: { key: SettingsTab; label: string; icon: typeof Sun }[] = [ { key: 'appearance', label: '外观', icon: Palette }, { key: 'data', label: '数据与格式', icon: RefreshCw }, { key: 'advanced', label: '高级', icon: Code2 }, { key: 'account', label: '账号安全', icon: ShieldCheck }, + { key: 'system', label: '服务', icon: Server }, { key: 'notifications', label: '通知', icon: Bell }, { key: 'about', label: '关于', icon: Info }, ]; @@ -78,6 +80,7 @@ export function SettingsPage() { {tab === 'data' && } {tab === 'advanced' && } {tab === 'account' && } + {tab === 'system' && } {tab === 'notifications' && } {tab === 'about' && } diff --git a/packages/webui/src/components/settings/system-panel.tsx b/packages/webui/src/components/settings/system-panel.tsx new file mode 100644 index 00000000..399fd484 --- /dev/null +++ b/packages/webui/src/components/settings/system-panel.tsx @@ -0,0 +1,205 @@ +// Settings → 服务 tab (Wave A1). Edits the WebUI listener's own config +// (port / bind host / trust-proxy / HTTPS). Listener-level changes are +// persisted but apply only after a restart — there is no supervisor to +// self-restart, so we never offer a "restart now" button, only a banner. +// Fields currently pinned by SNOWLUMA_* env vars are flagged (edits to them +// won't take effect until the env var is removed). +import { useEffect, useState } from 'react'; +import { motion } from 'motion/react'; +import { AlertTriangle, Loader2, Lock, Save, ShieldCheck, Trash2 } from 'lucide-react'; +import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'; +import { Badge } from '@/components/ui/badge'; +import { Button } from '@/components/ui/button'; +import { Input } from '@/components/ui/input'; +import { Label } from '@/components/ui/label'; +import { ToggleSwitch } from '@/components/ui/toggle-switch'; +import { useApi } from '@/lib/api'; +import { cn } from '@/lib/utils'; +import type { SystemSettingsResponse } from '@/types'; + +const TEXTAREA_CLS = + 'w-full min-h-28 rounded-lg border border-border bg-background px-3 py-2 font-mono text-xs ' + + 'outline-none focus-visible:ring-2 focus-visible:ring-ring'; + +export function SystemPanel() { + const api = useApi(); + const [data, setData] = useState(null); + const [loading, setLoading] = useState(true); + const [saving, setSaving] = useState(false); + const [msg, setMsg] = useState<{ kind: 'ok' | 'err'; text: string } | null>(null); + + // editable form state + const [port, setPort] = useState(''); + const [host, setHost] = useState('0.0.0.0'); + const [trustProxy, setTrustProxy] = useState(''); + const [tlsEnabled, setTlsEnabled] = useState(false); + const [certPem, setCertPem] = useState(''); + const [keyPem, setKeyPem] = useState(''); + + const load = async () => { + try { + const res = await api.systemSettings.get(); + setData(res); + setPort(String(res.settings.webuiPort)); + setHost(res.settings.webuiHost); + setTrustProxy(res.settings.trustProxy); + setTlsEnabled(res.settings.tlsEnabled); + } catch { + setMsg({ kind: 'err', text: '加载系统设置失败' }); + } finally { + setLoading(false); + } + }; + + useEffect(() => { void load(); }, []); // eslint-disable-line react-hooks/exhaustive-deps + + const overridden = (field: string) => data?.envOverrides.includes(field) ?? false; + const flash = (kind: 'ok' | 'err', text: string) => { + setMsg({ kind, text }); + window.setTimeout(() => setMsg(null), 4000); + }; + + const saveSettings = async () => { + const portNum = Number(port); + if (!Number.isInteger(portNum) || portNum <= 0 || portNum > 65535) { + flash('err', '端口必须是 1–65535 的整数'); + return; + } + setSaving(true); + try { + await api.systemSettings.save({ webuiPort: portNum, webuiHost: host.trim(), trustProxy, tlsEnabled }); + await load(); + flash('ok', '已保存,重启 SnowLuma 后生效'); + } catch (e) { + flash('err', e instanceof Error ? e.message : '保存失败'); + } finally { + setSaving(false); + } + }; + + const uploadCert = async () => { + if (!certPem.trim() || !keyPem.trim()) { flash('err', '请同时填入证书与私钥'); return; } + setSaving(true); + try { + await api.systemSettings.uploadCert(certPem, keyPem); + setCertPem(''); setKeyPem(''); + await load(); + flash('ok', '证书已保存,重启后生效'); + } catch (e) { + flash('err', e instanceof Error ? e.message : '证书保存失败'); + } finally { + setSaving(false); + } + }; + + const deleteCert = async () => { + setSaving(true); + try { + await api.systemSettings.deleteCert(); + await load(); + flash('ok', '证书已删除'); + } catch (e) { + flash('err', e instanceof Error ? e.message : '删除失败'); + } finally { + setSaving(false); + } + }; + + if (loading) { + return
加载中…
; + } + + const EnvBadge = ({ field }: { field: string }) => + overridden(field) + ? 被环境变量覆盖 + : null; + + return ( + + {/* restart-to-apply notice */} +
+ + 监听相关设置(端口 / 绑定地址 / HTTPS)属服务级配置,保存后需重启 SnowLuma 才生效;本机无自动重启。 +
+ + {msg && ( +
{msg.text}
+ )} + + + + 服务监听 + WebUI 当前监听端口:{data?.listeningPort} + + +
+ + setPort(e.target.value)} inputMode="numeric" placeholder="5099" /> +
+
+ + +
+
+ + setTrustProxy(e.target.value)} placeholder="留空=只信任 socket 对端;1=信任反代头" /> +

仅在 WebUI 位于受信任反向代理之后时才设为 1 / loopback / IP 列表。

+
+
+ +
+
+
+ + + + + HTTPS / TLS + + + {data?.hasCert ? '已安装证书。' : '尚未安装证书。'} 启用 TLS 需先安装有效的证书与私钥(PEM)。 + + + +
+ + +
+ +
+ +