diff --git a/packages/webui/src/components/pages/settings-page.tsx b/packages/webui/src/components/pages/settings-page.tsx index 28d8ea8d..0dee1940 100644 --- a/packages/webui/src/components/pages/settings-page.tsx +++ b/packages/webui/src/components/pages/settings-page.tsx @@ -1,4 +1,4 @@ -import { useRef, useState, type ReactNode } from 'react'; +import { createContext, useContext, useId, useRef, useState, type ReactNode } from 'react'; import { motion } from 'motion/react'; import { Accessibility, Bug, Check, Clock, Code2, Download, ExternalLink, Github, Image as ImageIcon, @@ -7,8 +7,6 @@ import { } from 'lucide-react'; import { Card, CardContent, CardHeader, CardTitle, CardDescription } from '@/components/ui/card'; import { Button } from '@/components/ui/button'; -import { Label } from '@/components/ui/label'; -import { Separator } from '@/components/ui/separator'; import { ToggleSwitch } from '@/components/ui/toggle-switch'; import { ACCENTS, @@ -48,16 +46,491 @@ const TABS: { key: SettingsTab; label: string; icon: typeof Sun }[] = [ export function SettingsPage() { const [tab, setTab] = useState('appearance'); + const active = TABS.find((t) => t.key === tab); + + return ( +
+ + +
+ +

{active?.label}

+ {tab === 'appearance' && } + {tab === 'data' && } + {tab === 'advanced' && } + {tab === 'account' && } + {tab === 'about' && } +
+
+
+ ); +} + +// ─────────────── nav: sticky rail (desktop) / scroll strip (mobile) ─────────────── + +function SettingsNav({ tab, onChange }: { tab: SettingsTab; onChange: (t: SettingsTab) => void }) { + return ( + + ); +} + +// ─────────────── shared little controls ─────────────── + +interface Opt { value: T; label: string; icon?: typeof Sun } + +// Associates a SettingRow's visible label with the control inside it (so the +// segmented radiogroup gets an accessible name without prop-threading). +const RowLabelContext = createContext(undefined); + +// iOS/macOS segmented control: a recessed track with a raised pill on the +// selected segment. Single-select → exposed as a radiogroup. Wraps gracefully. +function Segmented({ + value, options, onChange, +}: { value: T; options: Opt[]; onChange: (v: T) => void }) { + const labelledBy = useContext(RowLabelContext); + return ( +
+ {options.map((opt) => { + const active = value === opt.value; + const Icon = opt.icon; + return ( + + ); + })} +
+ ); +} + +/** + * A grouped-list row. `layout`: + * - 'auto' — control sits right of the label on sm+, stacks under it on mobile (short controls) + * - 'inline' — control always pinned right (switches) + * - 'stack' — control always full-width under the label (color grids, sliders, wide segments) + */ +function SettingRow({ + label, hint, children, layout = 'auto', +}: { label: string; hint?: ReactNode; children: ReactNode; layout?: 'auto' | 'inline' | 'stack' }) { + const labelId = useId(); + const head = ( +
+

{label}

+ {hint &&

{hint}

} +
+ ); + const control = {children}; + if (layout === 'stack') { + return
{head}{control}
; + } + if (layout === 'inline') { + return ( +
+ {head} +
{control}
+
+ ); + } + return ( +
+ {head} +
{control}
+
+ ); +} + +// A settings group: a card whose header sits above a hairline-divided list of rows. +function Group({ + title, description, icon: Icon, children, +}: { title: string; description?: string; icon?: typeof Sun; children: ReactNode }) { + return ( + + + + {Icon && } + {title} + + {description && {description}} + +
{children}
+
+ ); +} + +// ─────────────── 外观 ─────────────── + +const MODE_OPTIONS: Opt[] = [ + { value: 'light', label: '浅色', icon: Sun }, + { value: 'dark', label: '深色', icon: Moon }, + { value: 'system', label: '跟随系统', icon: Monitor }, +]; + +const DARK_INTENSITY_OPTIONS: Opt[] = [ + { value: 'soft', label: '柔和' }, + { value: 'black', label: '纯黑 (OLED)' }, +]; + +const SCOPE_OPTIONS: Opt[] = [ + { value: 'global', label: '全局' }, + { value: 'sidebar', label: '仅侧栏' }, +]; + +const SIDEBAR_STYLE_OPTIONS: Opt[] = [ + { value: 'follow', label: '跟随背景' }, + { value: 'panel', label: '浅色面板' }, + { value: 'accent', label: '强调色' }, +]; + +const BG_TYPE_OPTIONS: Opt[] = [ + { value: 'none', label: '无' }, + { value: 'solid', label: '纯色' }, + { value: 'gradient', label: '渐变' }, + { value: 'image', label: '图片' }, +]; + +const DENSITY_OPTIONS: Opt[] = [ + { value: 'cozy', label: '舒适' }, + { value: 'compact', label: '紧凑' }, +]; + +function AppearancePanel() { + const { appearance, setAppearance, uploadBackground, removeBackground } = useTheme(); + const a = appearance; return (
- + + + setAppearance({ mode })} /> + + + setAppearance({ darkIntensity })} /> + + - {tab === 'appearance' && } - {tab === 'data' && } - {tab === 'advanced' && } - {tab === 'account' && } - {tab === 'about' && } + + +
+ {ACCENTS.map((spec) => { + const active = a.accentMode === 'preset' && a.accentPreset === spec.id; + return ( + + ); + })} + +
+
+ + setAppearance({ accentScope })} /> + + + setAppearance({ sidebarStyle })} /> + +
+ + + + + + ({ value: f.id, label: f.label }))} onChange={(fontSans) => setAppearance({ fontSans })} /> + + + ({ value: f.id, label: f.label }))} onChange={(fontMono) => setAppearance({ fontMono })} /> + + + ({ value: r.value, label: r.label }))} onChange={(radius) => setAppearance({ radius })} /> + + +
+ {Math.round(UI_SCALE.min * 100)}% + setAppearance({ uiScale: Number(e.target.value) })} + className="h-2 flex-1 cursor-pointer accent-primary" + aria-label="界面缩放" + /> + {Math.round(UI_SCALE.max * 100)}% +
+
+ + setAppearance({ density })} /> + +
+ + + + setAppearance({ reduceMotion })} ariaLabel="减少动效" /> + + + setAppearance({ highContrast })} ariaLabel="高对比模式" /> + + + setAppearance({ sidebarDefaultCollapsed })} ariaLabel="侧栏默认折叠" /> + + +
+ ); +} + +function BackgroundCard({ + appearance: a, setAppearance, uploadBackground, removeBackground, +}: { + appearance: ReturnType['appearance']; + setAppearance: ReturnType['setAppearance']; + uploadBackground: ReturnType['uploadBackground']; + removeBackground: ReturnType['removeBackground']; +}) { + const fileRef = useRef(null); + const [uploading, setUploading] = useState(false); + const [busy, setBusy] = useState(false); + const [err, setErr] = useState(null); + + const onFile = async (e: React.ChangeEvent) => { + const file = e.target.files?.[0]; + if (fileRef.current) fileRef.current.value = ''; + if (!file) return; + setUploading(true); + setErr(null); + try { + await uploadBackground(file); + } catch (ex) { + setErr(ex instanceof Error ? ex.message : '上传失败'); + } finally { + setUploading(false); + } + }; + + const onRemove = async () => { + setBusy(true); + setErr(null); + try { + await removeBackground(); + } catch { + setErr('删除失败'); + } finally { + setBusy(false); + } + }; + + return ( + + + setAppearance({ background: { type } })} /> + + + {a.background.type === 'solid' && ( + +
+ + {a.background.color} +
+
+ )} + + {a.background.type === 'gradient' && ( + +
+ {GRADIENT_OPTIONS.map((g) => { + const active = a.background.gradient === g.id; + return ( +
+
+ )} + + {a.background.type === 'image' && ( + <> + +
+ + + {a.background.hasImage && ( + + )} +
+ {a.background.hasImage && ( +
+ 背景预览 +
+ )} +
+ + {a.background.hasImage && ( + <> + + setAppearance({ background: { imageOpacity: Number(e.target.value) } })} + className="h-2 w-full cursor-pointer accent-primary" + aria-label="遮罩强度" + /> + + + setAppearance({ background: { imageBlur: Number(e.target.value) } })} + className="h-2 w-full cursor-pointer accent-primary" + aria-label="背景模糊" + /> + + + )} + + )} + + {err &&

{err}

} +
+ ); +} + +// ─────────────── 数据与格式 ─────────────── + +const TIME_FORMAT_OPTIONS: Opt[] = [ + { value: '24h', label: '24 小时制' }, + { value: '12h', label: '12 小时制 (AM/PM)' }, +]; + +function DataPanel() { + const { appearance, setAppearance } = useTheme(); + const { pages, setPages } = useLayout(); + return ( +
+ + + ({ value: o.value, label: o.label }))} + onChange={(pollInterval) => setAppearance({ pollInterval })} + /> + + + + + + ({ value: n.to as string, label: n.label }))} + onChange={(defaultRoute) => setPages({ defaultRoute })} + /> + + + + + + setAppearance({ timeFormat })} /> + +
); } @@ -124,9 +597,9 @@ function AdvancedPanel() { return (
- - 自定义 CSS - + + 自定义 CSS + 高级用户可注入自定义样式,登录后全局生效(登录页不受影响)。 若改坏了界面,在地址后加 ?safe-mode=1 可临时禁用自定义 CSS 进来修复。 @@ -156,9 +629,9 @@ function AdvancedPanel() { - - 备份与重置 - 导出 / 导入全部界面配置(外观 + 布局 + 自定义 CSS)。背景图片需另行重新上传。 + + 备份与重置 + 导出 / 导入全部界面配置(外观 + 布局 + 自定义 CSS)。背景图片需另行重新上传。
@@ -191,510 +664,6 @@ function AdvancedPanel() { ); } -// ─────────────── mini tab strip ─────────────── - -function MiniTabs({ tab, onChange }: { tab: SettingsTab; onChange: (t: SettingsTab) => void }) { - return ( -
- {TABS.map((t) => { - const Icon = t.icon; - const active = tab === t.key; - return ( - - ); - })} -
- ); -} - -// ─────────────── shared little controls ─────────────── - -interface Opt { value: T; label: string; icon?: typeof Sun } - -function Segmented({ - value, options, onChange, -}: { value: T; options: Opt[]; onChange: (v: T) => void }) { - return ( -
- {options.map((opt) => { - const active = value === opt.value; - const Icon = opt.icon; - return ( - - ); - })} -
- ); -} - -function Field({ label, hint, children }: { label: string; hint?: string; children: ReactNode }) { - return ( -
-
- - {hint &&

{hint}

} -
- {children} -
- ); -} - -function ToggleRow({ label, hint, value, onChange }: { label: string; hint?: string; value: boolean; onChange: (v: boolean) => void }) { - return ( -
-
-

{label}

- {hint &&

{hint}

} -
- -
- ); -} - -// ─────────────── 外观 ─────────────── - -const MODE_OPTIONS: Opt[] = [ - { value: 'light', label: '浅色', icon: Sun }, - { value: 'dark', label: '深色', icon: Moon }, - { value: 'system', label: '跟随系统', icon: Monitor }, -]; - -const DARK_INTENSITY_OPTIONS: Opt[] = [ - { value: 'soft', label: '柔和' }, - { value: 'black', label: '纯黑 (OLED)' }, -]; - -const SCOPE_OPTIONS: Opt[] = [ - { value: 'global', label: '全局' }, - { value: 'sidebar', label: '仅侧栏' }, -]; - -const SIDEBAR_STYLE_OPTIONS: Opt[] = [ - { value: 'follow', label: '跟随背景' }, - { value: 'panel', label: '浅色面板' }, - { value: 'accent', label: '强调色' }, -]; - -const BG_TYPE_OPTIONS: Opt[] = [ - { value: 'none', label: '无' }, - { value: 'solid', label: '纯色' }, - { value: 'gradient', label: '渐变' }, - { value: 'image', label: '图片' }, -]; - -const DENSITY_OPTIONS: Opt[] = [ - { value: 'cozy', label: '舒适' }, - { value: 'compact', label: '紧凑' }, -]; - -function AppearancePanel() { - const { appearance, setAppearance, uploadBackground, removeBackground } = useTheme(); - const a = appearance; - - return ( -
- {/* 主题 */} - - - 主题 - 明暗模式与深色风格。所有改动立即生效并保存到服务器,跨设备同步。 - - - - setAppearance({ mode })} /> - - - - setAppearance({ darkIntensity })} /> - - - - - {/* 强调色 */} - - - 强调色 - 按钮、链接与高亮使用的主色调。 - - - -
- {ACCENTS.map((spec) => { - const active = a.accentMode === 'preset' && a.accentPreset === spec.id; - return ( - - ); - })} - {/* custom colour */} - -
-
- - - setAppearance({ accentScope })} /> - - - - setAppearance({ sidebarStyle })} /> - -
-
- - {/* 背景 */} - - - {/* 排版与界面 */} - - - 排版与界面 - 字体、圆角、缩放与密度。 - - -
- - ({ value: f.id, label: f.label }))} onChange={(fontSans) => setAppearance({ fontSans })} /> - - - ({ value: f.id, label: f.label }))} onChange={(fontMono) => setAppearance({ fontMono })} /> - -
- - - ({ value: r.value, label: `${r.label}(${r.value}rem)` }))} - onChange={(radius) => setAppearance({ radius })} - /> - - - -
- {Math.round(UI_SCALE.min * 100)}% - setAppearance({ uiScale: Number(e.target.value) })} - className="h-2 flex-1 cursor-pointer accent-primary" - aria-label="界面缩放" - /> - {Math.round(UI_SCALE.max * 100)}% -
-
- - - setAppearance({ density })} /> - -
-
- - {/* 无障碍与侧栏 */} - - - 无障碍与侧栏 - 动效、对比度与侧栏默认状态。 - - - setAppearance({ reduceMotion })} - /> - - setAppearance({ highContrast })} - /> - - setAppearance({ sidebarDefaultCollapsed })} - /> - - -
- ); -} - -function BackgroundCard({ - appearance: a, setAppearance, uploadBackground, removeBackground, -}: { - appearance: ReturnType['appearance']; - setAppearance: ReturnType['setAppearance']; - uploadBackground: ReturnType['uploadBackground']; - removeBackground: ReturnType['removeBackground']; -}) { - const fileRef = useRef(null); - const [uploading, setUploading] = useState(false); - const [busy, setBusy] = useState(false); - const [err, setErr] = useState(null); - - const onFile = async (e: React.ChangeEvent) => { - const file = e.target.files?.[0]; - if (fileRef.current) fileRef.current.value = ''; - if (!file) return; - setUploading(true); - setErr(null); - try { - await uploadBackground(file); - } catch (ex) { - setErr(ex instanceof Error ? ex.message : '上传失败'); - } finally { - setUploading(false); - } - }; - - const onRemove = async () => { - setBusy(true); - setErr(null); - try { - await removeBackground(); - } catch { - setErr('删除失败'); - } finally { - setBusy(false); - } - }; - - return ( - - - 背景 - 纯色、渐变或自定义壁纸,会显示在卡片之间与登录页。 - - - - setAppearance({ background: { type } })} /> - - - {a.background.type === 'solid' && ( - -
- - {a.background.color} -
-
- )} - - {a.background.type === 'gradient' && ( - -
- {GRADIENT_OPTIONS.map((g) => { - const active = a.background.gradient === g.id; - return ( -
-
- )} - - {a.background.type === 'image' && ( -
- -
- - - {a.background.hasImage && ( - - )} -
-
- - {a.background.hasImage && ( - <> -
- 背景预览 -
- - setAppearance({ background: { imageOpacity: Number(e.target.value) } })} - className="h-2 w-full cursor-pointer accent-primary" - aria-label="遮罩强度" - /> - - - setAppearance({ background: { imageBlur: Number(e.target.value) } })} - className="h-2 w-full cursor-pointer accent-primary" - aria-label="背景模糊" - /> - - - )} -
- )} - - {err &&

{err}

} -
-
- ); -} - -// ─────────────── 数据与格式 ─────────────── - -const TIME_FORMAT_OPTIONS: Opt[] = [ - { value: '24h', label: '24 小时制' }, - { value: '12h', label: '12 小时制 (AM/PM)' }, -]; - -function DataPanel() { - const { appearance, setAppearance } = useTheme(); - const { pages, setPages } = useLayout(); - return ( -
- - - 数据刷新 - 总览页轮询刷新 QQ 列表、进程列表与系统状态的间隔。 - - - - ({ value: o.value, label: o.label }))} - onChange={(pollInterval) => setAppearance({ pollInterval })} - /> - - - - - - - 登录后落地页 - 登录后默认打开的页面。 - - - - ({ value: n.to as string, label: n.label }))} - onChange={(defaultRoute) => setPages({ defaultRoute })} - /> - - - - - - - 时间格式 - 日志与告警等时间戳的显示方式。 - - - - setAppearance({ timeFormat })} /> - - - -
- ); -} - // ─────────────── 账号安全 ─────────────── function AccountPanel() { @@ -703,39 +672,29 @@ function AccountPanel() { const [pwdSavedAt, setPwdSavedAt] = useState(null); return ( - - - 账号安全 - WebUI 仅有 admin 一个账号,密码会以 scrypt 哈希形式持久化到 config/webui.json。 - - - - {pwdSavedAt && ( - - 密码已更新({new Date(pwdSavedAt).toLocaleTimeString()})— 其他设备的会话已失效。 - - )} - - - -
-

退出登录

-

立即清除当前浏览器的会话令牌。

- -
-
+ + + + setPwdSavedAt(Date.now())} /> -
+ ); }