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

@@ -6,10 +6,12 @@
// Scales to many channels (40+): collapsed by default to a one-line summary of
// selected channels as removable chips; expanded into a searchable, bulk-
// actionable, height-bounded scroll list so it never blows up the page.
import { useEffect, useMemo, useState } from 'react';
import { useCallback, useEffect, useMemo, useState } from 'react';
import { AnimatePresence, motion } from 'motion/react';
import { ChevronDown, Loader2, Search, X } from 'lucide-react';
import { ChevronDown, Search, X } 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 { ScrollArea } from '@/components/ui/scroll-area';
@@ -29,24 +31,27 @@ const CHIP_CAP = 8;
export function NotificationOptIn({ selectedIds, onChange }: NotificationOptInProps) {
const api = useApi();
const [channels, setChannels] = useState<NotificationChannel[] | null>(null);
const [loadError, setLoadError] = useState<string | null>(null);
const [expanded, setExpanded] = useState(false);
const [query, setQuery] = useState('');
useEffect(() => {
let cancelled = false;
void (async () => {
try {
const cfg = await api.notifications.getConfig();
if (!cancelled) setChannels(cfg.channels);
} catch {
if (!cancelled) setChannels([]);
}
})();
return () => {
cancelled = true;
};
const load = useCallback(async () => {
setChannels(null);
setLoadError(null);
try {
const cfg = await api.notifications.getConfig();
setChannels(cfg.channels);
} catch (error) {
console.error('load notification opt-ins failed', error);
setLoadError(error instanceof Error ? error.message : '加载通知渠道失败');
setChannels([]);
}
}, [api]);
useEffect(() => {
void load();
}, [load]);
const selected = useMemo(() => new Set(selectedIds), [selectedIds]);
const toggle = (id: string, on: boolean) => {
@@ -105,124 +110,136 @@ export function NotificationOptIn({ selectedIds, onChange }: NotificationOptInPr
/>
</button>
{channels === null ? (
<div className="flex items-center gap-2 text-xs text-muted-foreground">
<Loader2 className="size-3.5 animate-spin" /> 加载渠道…
</div>
) : total === 0 ? (
<p className="text-xs text-muted-foreground">还没有全局渠道——先到「设置 → 通知」添加。</p>
) : (
<>
{/* collapsed: selected channels as removable chips */}
{!expanded &&
(selectedCount === 0 ? (
<p className="text-xs text-muted-foreground">未选择任何渠道。展开以选择。</p>
) : (
<div className="flex flex-wrap gap-1.5">
{selectedChips.slice(0, CHIP_CAP).map((ch) => (
<Badge key={ch.id} variant="secondary" className="max-w-[12rem] pr-1">
<span className="truncate">{ch.name}</span>
<button
type="button"
onClick={() => toggle(ch.id, false)}
aria-label={`移除 ${ch.name}`}
className="ml-0.5 rounded-full p-0.5 text-muted-foreground transition-colors hover:bg-foreground/10 hover:text-foreground"
>
<X className="size-3" />
</button>
</Badge>
))}
{selectedCount > CHIP_CAP && (
<button
type="button"
onClick={() => setExpanded(true)}
className="rounded-full px-2 py-0.5 text-xs font-medium text-muted-foreground transition-colors hover:text-foreground"
>
+{selectedCount - CHIP_CAP} 更多
</button>
)}
</div>
))}
{/* expanded: search + bulk + bounded scroll list */}
<AnimatePresence initial={false}>
{expanded && (
<motion.div
initial={{ height: 0, opacity: 0 }}
animate={{ height: 'auto', opacity: 1 }}
exit={{ height: 0, opacity: 0 }}
transition={{ duration: 0.22 }}
className="overflow-hidden"
>
<div className="flex flex-col gap-3 pt-1">
<p className="text-xs leading-relaxed text-muted-foreground">
该账号上线 / 下线时向选中的渠道推送通知。渠道在「设置 → 通知」中全局定义。
</p>
<div className="flex items-center gap-2">
<div className="relative flex-1">
<Search className="pointer-events-none absolute left-2.5 top-1/2 size-3.5 -translate-y-1/2 text-muted-foreground" />
<Input
value={query}
onChange={(e) => setQuery(e.target.value)}
placeholder="搜索渠道…"
className="h-8 pl-8 text-xs"
/>
</div>
<button
type="button"
onClick={selectAll}
className="rounded-md px-2 py-1 text-xs font-medium text-muted-foreground transition-colors hover:bg-accent hover:text-foreground"
>
全选
</button>
<button
type="button"
onClick={clearAll}
className="rounded-md px-2 py-1 text-xs font-medium text-muted-foreground transition-colors hover:bg-accent hover:text-foreground"
>
清空
</button>
<SkeletonSwap
ready={channels !== null}
lines={2}
lineHeight={20}
reserve={40}
label="通知渠道"
className={channels !== null ? 'skeleton-swap-fluid min-h-[40px]' : ''}
>
{channels !== null ? (
loadError ? (
<div role="alert" className="flex items-center justify-between gap-3 text-xs text-destructive">
<span>{loadError}</span>
<Button variant="outline" size="sm" onClick={() => void load()}>重试</Button>
</div>
) : total === 0 ? (
<p className="text-xs text-muted-foreground">还没有全局渠道——先到「设置 → 通知」添加。</p>
) : (
<>
{/* collapsed: selected channels as removable chips */}
{!expanded &&
(selectedCount === 0 ? (
<p className="text-xs text-muted-foreground">未选择任何渠道。展开以选择。</p>
) : (
<div className="flex flex-wrap gap-1.5">
{selectedChips.slice(0, CHIP_CAP).map((ch) => (
<Badge key={ch.id} variant="secondary" className="max-w-[12rem] pr-1">
<span className="truncate">{ch.name}</span>
<button
type="button"
onClick={() => toggle(ch.id, false)}
aria-label={`移除 ${ch.name}`}
className="ml-0.5 rounded-full p-0.5 text-muted-foreground transition-colors hover:bg-foreground/10 hover:text-foreground"
>
<X className="size-3" />
</button>
</Badge>
))}
{selectedCount > CHIP_CAP && (
<button
type="button"
onClick={() => setExpanded(true)}
className="rounded-full px-2 py-0.5 text-xs font-medium text-muted-foreground transition-colors hover:text-foreground"
>
+{selectedCount - CHIP_CAP} 更多
</button>
)}
</div>
))}
{filtered.length === 0 ? (
<p className="py-4 text-center text-xs text-muted-foreground">无匹配渠道</p>
) : (
<ScrollArea className="max-h-64 rounded-lg border bg-background/40">
<div className="divide-y divide-border/60">
{filtered.map((ch) => (
<div
key={ch.id}
className="flex items-center justify-between gap-3 px-3 py-2"
>
<div className="min-w-0">
<div className="flex items-center gap-1.5">
<span className="truncate text-sm">{ch.name || ch.id}</span>
{!ch.enabled && (
<span className="shrink-0 text-micro text-muted-foreground">(已全局禁用)</span>
)}
</div>
<p className="truncate text-meta text-muted-foreground">{ch.url}</p>
</div>
<ToggleSwitch
// Globally-disabled channel: lock the switch and force it off
// (it can never fire, so the per-account opt-in must read off).
value={ch.enabled && selected.has(ch.id)}
onChange={(v) => toggle(ch.id, v)}
ariaLabel={`为该账号启用渠道 ${ch.name || ch.id}`}
disabled={!ch.enabled}
/>
</div>
))}
{/* expanded: search + bulk + bounded scroll list */}
<AnimatePresence initial={false}>
{expanded && (
<motion.div
initial={{ height: 0, opacity: 0 }}
animate={{ height: 'auto', opacity: 1 }}
exit={{ height: 0, opacity: 0 }}
transition={{ duration: 0.22 }}
className="overflow-hidden"
>
<div className="flex flex-col gap-3 pt-1">
<p className="text-xs leading-relaxed text-muted-foreground">
该账号上线 / 下线时向选中的渠道推送通知。渠道在「设置 → 通知」中全局定义。
</p>
<div className="flex items-center gap-2">
<div className="relative flex-1">
<Search className="pointer-events-none absolute left-2.5 top-1/2 size-3.5 -translate-y-1/2 text-muted-foreground" />
<Input
value={query}
onChange={(e) => setQuery(e.target.value)}
placeholder="搜索渠道…"
className="h-8 pl-8 text-xs"
/>
</div>
<button
type="button"
onClick={selectAll}
className="rounded-md px-2 py-1 text-xs font-medium text-muted-foreground transition-colors hover:bg-accent hover:text-foreground"
>
全选
</button>
<button
type="button"
onClick={clearAll}
className="rounded-md px-2 py-1 text-xs font-medium text-muted-foreground transition-colors hover:bg-accent hover:text-foreground"
>
清空
</button>
</div>
</ScrollArea>
)}
</div>
</motion.div>
)}
</AnimatePresence>
</>
)}
{filtered.length === 0 ? (
<p className="py-4 text-center text-xs text-muted-foreground">无匹配渠道</p>
) : (
<ScrollArea className="max-h-64 rounded-lg border bg-background/40">
<div className="divide-y divide-border/60">
{filtered.map((ch) => (
<div
key={ch.id}
className="flex items-center justify-between gap-3 px-3 py-2"
>
<div className="min-w-0">
<div className="flex items-center gap-1.5">
<span className="truncate text-sm">{ch.name || ch.id}</span>
{!ch.enabled && (
<span className="shrink-0 text-micro text-muted-foreground">(已全局禁用)</span>
)}
</div>
<p className="truncate text-meta text-muted-foreground">{ch.url}</p>
</div>
<ToggleSwitch
// Globally-disabled channel: lock the switch and force it off
// (it can never fire, so the per-account opt-in must read off).
value={ch.enabled && selected.has(ch.id)}
onChange={(v) => toggle(ch.id, v)}
ariaLabel={`为该账号启用渠道 ${ch.name || ch.id}`}
disabled={!ch.enabled}
/>
</div>
))}
</div>
</ScrollArea>
)}
</div>
</motion.div>
)}
</AnimatePresence>
</>
)
) : null}
</SkeletonSwap>
</div>
);
}