feat(webui): standardize content loading states

Use consistent delayed placeholders across data-backed console surfaces.
Avoid flashing loading UI for fast local responses while keeping slower transitions stable.
Keep failures visible and prevent gated content from opening on incomplete checks.
This commit is contained in:
DrSmoothl committed 2026-07-31 16:56:44 +08:00
1 parent f159b2f41a
commit 3c13838867
22 files changed
+2493 -1527

No files matched your search

@@ -2,11 +2,12 @@
// info-only cards (name / url / template + an inline enable toggle); creating
// and editing always go through a dialog. All changes auto-save (debounced),
// like the per-account config page — no explicit save button.
import { useEffect, useRef, useState } from 'react';
import { useCallback, useEffect, useRef, useState } from 'react';
import { motion } from 'motion/react';
import { Bell, Loader2, Pencil, Plus, Send, Trash2 } from 'lucide-react';
import { Badge } from '@/components/ui/badge';
import { Button } from '@/components/ui/button';
import { SkeletonSwap } from '@/components/interior/skeleton-swap';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { ToggleSwitch } from '@/components/ui/toggle-switch';
@@ -48,23 +49,22 @@ export function NotificationsPanel() {
const saveTimer = useRef<number | null>(null);
const saveGen = useRef(0);
useEffect(() => {
let cancelled = false;
void (async () => {
try {
const cfg = await api.notifications.getConfig();
if (!cancelled) setConfig(cfg);
} catch {
if (!cancelled) setMsg({ kind: 'err', text: '加载通知配置失败' });
} finally {
if (!cancelled) setLoading(false);
}
})();
return () => {
cancelled = true;
};
const load = useCallback(async () => {
setLoading(true);
try {
setConfig(await api.notifications.getConfig());
} catch (error) {
console.error('load notifications config failed', error);
setMsg({ kind: 'err', text: '加载通知配置失败' });
} finally {
setLoading(false);
}
}, [api, setMsg]);
useEffect(() => {
void load();
}, [load]);
useEffect(
() => () => {
if (saveTimer.current) clearTimeout(saveTimer.current);
@@ -111,9 +111,23 @@ export function NotificationsPanel() {
if (loading || !config) {
return (
<div className="flex items-center gap-2 rounded-lg border bg-card/40 p-6 text-sm text-muted-foreground">
<Loader2 className="size-4 animate-spin" /> 加载中…
</div>
<SkeletonSwap
ready={!loading}
lines={6}
lineHeight={26}
reserve={156}
label="通知配置"
className={!loading ? 'skeleton-swap-fluid min-h-[156px]' : ''}
>
{!loading && !config ? (
<div role="alert" className="flex items-center justify-between gap-3 rounded-lg border border-destructive/30 bg-destructive/10 p-4 text-sm text-destructive">
<span>{msg?.text ?? '加载通知配置失败'}</span>
<Button variant="outline" size="sm" onClick={() => void load()}>
重试
</Button>
</div>
) : null}
</SkeletonSwap>
);
}
@@ -172,89 +186,98 @@ export function NotificationsPanel() {
};
return (
<div className="flex flex-col gap-4">
{/* Global settings */}
<div className="flex flex-col gap-4 rounded-xl border bg-card/40 p-4">
<div className="flex items-start gap-2 text-xs leading-relaxed text-muted-foreground">
<Bell className="mt-0.5 size-3.5 shrink-0" />
<p>
<SkeletonSwap
ready
lines={6}
lineHeight={26}
reserve={156}
label="通知配置"
className="skeleton-swap-fluid min-h-[156px]"
>
<div className="flex flex-col gap-4">
{/* Global settings */}
<div className="flex flex-col gap-4 rounded-xl border bg-card/40 p-4">
<div className="flex items-start gap-2 text-xs leading-relaxed text-muted-foreground">
<Bell className="mt-0.5 size-3.5 shrink-0" />
<p>
账号上线 / 下线时向启用的渠道 POST 一条通知(机械转发,仅去抖防刷屏)。渠道在此全局定义,每个账号在其「配置」页勾选启用哪些。
</p>
</div>
<div className="flex flex-col gap-1.5 border-t pt-3">
<Label>去抖窗口(秒)</Label>
<Input
type="number"
min={0}
max={3600}
className="w-32 tabular-nums"
value={config.debounceSeconds}
onChange={(e) => {
const n = Math.trunc(Number(e.target.value));
commit({ ...config, debounceSeconds: Number.isFinite(n) ? Math.min(3600, Math.max(0, n)) : 0 });
}}
/>
<p className="text-xs leading-relaxed text-muted-foreground">
</p>
</div>
<div className="flex flex-col gap-1.5 border-t pt-3">
<Label>去抖窗口(秒)</Label>
<Input
type="number"
min={0}
max={3600}
className="w-32 tabular-nums"
value={config.debounceSeconds}
onChange={(e) => {
const n = Math.trunc(Number(e.target.value));
commit({ ...config, debounceSeconds: Number.isFinite(n) ? Math.min(3600, Math.max(0, n)) : 0 });
}}
/>
<p className="text-xs leading-relaxed text-muted-foreground">
下线后在该秒数内自愈则不发;超时才发「下线」,恢复时再发「上线」。<code className="font-mono">0</code> = 立即发、不去抖。
</p>
</p>
</div>
</div>
</div>
{/* Channels */}
<div className="flex flex-col gap-2">
<div className="flex items-center justify-between">
<h3 className="text-sm font-semibold tracking-tight">通知渠道</h3>
{channels.length > 0 && (
<Button variant="outline" size="sm" onClick={openCreate}>
<Plus className="size-3.5" /> 新增渠道
</Button>
{/* Channels */}
<div className="flex flex-col gap-2">
<div className="flex items-center justify-between">
<h3 className="text-sm font-semibold tracking-tight">通知渠道</h3>
{channels.length > 0 && (
<Button variant="outline" size="sm" onClick={openCreate}>
<Plus className="size-3.5" /> 新增渠道
</Button>
)}
</div>
{channels.length === 0 ? (
<div className="flex flex-col items-center justify-center gap-3 rounded-xl border border-dashed py-16 text-muted-foreground">
<Bell className="size-8 opacity-40" strokeWidth={1.5} />
<p className="text-sm">还没有通知渠道</p>
<Button variant="outline" size="sm" onClick={openCreate}>
<Plus className="size-3.5" /> 创建第一个
</Button>
</div>
) : (
<div className="flex flex-col gap-2">
{channels.map((ch, i) => (
<ChannelCard
key={ch.id}
channel={ch}
testing={testing === ch.id}
onToggle={(v) => setChannels(
channels.map((c, idx) => (idx === i ? { ...c, enabled: v } : c)),
{ title: '正在更新通知渠道', successTitle: '通知渠道已更新' },
)}
onTest={() => void test(ch.id)}
onEdit={() => openEdit(i)}
onDelete={() => setChannels(
channels.filter((_, idx) => idx !== i),
{ title: '正在删除通知渠道', successTitle: '通知渠道已删除' },
)}
/>
))}
</div>
)}
</div>
{channels.length === 0 ? (
<div className="flex flex-col items-center justify-center gap-3 rounded-xl border border-dashed py-16 text-muted-foreground">
<Bell className="size-8 opacity-40" strokeWidth={1.5} />
<p className="text-sm">还没有通知渠道</p>
<Button variant="outline" size="sm" onClick={openCreate}>
<Plus className="size-3.5" /> 创建第一个
</Button>
</div>
) : (
<div className="flex flex-col gap-2">
{channels.map((ch, i) => (
<ChannelCard
key={ch.id}
channel={ch}
testing={testing === ch.id}
onToggle={(v) => setChannels(
channels.map((c, idx) => (idx === i ? { ...c, enabled: v } : c)),
{ title: '正在更新通知渠道', successTitle: '通知渠道已更新' },
)}
onTest={() => void test(ch.id)}
onEdit={() => openEdit(i)}
onDelete={() => setChannels(
channels.filter((_, idx) => idx !== i),
{ title: '正在删除通知渠道', successTitle: '通知渠道已删除' },
)}
/>
))}
</div>
{msg && <p className={cn('text-xs', msg.kind === 'ok' ? 'text-success' : 'text-destructive')}>{msg.text}</p>}
{dialog.open && (
<NotificationChannelDialog
open={dialog.open}
onOpenChange={(open) => !open && setDialog((d) => ({ ...d, open: false }))}
isEdit={dialog.index != null}
initial={dialog.seed}
otherIds={otherIds(dialog.index)}
onSubmit={submitChannel}
/>
)}
</div>
{msg && <p className={cn('text-xs', msg.kind === 'ok' ? 'text-success' : 'text-destructive')}>{msg.text}</p>}
{dialog.open && (
<NotificationChannelDialog
open={dialog.open}
onOpenChange={(open) => !open && setDialog((d) => ({ ...d, open: false }))}
isEdit={dialog.index != null}
initial={dialog.seed}
otherIds={otherIds(dialog.index)}
onSubmit={submitChannel}
/>
)}
</div>
</SkeletonSwap>
);
}