feat(webui): settings tabs, password dialog, instant node save, error pages
Five WebUI changes: 1. Settings page rebuilt around mini tabs (外观 / 数据刷新 / 账号安全) instead of three stacked cards; content and behaviour unchanged. 2. "修改访问密码" now opens an independent ChangePasswordDialog modal rather than swapping the whole page for the forced-change screen. The form is extracted to a shared ChangePasswordForm used by both the modal and the forced full-screen ChangePasswordPage — behaviour identical. 3. Node config: creating / editing / deleting / enable-toggling an HTTP/WS endpoint now persists immediately — clicking 保存 in the editor dialog IS the save. Previously it only marked the config dirty until you also hit the top-right 保存, which silently cost many users their token edits. `save()` takes an explicit config so it persists the freshly computed value, not the stale closed-over state. The general-settings tab keeps its explicit top-right save. 4. Forced first-time change-password no longer renders an old-password field: it carries the password the user just logged in with. Browsers (Edge/Chrome) were autofilling that field on upgrade, so users thought the old password was pre-filled correctly and could never save. A returning already-authed session (no fresh login) still shows the field. 5. Added default error + 404 screens (status-screens.tsx), wired into the TanStack router via defaultErrorComponent / defaultNotFoundComponent.
This commit is contained in:
9 files changed
+768
-495
No files matched your search
@@ -21,6 +21,11 @@ function AuthBoundary() {
|
||||
const [authed, setAuthed] = useState(false);
|
||||
const [mustChange, setMustChange] = useState(false);
|
||||
const [status, setStatus] = useState('未连接');
|
||||
// The password from *this* session's login, carried into the forced
|
||||
// change-password gate so it doesn't have to render an old-password field
|
||||
// (which browsers autofill, misleading users on upgrade). Stays undefined
|
||||
// for a returning session that's already authed but still must change.
|
||||
const [loginPassword, setLoginPassword] = useState<string | undefined>(undefined);
|
||||
|
||||
const client = useMemo<ApiClient>(
|
||||
() =>
|
||||
@@ -63,14 +68,21 @@ function AuthBoundary() {
|
||||
</div>
|
||||
) : !authed ? (
|
||||
<LoginGate
|
||||
onAuthed={(needsChange) => {
|
||||
onAuthed={(needsChange, password) => {
|
||||
setAuthed(true);
|
||||
setStatus('已连接');
|
||||
setMustChange(needsChange);
|
||||
setLoginPassword(password);
|
||||
}}
|
||||
/>
|
||||
) : mustChange ? (
|
||||
<ForcedChangePasswordGate onSuccess={() => setMustChange(false)} />
|
||||
<ForcedChangePasswordGate
|
||||
knownOldPassword={loginPassword}
|
||||
onSuccess={() => {
|
||||
setMustChange(false);
|
||||
setLoginPassword(undefined);
|
||||
}}
|
||||
/>
|
||||
) : (
|
||||
<SessionProvider value={{ status, onLogoutComplete: handleLogoutComplete }}>
|
||||
<RouterProvider router={appRouter} />
|
||||
@@ -81,13 +93,13 @@ function AuthBoundary() {
|
||||
);
|
||||
}
|
||||
|
||||
function LoginGate({ onAuthed }: { onAuthed: (mustChange: boolean) => void }) {
|
||||
function LoginGate({ onAuthed }: { onAuthed: (mustChange: boolean, password: string) => void }) {
|
||||
const api = useApi();
|
||||
const handleLogin = useCallback(
|
||||
async (password: string) => {
|
||||
const result = await api.login(password);
|
||||
if (!result.ok) return { success: false, error: result.message };
|
||||
onAuthed(result.mustChangePassword);
|
||||
onAuthed(result.mustChangePassword, password);
|
||||
return { success: true };
|
||||
},
|
||||
[api, onAuthed],
|
||||
@@ -95,11 +107,17 @@ function LoginGate({ onAuthed }: { onAuthed: (mustChange: boolean) => void }) {
|
||||
return <LoginPage onLogin={handleLogin} />;
|
||||
}
|
||||
|
||||
function ForcedChangePasswordGate({ onSuccess }: { onSuccess: () => void }) {
|
||||
function ForcedChangePasswordGate({
|
||||
knownOldPassword,
|
||||
onSuccess,
|
||||
}: {
|
||||
knownOldPassword?: string;
|
||||
onSuccess: () => void;
|
||||
}) {
|
||||
const api = useApi();
|
||||
return (
|
||||
<ChangePasswordPage
|
||||
forced
|
||||
knownOldPassword={knownOldPassword}
|
||||
checkStrength={(p) => api.checkPasswordStrength(p)}
|
||||
submit={(o, n) => api.changePassword(o, n)}
|
||||
onSuccess={onSuccess}
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogDescription,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
} from '@/components/ui/dialog';
|
||||
import { ChangePasswordForm } from '@/components/pages/change-password-form';
|
||||
import { useApi } from '@/lib/api';
|
||||
|
||||
interface ChangePasswordDialogProps {
|
||||
open: boolean;
|
||||
onOpenChange: (open: boolean) => void;
|
||||
/** Fired after the password is changed successfully (dialog closes itself). */
|
||||
onSuccess: () => void;
|
||||
}
|
||||
|
||||
/**
|
||||
* In-app "修改访问密码" modal. The forced first-time flow uses the full-screen
|
||||
* ChangePasswordPage; this is the voluntary change a logged-in admin triggers
|
||||
* from 设置 → 账号安全. Both share ChangePasswordForm, so behaviour is identical.
|
||||
*/
|
||||
export function ChangePasswordDialog({ open, onOpenChange, onSuccess }: ChangePasswordDialogProps) {
|
||||
const api = useApi();
|
||||
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={onOpenChange}>
|
||||
<DialogContent className="max-w-md">
|
||||
<DialogHeader>
|
||||
<DialogTitle>修改访问密码</DialogTitle>
|
||||
<DialogDescription>
|
||||
设置一个全新的强密码后,其它会话将被立即下线,需要重新登录。
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
<ChangePasswordForm
|
||||
checkStrength={(p) => api.checkPasswordStrength(p)}
|
||||
submit={(o, n) => api.changePassword(o, n)}
|
||||
onSuccess={() => {
|
||||
onSuccess();
|
||||
onOpenChange(false);
|
||||
}}
|
||||
onCancel={() => onOpenChange(false)}
|
||||
idPrefix="dialog-cpw"
|
||||
/>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,262 @@
|
||||
import { useEffect, useMemo, useState } from 'react';
|
||||
import { motion, AnimatePresence } from 'motion/react';
|
||||
import { Check, Eye, EyeOff, Loader2 } from 'lucide-react';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Input } from '@/components/ui/input';
|
||||
import { Label } from '@/components/ui/label';
|
||||
import { cn } from '@/lib/utils';
|
||||
|
||||
export interface PasswordRule {
|
||||
id: string;
|
||||
label: string;
|
||||
ok: boolean;
|
||||
}
|
||||
|
||||
export interface ChangePasswordFormProps {
|
||||
/**
|
||||
* When provided, the "current password" field is omitted entirely and this
|
||||
* value is used as the old password.
|
||||
*
|
||||
* Used by the forced first-time flow: the old password is *exactly* what
|
||||
* the user just logged in with, so we fill it ourselves. Rendering the
|
||||
* field at all let Edge/Chrome password-managers autofill it on upgrade —
|
||||
* users saw it pre-filled, assumed it was correct, and could never save
|
||||
* (the autofilled value was stale / not the temp password). Dropping the
|
||||
* field removes the trap entirely.
|
||||
*/
|
||||
knownOldPassword?: string;
|
||||
/** Sends `{ password }`; returns the rule list + valid flag. */
|
||||
checkStrength: (password: string) => Promise<{ rules: PasswordRule[]; valid: boolean }>;
|
||||
/** Sends `{ oldPassword, newPassword }`. Returns success or error message. */
|
||||
submit: (oldPassword: string, newPassword: string) => Promise<{ success: boolean; message?: string }>;
|
||||
/** Called after a successful submit. */
|
||||
onSuccess: () => void;
|
||||
/** When provided, a cancel button is shown (used by the settings dialog). */
|
||||
onCancel?: () => void;
|
||||
/** Disambiguates input ids if two instances ever mount at once. */
|
||||
idPrefix?: string;
|
||||
submitLabel?: string;
|
||||
}
|
||||
|
||||
export function ChangePasswordForm({
|
||||
knownOldPassword,
|
||||
checkStrength,
|
||||
submit,
|
||||
onSuccess,
|
||||
onCancel,
|
||||
idPrefix = 'cpw',
|
||||
submitLabel = '保存新密码',
|
||||
}: ChangePasswordFormProps) {
|
||||
const carriesOld = knownOldPassword !== undefined;
|
||||
|
||||
const [oldPassword, setOldPassword] = useState('');
|
||||
const [newPassword, setNewPassword] = useState('');
|
||||
const [confirmPassword, setConfirmPassword] = useState('');
|
||||
const [showOld, setShowOld] = useState(false);
|
||||
const [showNew, setShowNew] = useState(false);
|
||||
const [rules, setRules] = useState<PasswordRule[]>([]);
|
||||
const [valid, setValid] = useState(false);
|
||||
const [submitting, setSubmitting] = useState(false);
|
||||
const [error, setError] = useState('');
|
||||
|
||||
const effectiveOld = carriesOld ? (knownOldPassword as string) : oldPassword;
|
||||
const confirmMatches = newPassword.length > 0 && newPassword === confirmPassword;
|
||||
const canSubmit =
|
||||
!submitting && effectiveOld.length > 0 && valid && confirmMatches && effectiveOld !== newPassword;
|
||||
|
||||
// Debounce the strength check so we don't slam the API on every keystroke.
|
||||
useEffect(() => {
|
||||
let cancelled = false;
|
||||
const handle = window.setTimeout(async () => {
|
||||
try {
|
||||
const res = await checkStrength(newPassword);
|
||||
if (cancelled) return;
|
||||
setRules(res.rules);
|
||||
setValid(res.valid);
|
||||
} catch {
|
||||
/* ignore – the form will just stay disabled */
|
||||
}
|
||||
}, 180);
|
||||
return () => {
|
||||
cancelled = true;
|
||||
window.clearTimeout(handle);
|
||||
};
|
||||
}, [newPassword, checkStrength]);
|
||||
|
||||
const handleSubmit = async (e: React.FormEvent) => {
|
||||
e.preventDefault();
|
||||
if (!canSubmit) return;
|
||||
setSubmitting(true);
|
||||
setError('');
|
||||
try {
|
||||
const res = await submit(effectiveOld, newPassword);
|
||||
if (res.success) {
|
||||
onSuccess();
|
||||
} else {
|
||||
setError(res.message || '修改失败');
|
||||
}
|
||||
} catch (err) {
|
||||
setError(err instanceof Error ? err.message : '网络错误');
|
||||
} finally {
|
||||
setSubmitting(false);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<form onSubmit={handleSubmit} className="flex flex-col gap-4">
|
||||
{!carriesOld && (
|
||||
<div className="flex flex-col gap-1.5">
|
||||
<Label htmlFor={`${idPrefix}-old`}>当前密码</Label>
|
||||
<div className="relative">
|
||||
<Input
|
||||
id={`${idPrefix}-old`}
|
||||
type={showOld ? 'text' : 'password'}
|
||||
autoComplete="current-password"
|
||||
value={oldPassword}
|
||||
onChange={(e) => setOldPassword(e.target.value)}
|
||||
placeholder="输入当前访问密码"
|
||||
className="h-10 pr-10 text-sm"
|
||||
/>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setShowOld((v) => !v)}
|
||||
aria-label={showOld ? '隐藏密码' : '显示密码'}
|
||||
tabIndex={-1}
|
||||
className="absolute right-2 top-1/2 flex size-7 -translate-y-1/2 items-center justify-center rounded-md text-muted-foreground hover:bg-accent hover:text-foreground"
|
||||
>
|
||||
{showOld ? <EyeOff className="size-4" /> : <Eye className="size-4" />}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="flex flex-col gap-1.5">
|
||||
<Label htmlFor={`${idPrefix}-new`}>新密码</Label>
|
||||
<div className="relative">
|
||||
<Input
|
||||
id={`${idPrefix}-new`}
|
||||
type={showNew ? 'text' : 'password'}
|
||||
autoComplete="new-password"
|
||||
value={newPassword}
|
||||
onChange={(e) => setNewPassword(e.target.value)}
|
||||
placeholder="设置新的强密码"
|
||||
className="h-10 pr-10 text-sm"
|
||||
/>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setShowNew((v) => !v)}
|
||||
aria-label={showNew ? '隐藏密码' : '显示密码'}
|
||||
tabIndex={-1}
|
||||
className="absolute right-2 top-1/2 flex size-7 -translate-y-1/2 items-center justify-center rounded-md text-muted-foreground hover:bg-accent hover:text-foreground"
|
||||
>
|
||||
{showNew ? <EyeOff className="size-4" /> : <Eye className="size-4" />}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="flex flex-col gap-1.5">
|
||||
<Label htmlFor={`${idPrefix}-confirm`}>确认新密码</Label>
|
||||
<Input
|
||||
id={`${idPrefix}-confirm`}
|
||||
type={showNew ? 'text' : 'password'}
|
||||
autoComplete="new-password"
|
||||
value={confirmPassword}
|
||||
onChange={(e) => setConfirmPassword(e.target.value)}
|
||||
placeholder="再次输入新密码"
|
||||
className="h-10 text-sm"
|
||||
/>
|
||||
{confirmPassword.length > 0 && !confirmMatches && (
|
||||
<span className="text-[11px] text-destructive">两次输入的密码不一致</span>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<RuleList rules={rules} />
|
||||
|
||||
<AnimatePresence>
|
||||
{error && (
|
||||
<motion.p
|
||||
initial={{ opacity: 0, y: -4 }}
|
||||
animate={{ opacity: 1, y: 0 }}
|
||||
exit={{ opacity: 0 }}
|
||||
className="rounded-md bg-destructive/10 px-3 py-2 text-center text-xs text-destructive"
|
||||
>
|
||||
{error}
|
||||
</motion.p>
|
||||
)}
|
||||
</AnimatePresence>
|
||||
|
||||
<div className="flex items-center gap-2">
|
||||
{onCancel && (
|
||||
<Button type="button" variant="ghost" onClick={onCancel} className="h-10">
|
||||
取消
|
||||
</Button>
|
||||
)}
|
||||
<Button type="submit" disabled={!canSubmit} className="ml-auto h-10">
|
||||
{submitting ? (
|
||||
<>
|
||||
<Loader2 className="size-4 animate-spin" /> 提交中…
|
||||
</>
|
||||
) : (
|
||||
submitLabel
|
||||
)}
|
||||
</Button>
|
||||
</div>
|
||||
</form>
|
||||
);
|
||||
}
|
||||
|
||||
function RuleList({ rules }: { rules: PasswordRule[] }) {
|
||||
// Initial empty state: show a placeholder set so the user knows the rules
|
||||
// exist even before any input.
|
||||
const display = useMemo<PasswordRule[]>(() => {
|
||||
if (rules.length > 0) return rules;
|
||||
return [
|
||||
{ id: 'length', label: '长度不少于 10 位', ok: false },
|
||||
{ id: 'lower', label: '至少包含一个小写字母', ok: false },
|
||||
{ id: 'upper', label: '至少包含一个大写字母', ok: false },
|
||||
{ id: 'special', label: '至少包含一个特殊字符', ok: false },
|
||||
{ id: 'no-space', label: '不得包含空格', ok: false },
|
||||
];
|
||||
}, [rules]);
|
||||
|
||||
return (
|
||||
<ul className="grid gap-1.5 rounded-lg border bg-muted/30 p-3">
|
||||
{display.map((rule) => (
|
||||
<li key={rule.id} className="flex items-center gap-2">
|
||||
<motion.span
|
||||
initial={false}
|
||||
animate={{
|
||||
backgroundColor: rule.ok ? 'color-mix(in oklab, var(--primary) 20%, transparent)' : 'transparent',
|
||||
borderColor: rule.ok ? 'var(--primary)' : 'var(--border)',
|
||||
scale: rule.ok ? [1, 1.18, 1] : 1,
|
||||
}}
|
||||
transition={{ duration: 0.25 }}
|
||||
className={cn('flex size-4 items-center justify-center rounded-full border')}
|
||||
>
|
||||
<AnimatePresence>
|
||||
{rule.ok && (
|
||||
<motion.span
|
||||
key="check"
|
||||
initial={{ opacity: 0, scale: 0.4 }}
|
||||
animate={{ opacity: 1, scale: 1 }}
|
||||
exit={{ opacity: 0, scale: 0.4 }}
|
||||
transition={{ duration: 0.18 }}
|
||||
>
|
||||
<Check className="size-3 text-primary" strokeWidth={3} />
|
||||
</motion.span>
|
||||
)}
|
||||
</AnimatePresence>
|
||||
</motion.span>
|
||||
<motion.span
|
||||
initial={false}
|
||||
animate={{ color: rule.ok ? 'var(--foreground)' : 'var(--muted-foreground)' }}
|
||||
transition={{ duration: 0.2 }}
|
||||
className="text-xs"
|
||||
>
|
||||
{rule.label}
|
||||
</motion.span>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
);
|
||||
}
|
||||
@@ -1,104 +1,40 @@
|
||||
import { useEffect, useMemo, useState } from 'react';
|
||||
import { motion, AnimatePresence } from 'motion/react';
|
||||
import { Check, Eye, EyeOff, KeyRound, Loader2, ShieldAlert } from 'lucide-react';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { motion } from 'motion/react';
|
||||
import { ShieldAlert } from 'lucide-react';
|
||||
import { Card, CardContent } from '@/components/ui/card';
|
||||
import { Input } from '@/components/ui/input';
|
||||
import { Label } from '@/components/ui/label';
|
||||
import { ThemeToggle } from '@/components/theme-toggle';
|
||||
import { cn } from '@/lib/utils';
|
||||
import { ChangePasswordForm, type PasswordRule } from '@/components/pages/change-password-form';
|
||||
|
||||
export interface PasswordRule {
|
||||
id: string;
|
||||
label: string;
|
||||
ok: boolean;
|
||||
}
|
||||
// Re-exported so existing importers (e.g. the API client) keep resolving
|
||||
// `PasswordRule` from this module after the form was extracted.
|
||||
export type { PasswordRule };
|
||||
|
||||
interface ChangePasswordPageProps {
|
||||
/** When true, this is the forced first-time change flow. */
|
||||
forced?: boolean;
|
||||
/** Heading shown above the form. */
|
||||
/**
|
||||
* The password the user just logged in with. Passed straight through to the
|
||||
* form as `knownOldPassword`, so the forced flow never renders an
|
||||
* old-password field for a browser to autofill. See change-password-form.
|
||||
*/
|
||||
knownOldPassword?: string;
|
||||
title?: string;
|
||||
/** Subheading. */
|
||||
description?: string;
|
||||
/** Sends `{ password }` and returns the rule list + valid flag. */
|
||||
checkStrength: (password: string) => Promise<{ rules: PasswordRule[]; valid: boolean }>;
|
||||
/** Sends `{ oldPassword, newPassword }`. Returns success or error message. */
|
||||
submit: (oldPassword: string, newPassword: string) => Promise<{ success: boolean; message?: string }>;
|
||||
/** Called after a successful submit. */
|
||||
onSuccess: () => void;
|
||||
/** Called when user cancels (only available when not forced). */
|
||||
onCancel?: () => void;
|
||||
}
|
||||
|
||||
/**
|
||||
* Full-screen "you must set a new password before continuing" gate, shown
|
||||
* once right after the first login with a generated temp password. The
|
||||
* in-app "修改访问密码" action uses ChangePasswordDialog instead.
|
||||
*/
|
||||
export function ChangePasswordPage({
|
||||
forced = false,
|
||||
title,
|
||||
description,
|
||||
knownOldPassword,
|
||||
title = '请先设置新的访问密码',
|
||||
description = '为了保护你的实例,必须将首次启动生成的临时密码替换为符合下列要求的强密码。',
|
||||
checkStrength,
|
||||
submit,
|
||||
onSuccess,
|
||||
onCancel,
|
||||
}: ChangePasswordPageProps) {
|
||||
const [oldPassword, setOldPassword] = useState('');
|
||||
const [newPassword, setNewPassword] = useState('');
|
||||
const [confirmPassword, setConfirmPassword] = useState('');
|
||||
const [showOld, setShowOld] = useState(false);
|
||||
const [showNew, setShowNew] = useState(false);
|
||||
const [rules, setRules] = useState<PasswordRule[]>([]);
|
||||
const [valid, setValid] = useState(false);
|
||||
const [submitting, setSubmitting] = useState(false);
|
||||
const [error, setError] = useState('');
|
||||
|
||||
const confirmMatches = newPassword.length > 0 && newPassword === confirmPassword;
|
||||
const canSubmit =
|
||||
!submitting && oldPassword.length > 0 && valid && confirmMatches && oldPassword !== newPassword;
|
||||
|
||||
// Debounce the strength check so we don't slam the API on every keystroke.
|
||||
useEffect(() => {
|
||||
let cancelled = false;
|
||||
const handle = window.setTimeout(async () => {
|
||||
try {
|
||||
const res = await checkStrength(newPassword);
|
||||
if (cancelled) return;
|
||||
setRules(res.rules);
|
||||
setValid(res.valid);
|
||||
} catch {
|
||||
/* ignore – the form will just stay disabled */
|
||||
}
|
||||
}, 180);
|
||||
return () => {
|
||||
cancelled = true;
|
||||
window.clearTimeout(handle);
|
||||
};
|
||||
}, [newPassword, checkStrength]);
|
||||
|
||||
const handleSubmit = async (e: React.FormEvent) => {
|
||||
e.preventDefault();
|
||||
if (!canSubmit) return;
|
||||
setSubmitting(true);
|
||||
setError('');
|
||||
try {
|
||||
const res = await submit(oldPassword, newPassword);
|
||||
if (res.success) {
|
||||
onSuccess();
|
||||
} else {
|
||||
setError(res.message || '修改失败');
|
||||
}
|
||||
} catch (err) {
|
||||
setError(err instanceof Error ? err.message : '网络错误');
|
||||
} finally {
|
||||
setSubmitting(false);
|
||||
}
|
||||
};
|
||||
|
||||
const computedTitle = title ?? (forced ? '请先设置新的访问密码' : '修改访问密码');
|
||||
const computedDescription =
|
||||
description ??
|
||||
(forced
|
||||
? '为了保护你的实例,必须将首次启动生成的临时密码替换为符合下列要求的强密码。'
|
||||
: '设置一个全新的强密码后,其他会话将被立即下线,需要重新登录。');
|
||||
|
||||
return (
|
||||
<div className="relative flex min-h-screen items-center justify-center overflow-hidden bg-background px-4 py-8">
|
||||
<div
|
||||
@@ -128,176 +64,26 @@ export function ChangePasswordPage({
|
||||
<CardContent className="p-7 sm:p-9">
|
||||
<div className="flex items-start gap-3">
|
||||
<div className="flex size-12 shrink-0 items-center justify-center rounded-2xl bg-primary/10 ring-1 ring-primary/20">
|
||||
{forced ? (
|
||||
<ShieldAlert className="size-6 text-primary" />
|
||||
) : (
|
||||
<KeyRound className="size-6 text-primary" />
|
||||
)}
|
||||
<ShieldAlert className="size-6 text-primary" />
|
||||
</div>
|
||||
<div className="min-w-0">
|
||||
<h1 className="text-lg font-semibold tracking-tight">{computedTitle}</h1>
|
||||
<p className="mt-1 text-xs leading-relaxed text-muted-foreground">{computedDescription}</p>
|
||||
<h1 className="text-lg font-semibold tracking-tight">{title}</h1>
|
||||
<p className="mt-1 text-xs leading-relaxed text-muted-foreground">{description}</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<form onSubmit={handleSubmit} className="mt-6 flex flex-col gap-4">
|
||||
<div className="flex flex-col gap-1.5">
|
||||
<Label htmlFor="old">当前密码</Label>
|
||||
<div className="relative">
|
||||
<Input
|
||||
id="old"
|
||||
type={showOld ? 'text' : 'password'}
|
||||
autoComplete="current-password"
|
||||
value={oldPassword}
|
||||
onChange={(e) => setOldPassword(e.target.value)}
|
||||
placeholder="输入当前访问密码"
|
||||
className="h-10 pr-10 text-sm"
|
||||
/>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setShowOld((v) => !v)}
|
||||
aria-label={showOld ? '隐藏密码' : '显示密码'}
|
||||
tabIndex={-1}
|
||||
className="absolute right-2 top-1/2 flex size-7 -translate-y-1/2 items-center justify-center rounded-md text-muted-foreground hover:bg-accent hover:text-foreground"
|
||||
>
|
||||
{showOld ? <EyeOff className="size-4" /> : <Eye className="size-4" />}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="flex flex-col gap-1.5">
|
||||
<Label htmlFor="new">新密码</Label>
|
||||
<div className="relative">
|
||||
<Input
|
||||
id="new"
|
||||
type={showNew ? 'text' : 'password'}
|
||||
autoComplete="new-password"
|
||||
value={newPassword}
|
||||
onChange={(e) => setNewPassword(e.target.value)}
|
||||
placeholder="设置新的强密码"
|
||||
className="h-10 pr-10 text-sm"
|
||||
/>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setShowNew((v) => !v)}
|
||||
aria-label={showNew ? '隐藏密码' : '显示密码'}
|
||||
tabIndex={-1}
|
||||
className="absolute right-2 top-1/2 flex size-7 -translate-y-1/2 items-center justify-center rounded-md text-muted-foreground hover:bg-accent hover:text-foreground"
|
||||
>
|
||||
{showNew ? <EyeOff className="size-4" /> : <Eye className="size-4" />}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="flex flex-col gap-1.5">
|
||||
<Label htmlFor="confirm">确认新密码</Label>
|
||||
<Input
|
||||
id="confirm"
|
||||
type={showNew ? 'text' : 'password'}
|
||||
autoComplete="new-password"
|
||||
value={confirmPassword}
|
||||
onChange={(e) => setConfirmPassword(e.target.value)}
|
||||
placeholder="再次输入新密码"
|
||||
className="h-10 text-sm"
|
||||
/>
|
||||
{confirmPassword.length > 0 && !confirmMatches && (
|
||||
<span className="text-[11px] text-destructive">两次输入的密码不一致</span>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<RuleList rules={rules} />
|
||||
|
||||
<AnimatePresence>
|
||||
{error && (
|
||||
<motion.p
|
||||
initial={{ opacity: 0, y: -4 }}
|
||||
animate={{ opacity: 1, y: 0 }}
|
||||
exit={{ opacity: 0 }}
|
||||
className="rounded-md bg-destructive/10 px-3 py-2 text-center text-xs text-destructive"
|
||||
>
|
||||
{error}
|
||||
</motion.p>
|
||||
)}
|
||||
</AnimatePresence>
|
||||
|
||||
<div className="flex items-center gap-2">
|
||||
{!forced && onCancel && (
|
||||
<Button type="button" variant="ghost" onClick={onCancel} className="h-10">
|
||||
取消
|
||||
</Button>
|
||||
)}
|
||||
<Button type="submit" disabled={!canSubmit} className="ml-auto h-10">
|
||||
{submitting ? (
|
||||
<>
|
||||
<Loader2 className="size-4 animate-spin" /> 提交中…
|
||||
</>
|
||||
) : (
|
||||
'保存新密码'
|
||||
)}
|
||||
</Button>
|
||||
</div>
|
||||
</form>
|
||||
<div className="mt-6">
|
||||
<ChangePasswordForm
|
||||
knownOldPassword={knownOldPassword}
|
||||
checkStrength={checkStrength}
|
||||
submit={submit}
|
||||
onSuccess={onSuccess}
|
||||
idPrefix="forced-cpw"
|
||||
/>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</motion.div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function RuleList({ rules }: { rules: PasswordRule[] }) {
|
||||
// Initial empty state: show a placeholder set so the user knows the rules
|
||||
// exist even before any input.
|
||||
const display = useMemo<PasswordRule[]>(() => {
|
||||
if (rules.length > 0) return rules;
|
||||
return [
|
||||
{ id: 'length', label: '长度不少于 10 位', ok: false },
|
||||
{ id: 'lower', label: '至少包含一个小写字母', ok: false },
|
||||
{ id: 'upper', label: '至少包含一个大写字母', ok: false },
|
||||
{ id: 'special', label: '至少包含一个特殊字符', ok: false },
|
||||
{ id: 'no-space', label: '不得包含空格', ok: false },
|
||||
];
|
||||
}, [rules]);
|
||||
|
||||
return (
|
||||
<ul className="grid gap-1.5 rounded-lg border bg-muted/30 p-3">
|
||||
{display.map((rule) => (
|
||||
<li key={rule.id} className="flex items-center gap-2">
|
||||
<motion.span
|
||||
initial={false}
|
||||
animate={{
|
||||
backgroundColor: rule.ok ? 'color-mix(in oklab, var(--primary) 20%, transparent)' : 'transparent',
|
||||
borderColor: rule.ok ? 'var(--primary)' : 'var(--border)',
|
||||
scale: rule.ok ? [1, 1.18, 1] : 1,
|
||||
}}
|
||||
transition={{ duration: 0.25 }}
|
||||
className={cn(
|
||||
'flex size-4 items-center justify-center rounded-full border',
|
||||
)}
|
||||
>
|
||||
<AnimatePresence>
|
||||
{rule.ok && (
|
||||
<motion.span
|
||||
key="check"
|
||||
initial={{ opacity: 0, scale: 0.4 }}
|
||||
animate={{ opacity: 1, scale: 1 }}
|
||||
exit={{ opacity: 0, scale: 0.4 }}
|
||||
transition={{ duration: 0.18 }}
|
||||
>
|
||||
<Check className="size-3 text-primary" strokeWidth={3} />
|
||||
</motion.span>
|
||||
)}
|
||||
</AnimatePresence>
|
||||
</motion.span>
|
||||
<motion.span
|
||||
initial={false}
|
||||
animate={{ color: rule.ok ? 'var(--foreground)' : 'var(--muted-foreground)' }}
|
||||
transition={{ duration: 0.2 }}
|
||||
className="text-xs"
|
||||
>
|
||||
{rule.label}
|
||||
</motion.span>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
);
|
||||
}
|
||||
@@ -68,40 +68,47 @@ export function ConfigPage() {
|
||||
[pendingSwitchUin, qqList],
|
||||
);
|
||||
|
||||
// Per-kind helpers (typed at the call site so the dialog's onSubmit
|
||||
// payload is exactly the kind's adapter shape).
|
||||
function updateKind<K extends NetworkKind>(kind: K, next: OneBotNetworks[K]): void {
|
||||
// A discrete node mutation (create / edit / delete / enable-toggle) is
|
||||
// persisted the moment it happens — clicking 保存 inside the editor dialog
|
||||
// (or flipping the enable switch) IS the save. This removes the old
|
||||
// two-step trap where editing a token in the dialog only marked the config
|
||||
// "dirty" until you also pressed the top-right 保存, which silently cost
|
||||
// many users their token edits. The general-settings tab keeps its explicit
|
||||
// top-right save (it's a continuously-edited free-form surface).
|
||||
function commitKind<K extends NetworkKind>(kind: K, nextList: OneBotNetworks[K]): void {
|
||||
if (!config) return;
|
||||
setConfig({ ...config, networks: { ...config.networks, [kind]: next } });
|
||||
const next = { ...config, networks: { ...config.networks, [kind]: nextList } };
|
||||
setConfig(next);
|
||||
void save(next);
|
||||
}
|
||||
|
||||
function handleCreate<K extends NetworkKind>(kind: K, item: OneBotNetworks[K][number]): void {
|
||||
if (!config) return;
|
||||
const list = config.networks[kind] as OneBotNetworks[K];
|
||||
updateKind(kind, [...list, item] as OneBotNetworks[K]);
|
||||
commitKind(kind, [...list, item] as OneBotNetworks[K]);
|
||||
}
|
||||
|
||||
function handleEdit<K extends NetworkKind>(kind: K, index: number, item: OneBotNetworks[K][number]): void {
|
||||
if (!config) return;
|
||||
const list = config.networks[kind] as OneBotNetworks[K];
|
||||
const next = list.map((it, i) => (i === index ? item : it)) as OneBotNetworks[K];
|
||||
updateKind(kind, next);
|
||||
commitKind(kind, list.map((it, i) => (i === index ? item : it)) as OneBotNetworks[K]);
|
||||
}
|
||||
|
||||
function handleDelete<K extends NetworkKind>(kind: K, index: number): void {
|
||||
if (!config) return;
|
||||
const list = config.networks[kind] as OneBotNetworks[K];
|
||||
const next = list.filter((_, i) => i !== index) as OneBotNetworks[K];
|
||||
updateKind(kind, next);
|
||||
commitKind(kind, list.filter((_, i) => i !== index) as OneBotNetworks[K]);
|
||||
}
|
||||
|
||||
function handleToggleEnabled<K extends NetworkKind>(kind: K, index: number, enabled: boolean): void {
|
||||
if (!config) return;
|
||||
const list = config.networks[kind] as OneBotNetworks[K];
|
||||
const next = list.map((it, i) =>
|
||||
i === index ? ({ ...it, enabled: enabled ? undefined : false } as OneBotNetworks[K][number]) : it,
|
||||
) as OneBotNetworks[K];
|
||||
updateKind(kind, next);
|
||||
commitKind(
|
||||
kind,
|
||||
list.map((it, i) =>
|
||||
i === index ? ({ ...it, enabled: enabled ? undefined : false } as OneBotNetworks[K][number]) : it,
|
||||
) as OneBotNetworks[K],
|
||||
);
|
||||
}
|
||||
|
||||
const openCreate = (kind: NetworkKind) => {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { useState } from 'react';
|
||||
import { motion } from 'motion/react';
|
||||
import { Check, KeyRound, Loader2, Monitor, Moon, Sun } from 'lucide-react';
|
||||
import { Check, KeyRound, Monitor, Moon, Palette, RefreshCw, ShieldCheck, Sun } 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';
|
||||
@@ -14,8 +14,7 @@ import {
|
||||
type Density,
|
||||
type ThemeMode,
|
||||
} from '@/contexts/ThemeContext';
|
||||
import { ChangePasswordPage } from '@/components/pages/change-password-page';
|
||||
import { useApi } from '@/lib/api';
|
||||
import { ChangePasswordDialog } from '@/components/change-password-dialog';
|
||||
import { useAppState } from '@/contexts/AppStateContext';
|
||||
import { cn } from '@/lib/utils';
|
||||
|
||||
@@ -30,233 +29,279 @@ const DENSITY_OPTIONS: { value: Density; label: string; description: string }[]
|
||||
{ value: 'compact', label: '紧凑', description: '更小的字号与行距,单屏放更多内容' },
|
||||
];
|
||||
|
||||
type SettingsTab = 'appearance' | 'data' | 'account';
|
||||
|
||||
const TABS: { key: SettingsTab; label: string; icon: typeof Sun }[] = [
|
||||
{ key: 'appearance', label: '外观', icon: Palette },
|
||||
{ key: 'data', label: '数据刷新', icon: RefreshCw },
|
||||
{ key: 'account', label: '账号安全', icon: ShieldCheck },
|
||||
];
|
||||
|
||||
export function SettingsPage() {
|
||||
const api = useApi();
|
||||
const { onLogout } = useAppState();
|
||||
const {
|
||||
mode, setMode,
|
||||
accent, setAccent,
|
||||
radius, setRadius,
|
||||
density, setDensity,
|
||||
pollInterval, setPollInterval,
|
||||
} = useTheme();
|
||||
|
||||
const [showChangePwd, setShowChangePwd] = useState(false);
|
||||
const [pwdSavedAt, setPwdSavedAt] = useState<number | null>(null);
|
||||
|
||||
if (showChangePwd) {
|
||||
return (
|
||||
<ChangePasswordPage
|
||||
forced={false}
|
||||
checkStrength={(p) => api.checkPasswordStrength(p)}
|
||||
submit={(o, n) => api.changePassword(o, n)}
|
||||
onSuccess={() => {
|
||||
setShowChangePwd(false);
|
||||
setPwdSavedAt(Date.now());
|
||||
}}
|
||||
onCancel={() => setShowChangePwd(false)}
|
||||
/>
|
||||
);
|
||||
}
|
||||
const [tab, setTab] = useState<SettingsTab>('appearance');
|
||||
|
||||
return (
|
||||
<div className="flex flex-col gap-6">
|
||||
{/* Appearance */}
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>外观</CardTitle>
|
||||
<CardDescription>调整界面的明暗模式、强调色、圆角与密度。所有改动会立即应用并保存到本地。</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent className="flex flex-col gap-6">
|
||||
{/* Mode */}
|
||||
<div className="flex flex-col gap-2">
|
||||
<Label>显示模式</Label>
|
||||
<div className="flex flex-wrap gap-2">
|
||||
{MODE_OPTIONS.map((opt) => {
|
||||
const Icon = opt.icon;
|
||||
const active = mode === opt.value;
|
||||
return (
|
||||
<button
|
||||
key={opt.value}
|
||||
type="button"
|
||||
onClick={() => setMode(opt.value)}
|
||||
className={cn(
|
||||
'flex items-center gap-2 rounded-lg border px-3 py-2 text-sm transition-colors cursor-pointer',
|
||||
active
|
||||
? 'border-primary bg-primary/10 text-primary'
|
||||
: 'border-border bg-background hover:bg-accent/40',
|
||||
)}
|
||||
>
|
||||
<Icon className="size-4" />
|
||||
{opt.label}
|
||||
</button>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex flex-col gap-5">
|
||||
<MiniTabs tab={tab} onChange={setTab} />
|
||||
|
||||
<Separator />
|
||||
|
||||
{/* Accent color */}
|
||||
<div className="flex flex-col gap-2">
|
||||
<Label>强调色</Label>
|
||||
<div className="flex flex-wrap gap-2">
|
||||
{ACCENTS.map((spec) => {
|
||||
const active = accent === spec.id;
|
||||
return (
|
||||
<button
|
||||
key={spec.id}
|
||||
type="button"
|
||||
onClick={() => setAccent(spec.id as AccentColor)}
|
||||
title={spec.label}
|
||||
aria-label={spec.label}
|
||||
className={cn(
|
||||
'group relative flex h-9 w-9 items-center justify-center rounded-full border transition-transform cursor-pointer hover:scale-105',
|
||||
active ? 'border-foreground/30 ring-2 ring-primary' : 'border-border',
|
||||
)}
|
||||
style={{ backgroundColor: spec.swatch }}
|
||||
>
|
||||
{active && (
|
||||
<motion.span
|
||||
initial={{ scale: 0.4, opacity: 0 }}
|
||||
animate={{ scale: 1, opacity: 1 }}
|
||||
className="text-white drop-shadow-sm"
|
||||
>
|
||||
<Check className="size-4" strokeWidth={3} />
|
||||
</motion.span>
|
||||
)}
|
||||
</button>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
<p className="text-[11px] text-muted-foreground">当前:{ACCENTS.find((a) => a.id === accent)?.label}</p>
|
||||
</div>
|
||||
|
||||
<Separator />
|
||||
|
||||
{/* Radius */}
|
||||
<div className="flex flex-col gap-2">
|
||||
<Label>圆角</Label>
|
||||
<div className="flex flex-wrap gap-2">
|
||||
{RADIUS_OPTIONS.map((opt) => {
|
||||
const active = Math.abs(radius - opt.value) < 1e-6;
|
||||
return (
|
||||
<button
|
||||
key={opt.value}
|
||||
type="button"
|
||||
onClick={() => setRadius(opt.value)}
|
||||
className={cn(
|
||||
'rounded-md border px-3 py-2 text-sm transition-colors cursor-pointer',
|
||||
active
|
||||
? 'border-primary bg-primary/10 text-primary'
|
||||
: 'border-border bg-background hover:bg-accent/40',
|
||||
)}
|
||||
style={{ borderRadius: `${opt.value}rem` }}
|
||||
>
|
||||
{opt.label}({opt.value}rem)
|
||||
</button>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<Separator />
|
||||
|
||||
{/* Density */}
|
||||
<div className="flex flex-col gap-2">
|
||||
<Label>显示密度</Label>
|
||||
<div className="grid gap-2 sm:grid-cols-2">
|
||||
{DENSITY_OPTIONS.map((opt) => {
|
||||
const active = density === opt.value;
|
||||
return (
|
||||
<button
|
||||
key={opt.value}
|
||||
type="button"
|
||||
onClick={() => setDensity(opt.value)}
|
||||
className={cn(
|
||||
'flex flex-col items-start gap-1 rounded-lg border p-3 text-left transition-colors cursor-pointer',
|
||||
active
|
||||
? 'border-primary bg-primary/10'
|
||||
: 'border-border bg-background hover:bg-accent/40',
|
||||
)}
|
||||
>
|
||||
<span className="text-sm font-medium">{opt.label}</span>
|
||||
<span className="text-[11px] text-muted-foreground">{opt.description}</span>
|
||||
</button>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
{/* Behavior */}
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>数据刷新</CardTitle>
|
||||
<CardDescription>控制总览页轮询刷新 QQ 列表、进程列表与系统状态的间隔。</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<div className="flex flex-col gap-2">
|
||||
<Label>轮询间隔</Label>
|
||||
<div className="flex flex-wrap gap-2">
|
||||
{POLL_INTERVAL_OPTIONS.map((opt) => {
|
||||
const active = pollInterval === opt.value;
|
||||
return (
|
||||
<button
|
||||
key={opt.value}
|
||||
type="button"
|
||||
onClick={() => setPollInterval(opt.value)}
|
||||
className={cn(
|
||||
'rounded-md border px-3 py-2 text-sm transition-colors cursor-pointer',
|
||||
active
|
||||
? 'border-primary bg-primary/10 text-primary'
|
||||
: 'border-border bg-background hover:bg-accent/40',
|
||||
)}
|
||||
>
|
||||
{opt.label}
|
||||
</button>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
<p className="text-[11px] text-muted-foreground">当前生效值:{pollInterval === 0 ? '已暂停轮询' : `${pollInterval} 毫秒`}</p>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
{/* Account */}
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>账号安全</CardTitle>
|
||||
<CardDescription>WebUI 仅有 admin 一个账号,密码会以 scrypt 哈希形式持久化到 config/webui.json。</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent className="flex flex-col gap-3">
|
||||
<Button
|
||||
variant="outline"
|
||||
onClick={() => setShowChangePwd(true)}
|
||||
className="w-fit"
|
||||
>
|
||||
<KeyRound className="size-4" />
|
||||
修改访问密码
|
||||
</Button>
|
||||
{pwdSavedAt && (
|
||||
<span className="text-[11px] text-success">
|
||||
密码已更新({new Date(pwdSavedAt).toLocaleTimeString()})— 其他设备的会话已失效。
|
||||
</span>
|
||||
)}
|
||||
|
||||
<Separator />
|
||||
|
||||
<div>
|
||||
<p className="text-sm font-medium">退出登录</p>
|
||||
<p className="mt-1 text-[11px] text-muted-foreground">立即清除当前浏览器的会话令牌。</p>
|
||||
<Button variant="ghost" onClick={onLogout} className="mt-2 text-destructive hover:text-destructive">
|
||||
<Loader2 className="size-4 hidden" />
|
||||
退出登录
|
||||
</Button>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
{tab === 'appearance' && <AppearancePanel />}
|
||||
{tab === 'data' && <DataPanel />}
|
||||
{tab === 'account' && <AccountPanel />}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// ─────────────── mini tab strip ───────────────
|
||||
|
||||
function MiniTabs({ tab, onChange }: { tab: SettingsTab; onChange: (t: SettingsTab) => void }) {
|
||||
return (
|
||||
<div className="inline-flex w-fit items-center gap-1 rounded-lg border bg-muted/40 p-1">
|
||||
{TABS.map((t) => {
|
||||
const Icon = t.icon;
|
||||
const active = tab === t.key;
|
||||
return (
|
||||
<button
|
||||
key={t.key}
|
||||
type="button"
|
||||
onClick={() => onChange(t.key)}
|
||||
className={cn(
|
||||
'relative inline-flex items-center gap-1.5 rounded-md px-3 py-1.5 text-sm transition-colors cursor-pointer',
|
||||
active ? 'text-foreground' : 'text-muted-foreground hover:text-foreground',
|
||||
)}
|
||||
>
|
||||
{active && (
|
||||
<motion.span
|
||||
layoutId="settings-tab-pill"
|
||||
className="absolute inset-0 rounded-md bg-background shadow-sm"
|
||||
transition={{ type: 'spring', stiffness: 350, damping: 30 }}
|
||||
/>
|
||||
)}
|
||||
<span className="relative z-10 inline-flex items-center gap-1.5">
|
||||
<Icon className="size-4" />
|
||||
{t.label}
|
||||
</span>
|
||||
</button>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// ─────────────── 外观 ───────────────
|
||||
|
||||
function AppearancePanel() {
|
||||
const { mode, setMode, accent, setAccent, radius, setRadius, density, setDensity } = useTheme();
|
||||
return (
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>外观</CardTitle>
|
||||
<CardDescription>调整界面的明暗模式、强调色、圆角与密度。所有改动会立即应用并保存到本地。</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent className="flex flex-col gap-6">
|
||||
{/* Mode */}
|
||||
<div className="flex flex-col gap-2">
|
||||
<Label>显示模式</Label>
|
||||
<div className="flex flex-wrap gap-2">
|
||||
{MODE_OPTIONS.map((opt) => {
|
||||
const Icon = opt.icon;
|
||||
const active = mode === opt.value;
|
||||
return (
|
||||
<button
|
||||
key={opt.value}
|
||||
type="button"
|
||||
onClick={() => setMode(opt.value)}
|
||||
className={cn(
|
||||
'flex items-center gap-2 rounded-lg border px-3 py-2 text-sm transition-colors cursor-pointer',
|
||||
active
|
||||
? 'border-primary bg-primary/10 text-primary'
|
||||
: 'border-border bg-background hover:bg-accent/40',
|
||||
)}
|
||||
>
|
||||
<Icon className="size-4" />
|
||||
{opt.label}
|
||||
</button>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<Separator />
|
||||
|
||||
{/* Accent color */}
|
||||
<div className="flex flex-col gap-2">
|
||||
<Label>强调色</Label>
|
||||
<div className="flex flex-wrap gap-2">
|
||||
{ACCENTS.map((spec) => {
|
||||
const active = accent === spec.id;
|
||||
return (
|
||||
<button
|
||||
key={spec.id}
|
||||
type="button"
|
||||
onClick={() => setAccent(spec.id as AccentColor)}
|
||||
title={spec.label}
|
||||
aria-label={spec.label}
|
||||
className={cn(
|
||||
'group relative flex h-9 w-9 items-center justify-center rounded-full border transition-transform cursor-pointer hover:scale-105',
|
||||
active ? 'border-foreground/30 ring-2 ring-primary' : 'border-border',
|
||||
)}
|
||||
style={{ backgroundColor: spec.swatch }}
|
||||
>
|
||||
{active && (
|
||||
<motion.span
|
||||
initial={{ scale: 0.4, opacity: 0 }}
|
||||
animate={{ scale: 1, opacity: 1 }}
|
||||
className="text-white drop-shadow-sm"
|
||||
>
|
||||
<Check className="size-4" strokeWidth={3} />
|
||||
</motion.span>
|
||||
)}
|
||||
</button>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
<p className="text-[11px] text-muted-foreground">当前:{ACCENTS.find((a) => a.id === accent)?.label}</p>
|
||||
</div>
|
||||
|
||||
<Separator />
|
||||
|
||||
{/* Radius */}
|
||||
<div className="flex flex-col gap-2">
|
||||
<Label>圆角</Label>
|
||||
<div className="flex flex-wrap gap-2">
|
||||
{RADIUS_OPTIONS.map((opt) => {
|
||||
const active = Math.abs(radius - opt.value) < 1e-6;
|
||||
return (
|
||||
<button
|
||||
key={opt.value}
|
||||
type="button"
|
||||
onClick={() => setRadius(opt.value)}
|
||||
className={cn(
|
||||
'rounded-md border px-3 py-2 text-sm transition-colors cursor-pointer',
|
||||
active
|
||||
? 'border-primary bg-primary/10 text-primary'
|
||||
: 'border-border bg-background hover:bg-accent/40',
|
||||
)}
|
||||
style={{ borderRadius: `${opt.value}rem` }}
|
||||
>
|
||||
{opt.label}({opt.value}rem)
|
||||
</button>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<Separator />
|
||||
|
||||
{/* Density */}
|
||||
<div className="flex flex-col gap-2">
|
||||
<Label>显示密度</Label>
|
||||
<div className="grid gap-2 sm:grid-cols-2">
|
||||
{DENSITY_OPTIONS.map((opt) => {
|
||||
const active = density === opt.value;
|
||||
return (
|
||||
<button
|
||||
key={opt.value}
|
||||
type="button"
|
||||
onClick={() => setDensity(opt.value)}
|
||||
className={cn(
|
||||
'flex flex-col items-start gap-1 rounded-lg border p-3 text-left transition-colors cursor-pointer',
|
||||
active ? 'border-primary bg-primary/10' : 'border-border bg-background hover:bg-accent/40',
|
||||
)}
|
||||
>
|
||||
<span className="text-sm font-medium">{opt.label}</span>
|
||||
<span className="text-[11px] text-muted-foreground">{opt.description}</span>
|
||||
</button>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
);
|
||||
}
|
||||
|
||||
// ─────────────── 数据刷新 ───────────────
|
||||
|
||||
function DataPanel() {
|
||||
const { pollInterval, setPollInterval } = useTheme();
|
||||
return (
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>数据刷新</CardTitle>
|
||||
<CardDescription>控制总览页轮询刷新 QQ 列表、进程列表与系统状态的间隔。</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<div className="flex flex-col gap-2">
|
||||
<Label>轮询间隔</Label>
|
||||
<div className="flex flex-wrap gap-2">
|
||||
{POLL_INTERVAL_OPTIONS.map((opt) => {
|
||||
const active = pollInterval === opt.value;
|
||||
return (
|
||||
<button
|
||||
key={opt.value}
|
||||
type="button"
|
||||
onClick={() => setPollInterval(opt.value)}
|
||||
className={cn(
|
||||
'rounded-md border px-3 py-2 text-sm transition-colors cursor-pointer',
|
||||
active
|
||||
? 'border-primary bg-primary/10 text-primary'
|
||||
: 'border-border bg-background hover:bg-accent/40',
|
||||
)}
|
||||
>
|
||||
{opt.label}
|
||||
</button>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
<p className="text-[11px] text-muted-foreground">
|
||||
当前生效值:{pollInterval === 0 ? '已暂停轮询' : `${pollInterval} 毫秒`}
|
||||
</p>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
);
|
||||
}
|
||||
|
||||
// ─────────────── 账号安全 ───────────────
|
||||
|
||||
function AccountPanel() {
|
||||
const { onLogout } = useAppState();
|
||||
const [showChangePwd, setShowChangePwd] = useState(false);
|
||||
const [pwdSavedAt, setPwdSavedAt] = useState<number | null>(null);
|
||||
|
||||
return (
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>账号安全</CardTitle>
|
||||
<CardDescription>WebUI 仅有 admin 一个账号,密码会以 scrypt 哈希形式持久化到 config/webui.json。</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent className="flex flex-col gap-3">
|
||||
<Button variant="outline" onClick={() => setShowChangePwd(true)} className="w-fit">
|
||||
<KeyRound className="size-4" />
|
||||
修改访问密码
|
||||
</Button>
|
||||
{pwdSavedAt && (
|
||||
<span className="text-[11px] text-success">
|
||||
密码已更新({new Date(pwdSavedAt).toLocaleTimeString()})— 其他设备的会话已失效。
|
||||
</span>
|
||||
)}
|
||||
|
||||
<Separator />
|
||||
|
||||
<div>
|
||||
<p className="text-sm font-medium">退出登录</p>
|
||||
<p className="mt-1 text-[11px] text-muted-foreground">立即清除当前浏览器的会话令牌。</p>
|
||||
<Button variant="ghost" onClick={onLogout} className="mt-2 text-destructive hover:text-destructive">
|
||||
退出登录
|
||||
</Button>
|
||||
</div>
|
||||
</CardContent>
|
||||
|
||||
<ChangePasswordDialog
|
||||
open={showChangePwd}
|
||||
onOpenChange={setShowChangePwd}
|
||||
onSuccess={() => setPwdSavedAt(Date.now())}
|
||||
/>
|
||||
</Card>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
import type { ReactNode } from 'react';
|
||||
import { motion } from 'motion/react';
|
||||
import { AlertTriangle, Compass, Home, RotateCw } from 'lucide-react';
|
||||
import { Button } from '@/components/ui/button';
|
||||
|
||||
type Tone = 'primary' | 'destructive';
|
||||
|
||||
function StatusShell({
|
||||
tone,
|
||||
icon,
|
||||
code,
|
||||
title,
|
||||
description,
|
||||
children,
|
||||
}: {
|
||||
tone: Tone;
|
||||
icon: ReactNode;
|
||||
code?: string;
|
||||
title: string;
|
||||
description: string;
|
||||
children: ReactNode;
|
||||
}) {
|
||||
return (
|
||||
<div className="flex min-h-[60vh] w-full flex-col items-center justify-center px-4 text-center">
|
||||
<motion.div
|
||||
initial={{ opacity: 0, y: 16, scale: 0.98 }}
|
||||
animate={{ opacity: 1, y: 0, scale: 1 }}
|
||||
transition={{ duration: 0.4, ease: [0.16, 1, 0.3, 1] }}
|
||||
className="flex w-full max-w-md flex-col items-center"
|
||||
>
|
||||
<div
|
||||
className={
|
||||
tone === 'destructive'
|
||||
? 'flex size-14 items-center justify-center rounded-2xl bg-destructive/10 ring-1 ring-destructive/20'
|
||||
: 'flex size-14 items-center justify-center rounded-2xl bg-primary/10 ring-1 ring-primary/20'
|
||||
}
|
||||
>
|
||||
{icon}
|
||||
</div>
|
||||
{code && (
|
||||
<span className="mt-4 font-mono text-4xl font-bold tracking-tight tabular-nums text-muted-foreground/60">
|
||||
{code}
|
||||
</span>
|
||||
)}
|
||||
<h1 className="mt-2 text-xl font-semibold tracking-tight">{title}</h1>
|
||||
<p className="mt-2 text-sm leading-relaxed text-muted-foreground">{description}</p>
|
||||
<div className="mt-6 flex flex-wrap items-center justify-center gap-2">{children}</div>
|
||||
</motion.div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
/** Rendered by the router for any unmatched path. */
|
||||
export function NotFoundPage() {
|
||||
return (
|
||||
<StatusShell
|
||||
tone="primary"
|
||||
icon={<Compass className="size-7 text-primary" strokeWidth={1.75} />}
|
||||
code="404"
|
||||
title="页面不存在"
|
||||
description="你访问的地址没有对应的页面,可能链接已失效或输入有误。"
|
||||
>
|
||||
<Button onClick={() => window.location.assign('/')}>
|
||||
<Home className="size-4" /> 返回首页
|
||||
</Button>
|
||||
</StatusShell>
|
||||
);
|
||||
}
|
||||
|
||||
/** Rendered by the router when a route component throws while rendering. */
|
||||
export function ErrorPage({ error, reset }: { error?: Error; reset?: () => void }) {
|
||||
const message = error?.message?.trim();
|
||||
return (
|
||||
<StatusShell
|
||||
tone="destructive"
|
||||
icon={<AlertTriangle className="size-7 text-destructive" strokeWidth={1.75} />}
|
||||
title="页面出错了"
|
||||
description="渲染该页面时发生了意外错误。可以重试,或返回首页继续操作。"
|
||||
>
|
||||
{reset && (
|
||||
<Button variant="outline" onClick={reset}>
|
||||
<RotateCw className="size-4" /> 重试
|
||||
</Button>
|
||||
)}
|
||||
<Button onClick={() => window.location.assign('/')}>
|
||||
<Home className="size-4" /> 返回首页
|
||||
</Button>
|
||||
{message && (
|
||||
<pre className="mt-3 max-h-32 w-full overflow-auto rounded-md border bg-muted/40 px-3 py-2 text-left text-[11px] leading-relaxed text-muted-foreground whitespace-pre-wrap">
|
||||
{message}
|
||||
</pre>
|
||||
)}
|
||||
</StatusShell>
|
||||
);
|
||||
}
|
||||
@@ -25,7 +25,13 @@ export interface UseOneBotInstanceConfig {
|
||||
pendingSwitchUin: string | null;
|
||||
confirmSwitch: () => void;
|
||||
cancelSwitch: () => void;
|
||||
save: () => Promise<void>;
|
||||
/**
|
||||
* Persist to the backend. Pass an explicit config to save it directly —
|
||||
* the node dialog / enable-toggle / delete persist their freshly-computed
|
||||
* config in the same tick, before React has flushed `setConfig`, so they
|
||||
* can't rely on the (still-stale) closed-over `config`.
|
||||
*/
|
||||
save: (override?: OneBotConfig) => Promise<void>;
|
||||
saveStatus: string;
|
||||
}
|
||||
|
||||
@@ -117,11 +123,12 @@ export function useOneBotInstanceConfig(
|
||||
}, CLEAR_SAVE_STATUS_MS);
|
||||
}, []);
|
||||
|
||||
const save = useCallback(async () => {
|
||||
if (!selectedUin || !config) return;
|
||||
const save = useCallback(async (override?: OneBotConfig) => {
|
||||
const target = override ?? config;
|
||||
if (!selectedUin || !target) return;
|
||||
setSaveStatus('保存中...');
|
||||
try {
|
||||
const serverView = await api.config.save(selectedUin, config);
|
||||
const serverView = await api.config.save(selectedUin, target);
|
||||
setConfigState(serverView);
|
||||
setSavedSnapshot(JSON.stringify(serverView));
|
||||
setSaveStatus('保存成功');
|
||||
|
||||
@@ -6,6 +6,9 @@ import {
|
||||
Outlet,
|
||||
} from '@tanstack/react-router';
|
||||
import { AppLayout } from './app-layout';
|
||||
// Imported eagerly (not lazy): the error / not-found fallbacks must render
|
||||
// even when a route's own chunk failed to load.
|
||||
import { ErrorPage, NotFoundPage } from '@/components/pages/status-screens';
|
||||
|
||||
// Page components are loaded on demand so the initial paint only ships
|
||||
// the auth surface + layout shell. With `defaultPreload: 'intent'` set
|
||||
@@ -68,6 +71,8 @@ const routeTree = rootRoute.addChildren([
|
||||
export const appRouter = createRouter({
|
||||
routeTree,
|
||||
defaultPreload: 'intent',
|
||||
defaultNotFoundComponent: () => <NotFoundPage />,
|
||||
defaultErrorComponent: ({ error, reset }) => <ErrorPage error={error} reset={reset} />,
|
||||
});
|
||||
|
||||
declare module '@tanstack/react-router' {
|
||||
|
||||
Reference in new issue
Block a user