feat(webui): improve operation feedback
Keep confirmations in dialogs while showing clear progress and results for long-running actions. Auto-close completed notices, preserve important outcomes across reloads, and avoid noise from routine edits.
This commit is contained in:
20 files changed
+1863
-222
No files matched your search
@@ -0,0 +1,356 @@
|
||||
import {
|
||||
createContext,
|
||||
useCallback,
|
||||
useContext,
|
||||
useEffect,
|
||||
useMemo,
|
||||
useRef,
|
||||
useState,
|
||||
type ReactNode,
|
||||
} from 'react';
|
||||
import { CircleAlert, CircleCheck } from 'lucide-react';
|
||||
import { motion } from 'motion/react';
|
||||
import {
|
||||
CollapsibleBanner,
|
||||
type BannerState,
|
||||
} from '@/components/interior/collapsible-banner';
|
||||
import {
|
||||
LiveActivity,
|
||||
useLiveActivity,
|
||||
type ActivityInput,
|
||||
} from '@/components/interior/live-activity';
|
||||
|
||||
type ResultDetail<T> = string | ((result: T) => string | undefined);
|
||||
type ErrorDetail = string | ((error: unknown) => string);
|
||||
|
||||
export interface ActionFeedbackOptions<T = void> extends ActivityInput {
|
||||
successTitle?: string;
|
||||
successDetail?: ResultDetail<T>;
|
||||
errorTitle?: string;
|
||||
errorDetail?: ErrorDetail;
|
||||
/** Re-show a successful result after an imminent full-page reload. */
|
||||
surviveReload?: boolean;
|
||||
/**
|
||||
* Some APIs resolve transport-successfully while reporting a failed domain
|
||||
* result. Return its user-facing reason here so the activity ends in error
|
||||
* without changing the caller's result/control flow.
|
||||
*/
|
||||
resultError?: (result: T) => string | null | undefined;
|
||||
}
|
||||
|
||||
export interface ActionFeedbackHandle {
|
||||
update: (patch: Partial<ActivityInput>) => void;
|
||||
succeed: (patch?: Partial<ActivityInput>) => void;
|
||||
fail: (detail: string, patch?: Partial<ActivityInput>) => void;
|
||||
}
|
||||
|
||||
interface ActionFeedbackValue {
|
||||
startAction: (input: ActivityInput) => ActionFeedbackHandle;
|
||||
runAction: <T>(options: ActionFeedbackOptions<T>, action: () => Promise<T> | T) => Promise<T>;
|
||||
activity: ReturnType<typeof useLiveActivity>['activity'];
|
||||
notices: ActionNotice[];
|
||||
removeNotice: (id: number) => void;
|
||||
dismiss: () => void;
|
||||
}
|
||||
|
||||
interface ActionNotice {
|
||||
id: number;
|
||||
tone: 'success' | 'error';
|
||||
title: string;
|
||||
detail?: string;
|
||||
}
|
||||
|
||||
const ActionFeedbackContext = createContext<ActionFeedbackValue | null>(null);
|
||||
const RESULT_NOTICE_DURATION_MS = 5_000;
|
||||
const RELOAD_NOTICE_MAX_AGE_MS = 30_000;
|
||||
const RELOAD_NOTICE_KEY = 'snowluma_action_feedback_reload_notice';
|
||||
|
||||
interface ReloadNotice extends Omit<ActionNotice, 'id'> {
|
||||
createdAt: number;
|
||||
}
|
||||
|
||||
function preserveReloadNotice(notice: Omit<ActionNotice, 'id'>): void {
|
||||
try {
|
||||
sessionStorage.setItem(
|
||||
RELOAD_NOTICE_KEY,
|
||||
JSON.stringify({ ...notice, createdAt: Date.now() } satisfies ReloadNotice),
|
||||
);
|
||||
} catch (error) {
|
||||
console.error('preserve action feedback across reload failed', error);
|
||||
}
|
||||
}
|
||||
|
||||
function takeReloadNotice(): Omit<ActionNotice, 'id'> | null {
|
||||
let raw: string | null;
|
||||
try {
|
||||
raw = sessionStorage.getItem(RELOAD_NOTICE_KEY);
|
||||
sessionStorage.removeItem(RELOAD_NOTICE_KEY);
|
||||
} catch (error) {
|
||||
console.error('read action feedback after reload failed', error);
|
||||
return null;
|
||||
}
|
||||
if (!raw) return null;
|
||||
try {
|
||||
const parsed = JSON.parse(raw) as Partial<ReloadNotice>;
|
||||
if (
|
||||
(parsed.tone !== 'success' && parsed.tone !== 'error')
|
||||
|| typeof parsed.title !== 'string'
|
||||
|| typeof parsed.createdAt !== 'number'
|
||||
|| Date.now() - parsed.createdAt > RELOAD_NOTICE_MAX_AGE_MS
|
||||
) {
|
||||
throw new Error('invalid or expired reload notice');
|
||||
}
|
||||
return {
|
||||
tone: parsed.tone,
|
||||
title: parsed.title,
|
||||
...(typeof parsed.detail === 'string' ? { detail: parsed.detail } : {}),
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('parse action feedback after reload failed', error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export function actionErrorMessage(error: unknown): string {
|
||||
if (error instanceof Error && error.message.trim()) return error.message;
|
||||
if (typeof error === 'string' && error.trim()) return error;
|
||||
return '未知错误';
|
||||
}
|
||||
|
||||
function resolveResultDetail<T>(detail: ResultDetail<T> | undefined, result: T): string | undefined {
|
||||
return typeof detail === 'function' ? detail(result) : detail;
|
||||
}
|
||||
|
||||
function resolveErrorDetail(detail: ErrorDetail | undefined, error: unknown): string {
|
||||
if (typeof detail === 'function') return detail(error);
|
||||
return detail ?? actionErrorMessage(error);
|
||||
}
|
||||
|
||||
export function publishActionResult(
|
||||
ownsSurface: boolean,
|
||||
updateCurrentActivity: () => void,
|
||||
publishNotice: () => void,
|
||||
): void {
|
||||
if (ownsSurface) updateCurrentActivity();
|
||||
publishNotice();
|
||||
}
|
||||
|
||||
export function scheduleActionNoticeDismiss(onDismiss: () => void): () => void {
|
||||
const timeout = setTimeout(onDismiss, RESULT_NOTICE_DURATION_MS);
|
||||
return () => clearTimeout(timeout);
|
||||
}
|
||||
|
||||
export function ActionFeedbackProvider({ children }: { children: ReactNode }) {
|
||||
const {
|
||||
activity,
|
||||
start,
|
||||
update,
|
||||
succeed,
|
||||
fail,
|
||||
dismiss: dismissActivity,
|
||||
} = useLiveActivity();
|
||||
const activeId = useRef<string | null>(null);
|
||||
const noticeId = useRef(0);
|
||||
const [notices, setNotices] = useState<ActionNotice[]>([]);
|
||||
|
||||
const pushNotice = useCallback((notice: Omit<ActionNotice, 'id'>) => {
|
||||
const next = { ...notice, id: ++noticeId.current };
|
||||
setNotices((current) => [...current.slice(-2), next]);
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
const notice = takeReloadNotice();
|
||||
if (notice) pushNotice(notice);
|
||||
}, [pushNotice]);
|
||||
|
||||
const startAction = useCallback((input: ActivityInput): ActionFeedbackHandle => {
|
||||
const id = start(input);
|
||||
activeId.current = id;
|
||||
const ownsSurface = () => activeId.current === id;
|
||||
|
||||
return {
|
||||
update: (patch) => {
|
||||
if (ownsSurface()) update(patch);
|
||||
},
|
||||
succeed: (patch) => {
|
||||
publishActionResult(
|
||||
ownsSurface(),
|
||||
() => succeed(patch),
|
||||
() => pushNotice({
|
||||
tone: 'success',
|
||||
title: patch?.title ?? input.title,
|
||||
detail: patch?.detail ?? input.detail,
|
||||
}),
|
||||
);
|
||||
},
|
||||
fail: (detail, patch) => {
|
||||
publishActionResult(
|
||||
ownsSurface(),
|
||||
() => fail({ ...patch, detail }),
|
||||
() => pushNotice({
|
||||
tone: 'error',
|
||||
title: patch?.title ?? input.title,
|
||||
detail,
|
||||
}),
|
||||
);
|
||||
},
|
||||
};
|
||||
}, [start, update, succeed, fail, pushNotice]);
|
||||
|
||||
const runAction = useCallback(async <T,>(
|
||||
options: ActionFeedbackOptions<T>,
|
||||
action: () => Promise<T> | T,
|
||||
): Promise<T> => {
|
||||
const {
|
||||
successTitle,
|
||||
successDetail,
|
||||
errorTitle,
|
||||
errorDetail,
|
||||
resultError,
|
||||
surviveReload,
|
||||
...input
|
||||
} = options;
|
||||
const handle = startAction(input);
|
||||
try {
|
||||
const result = await action();
|
||||
const failed = resultError?.(result);
|
||||
if (failed) {
|
||||
handle.fail(failed, errorTitle ? { title: errorTitle } : undefined);
|
||||
} else {
|
||||
const detail = resolveResultDetail(successDetail, result);
|
||||
if (surviveReload) {
|
||||
preserveReloadNotice({
|
||||
tone: 'success',
|
||||
title: successTitle ?? input.title,
|
||||
detail: detail ?? input.detail,
|
||||
});
|
||||
}
|
||||
handle.succeed({
|
||||
...(successTitle ? { title: successTitle } : {}),
|
||||
...(detail !== undefined ? { detail } : {}),
|
||||
});
|
||||
}
|
||||
return result;
|
||||
} catch (error) {
|
||||
handle.fail(resolveErrorDetail(errorDetail, error), errorTitle ? { title: errorTitle } : undefined);
|
||||
throw error;
|
||||
}
|
||||
}, [startAction]);
|
||||
|
||||
const dismiss = useCallback(() => {
|
||||
activeId.current = null;
|
||||
dismissActivity();
|
||||
}, [dismissActivity]);
|
||||
|
||||
const removeNotice = useCallback((id: number) => {
|
||||
setNotices((current) => current.filter((notice) => notice.id !== id));
|
||||
}, []);
|
||||
|
||||
const value = useMemo<ActionFeedbackValue>(
|
||||
() => ({ startAction, runAction, activity, notices, removeNotice, dismiss }),
|
||||
[startAction, runAction, activity, notices, removeNotice, dismiss],
|
||||
);
|
||||
|
||||
return (
|
||||
<ActionFeedbackContext.Provider value={value}>
|
||||
{children}
|
||||
</ActionFeedbackContext.Provider>
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Rendered inside ThemeProvider's MotionConfig so SnowLuma's existing reduced
|
||||
* motion / disable-motion preference also reaches Interior's component.
|
||||
*/
|
||||
export function ActionFeedbackViewport() {
|
||||
const {
|
||||
activity,
|
||||
notices,
|
||||
removeNotice,
|
||||
dismiss,
|
||||
} = useActionFeedback();
|
||||
const runningActivity = activity?.phase === 'running' ? activity : null;
|
||||
|
||||
return (
|
||||
<div className="pointer-events-none fixed bottom-4 right-4 z-[80] flex w-[min(360px,calc(100vw-2rem))] flex-col items-end gap-2">
|
||||
{notices.map((notice) => (
|
||||
<ActionNoticeBanner
|
||||
key={notice.id}
|
||||
notice={notice}
|
||||
onRemoved={() => removeNotice(notice.id)}
|
||||
/>
|
||||
))}
|
||||
<LiveActivity
|
||||
activity={runningActivity}
|
||||
onDismiss={dismiss}
|
||||
dismissLabel="关闭操作反馈"
|
||||
label="操作反馈"
|
||||
className="w-full"
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function ActionNoticeBanner({
|
||||
notice,
|
||||
onRemoved,
|
||||
}: {
|
||||
notice: ActionNotice;
|
||||
onRemoved: () => void;
|
||||
}) {
|
||||
const [state, setState] = useState<BannerState>('dismissed');
|
||||
const visible = state !== 'dismissed';
|
||||
|
||||
useEffect(() => {
|
||||
const frame = requestAnimationFrame(() => setState('open'));
|
||||
return () => cancelAnimationFrame(frame);
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
if (!visible) return;
|
||||
return scheduleActionNoticeDismiss(() => setState('dismissed'));
|
||||
}, [visible]);
|
||||
|
||||
return (
|
||||
<div className="pointer-events-auto relative w-full">
|
||||
<CollapsibleBanner
|
||||
state={state}
|
||||
onStateChange={setState}
|
||||
onDismissed={onRemoved}
|
||||
title={notice.title}
|
||||
description={notice.detail}
|
||||
icon={notice.tone === 'success'
|
||||
? <CircleCheck className="size-4 text-success" />
|
||||
: <CircleAlert className="size-4 text-destructive" />}
|
||||
dismissLabel="关闭通知"
|
||||
dismissedMessage="操作通知已关闭。"
|
||||
className={notice.tone === 'success'
|
||||
? 'border-success/25'
|
||||
: 'border-destructive/30'}
|
||||
/>
|
||||
{state !== 'dismissed' ? (
|
||||
<div
|
||||
aria-hidden="true"
|
||||
data-action-feedback-countdown-track=""
|
||||
className="pointer-events-none absolute inset-x-[8px] bottom-0.5 h-[3px] overflow-hidden rounded-full bg-border/45"
|
||||
>
|
||||
<motion.div
|
||||
data-action-feedback-countdown=""
|
||||
className={notice.tone === 'success'
|
||||
? 'h-full w-full origin-left rounded-full bg-success shadow-[0_0_5px_color-mix(in_oklab,var(--success)_45%,transparent)]'
|
||||
: 'h-full w-full origin-left rounded-full bg-destructive shadow-[0_0_5px_color-mix(in_oklab,var(--destructive)_45%,transparent)]'}
|
||||
initial={{ scaleX: 1 }}
|
||||
animate={{ scaleX: 0 }}
|
||||
transition={{ duration: RESULT_NOTICE_DURATION_MS / 1_000, ease: 'linear' }}
|
||||
/>
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export function useActionFeedback(): ActionFeedbackValue {
|
||||
const value = useContext(ActionFeedbackContext);
|
||||
if (!value) throw new Error('useActionFeedback must be used within ActionFeedbackProvider');
|
||||
return value;
|
||||
}
|
||||
@@ -22,7 +22,7 @@ export interface AppStateValue {
|
||||
refreshSystem: () => void;
|
||||
refreshConnections: () => void;
|
||||
/** Re-run the update check; `force` bypasses the server-side cache. */
|
||||
refreshUpdate: (force?: boolean) => Promise<void>;
|
||||
refreshUpdate: (force?: boolean) => Promise<UpdateInfo | null>;
|
||||
/** Triggered from the topbar logout button. */
|
||||
onLogout: () => void;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { createContext, useCallback, useContext, useEffect, useMemo, useRef, useState, type ReactNode } from 'react';
|
||||
import { useActionFeedback } from '@/contexts/ActionFeedbackContext';
|
||||
import { useApi } from '@/lib/api';
|
||||
import { defaultOverviewGrid, defaultOverviewMobile, migrateOverviewBlocks } from '@/lib/dashboard-layout';
|
||||
import type { UiLayout, UiLayoutItem, UiPages } from '@/types';
|
||||
@@ -104,6 +105,7 @@ const LayoutContext = createContext<LayoutContextValue | null>(null);
|
||||
|
||||
export function LayoutProvider({ children }: { children: ReactNode }) {
|
||||
const api = useApi();
|
||||
const { runAction } = useActionFeedback();
|
||||
const [layout, setLayout] = useState<UiLayout>(DEFAULT_LAYOUT);
|
||||
const [pages, setPagesState] = useState<UiPages>(DEFAULT_PAGES);
|
||||
const [ready, setReady] = useState(false);
|
||||
@@ -155,11 +157,15 @@ export function LayoutProvider({ children }: { children: ReactNode }) {
|
||||
useEffect(() => () => {
|
||||
if (saveTimer.current) {
|
||||
clearTimeout(saveTimer.current);
|
||||
void api.ui.save({ layout: layoutRef.current }).catch(() => { /* best-effort */ });
|
||||
void api.ui.save({ layout: layoutRef.current }).catch((error) => {
|
||||
console.error('layout unmount flush failed', error);
|
||||
});
|
||||
}
|
||||
if (pagesTimer.current) {
|
||||
clearTimeout(pagesTimer.current);
|
||||
void api.ui.save({ pages: pagesRef.current }).catch(() => { /* best-effort */ });
|
||||
void api.ui.save({ pages: pagesRef.current }).catch((error) => {
|
||||
console.error('page preferences unmount flush failed', error);
|
||||
});
|
||||
}
|
||||
}, [api]);
|
||||
|
||||
@@ -170,9 +176,19 @@ export function LayoutProvider({ children }: { children: ReactNode }) {
|
||||
if (saveTimer.current) clearTimeout(saveTimer.current);
|
||||
saveTimer.current = setTimeout(() => {
|
||||
saveTimer.current = null; // mark not-pending so the unmount flush can't double-fire
|
||||
void api.ui.save({ layout: next }).catch(() => { /* best-effort */ });
|
||||
void runAction(
|
||||
{
|
||||
title: '正在更新界面布局',
|
||||
detail: '正在同步仪表盘与导航布局',
|
||||
successTitle: '界面布局已更新',
|
||||
errorTitle: '界面布局更新失败',
|
||||
},
|
||||
() => api.ui.save({ layout: next }),
|
||||
).catch((error) => {
|
||||
console.error('persist layout failed', error);
|
||||
});
|
||||
}, 300);
|
||||
}, [api]);
|
||||
}, [api, runAction]);
|
||||
|
||||
const setOverviewBlocks = useCallback((items: UiLayoutItem[]) => {
|
||||
persist({ ...layoutRef.current, overviewBlocks: items });
|
||||
@@ -207,9 +223,19 @@ export function LayoutProvider({ children }: { children: ReactNode }) {
|
||||
if (pagesTimer.current) clearTimeout(pagesTimer.current);
|
||||
pagesTimer.current = setTimeout(() => {
|
||||
pagesTimer.current = null;
|
||||
void api.ui.save({ pages: next }).catch(() => { /* best-effort */ });
|
||||
void runAction(
|
||||
{
|
||||
title: '正在更新页面设置',
|
||||
detail: '正在同步当前页面偏好',
|
||||
successTitle: '页面设置已更新',
|
||||
errorTitle: '页面设置更新失败',
|
||||
},
|
||||
() => api.ui.save({ pages: next }),
|
||||
).catch((error) => {
|
||||
console.error('persist page preferences failed', error);
|
||||
});
|
||||
}, 300);
|
||||
}, [api]);
|
||||
}, [api, runAction]);
|
||||
|
||||
const value = useMemo<LayoutContextValue>(() => ({
|
||||
overviewBlocks: layout.overviewBlocks,
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { createContext, useCallback, useContext, useEffect, useLayoutEffect, useMemo, useRef, useState, type ReactNode } from 'react';
|
||||
import { flushSync } from 'react-dom';
|
||||
import { MotionConfig } from 'motion/react';
|
||||
import { useActionFeedback } from '@/contexts/ActionFeedbackContext';
|
||||
import type { Palette, ThemeMode, UiAppearance, UiBackground } from '@/types';
|
||||
|
||||
// Re-export the appearance value types so consumers (settings page etc.) can
|
||||
@@ -524,15 +525,15 @@ function isSafeMode(): boolean {
|
||||
async function persistAppearance(appearance: UiAppearance): Promise<void> {
|
||||
const token = authToken();
|
||||
if (!token) return; // pre-auth: local cache only (no settings UI exists there anyway)
|
||||
try {
|
||||
await fetch('/api/ui', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${token}` },
|
||||
// Section-level save: only `appearance`; the server preserves `layout`.
|
||||
body: JSON.stringify({ appearance }),
|
||||
});
|
||||
} catch {
|
||||
/* best-effort — the local cache already holds the change */
|
||||
const response = await fetch('/api/ui', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${token}` },
|
||||
// Section-level save: only `appearance`; the server preserves `layout`.
|
||||
body: JSON.stringify({ appearance }),
|
||||
});
|
||||
if (!response.ok) {
|
||||
const payload = (await response.json().catch(() => ({}))) as { message?: string; error?: string };
|
||||
throw new Error(payload.message || payload.error || `外观保存失败(HTTP ${response.status})`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -634,6 +635,7 @@ function getSystemTheme(): 'light' | 'dark' {
|
||||
}
|
||||
|
||||
export function ThemeProvider({ children }: { children: ReactNode }) {
|
||||
const { runAction } = useActionFeedback();
|
||||
const [appearance, setAppearanceState] = useState<UiAppearance>(readCache);
|
||||
const [systemTheme, setSystemTheme] = useState<'light' | 'dark'>(getSystemTheme);
|
||||
const [ready, setReady] = useState(false);
|
||||
@@ -671,7 +673,9 @@ export function ThemeProvider({ children }: { children: ReactNode }) {
|
||||
// clobber a look already set from another device.
|
||||
if (legacy && server && isPristine(server)) {
|
||||
next = { ...server, ...legacy, background: server.background };
|
||||
void persistAppearance(next);
|
||||
void persistAppearance(next).catch((error) => {
|
||||
console.error('appearance migration save failed', error);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -714,8 +718,21 @@ export function ThemeProvider({ children }: { children: ReactNode }) {
|
||||
setAppearanceState(next);
|
||||
writeCache(next);
|
||||
if (saveTimer.current) clearTimeout(saveTimer.current);
|
||||
saveTimer.current = setTimeout(() => void persistAppearance(next), 400);
|
||||
}, []);
|
||||
saveTimer.current = setTimeout(() => {
|
||||
void runAction(
|
||||
{
|
||||
title: '正在更新外观',
|
||||
detail: '正在同步界面设置',
|
||||
successTitle: '外观已更新',
|
||||
successDetail: '界面设置已同步',
|
||||
errorTitle: '外观更新失败',
|
||||
},
|
||||
() => persistAppearance(next),
|
||||
).catch((error) => {
|
||||
console.error('persist appearance failed', error);
|
||||
});
|
||||
}, 400);
|
||||
}, [runAction]);
|
||||
|
||||
const setAppearance = useCallback((patch: AppearancePatch) => {
|
||||
const prev = appearanceRef.current;
|
||||
@@ -732,30 +749,48 @@ export function ThemeProvider({ children }: { children: ReactNode }) {
|
||||
}
|
||||
}, [commit]);
|
||||
|
||||
const uploadBackground = useMemo(() => async (file: File) => {
|
||||
const token = authToken();
|
||||
if (!token) throw new Error('未登录');
|
||||
const form = new FormData();
|
||||
form.append('file', file);
|
||||
const res = await fetch('/api/ui/background', { method: 'POST', headers: { Authorization: `Bearer ${token}` }, body: form });
|
||||
if (!res.ok) {
|
||||
const e = (await res.json().catch(() => ({}))) as { message?: string };
|
||||
throw new Error(e.message || '上传失败');
|
||||
}
|
||||
const data = (await res.json()) as { config?: { appearance?: UiAppearance } };
|
||||
const ap = data.config?.appearance;
|
||||
if (ap) { appearanceRef.current = ap; setAppearanceState(ap); writeCache(ap); }
|
||||
}, []);
|
||||
const uploadBackground = useMemo(() => async (file: File) => runAction(
|
||||
{
|
||||
title: '正在上传背景',
|
||||
detail: file.name,
|
||||
successTitle: '背景已更新',
|
||||
successDetail: file.name,
|
||||
errorTitle: '背景上传失败',
|
||||
},
|
||||
async () => {
|
||||
const token = authToken();
|
||||
if (!token) throw new Error('未登录');
|
||||
const form = new FormData();
|
||||
form.append('file', file);
|
||||
const res = await fetch('/api/ui/background', { method: 'POST', headers: { Authorization: `Bearer ${token}` }, body: form });
|
||||
if (!res.ok) {
|
||||
const e = (await res.json().catch(() => ({}))) as { message?: string };
|
||||
throw new Error(e.message || '上传失败');
|
||||
}
|
||||
const data = (await res.json()) as { config?: { appearance?: UiAppearance } };
|
||||
const ap = data.config?.appearance;
|
||||
if (ap) { appearanceRef.current = ap; setAppearanceState(ap); writeCache(ap); }
|
||||
},
|
||||
), [runAction]);
|
||||
|
||||
const removeBackground = useMemo(() => async () => {
|
||||
const token = authToken();
|
||||
if (!token) throw new Error('未登录');
|
||||
const res = await fetch('/api/ui/background', { method: 'DELETE', headers: { Authorization: `Bearer ${token}` } });
|
||||
if (!res.ok) throw new Error('删除失败');
|
||||
const data = (await res.json()) as { config?: { appearance?: UiAppearance } };
|
||||
const ap = data.config?.appearance;
|
||||
if (ap) { appearanceRef.current = ap; setAppearanceState(ap); writeCache(ap); }
|
||||
}, []);
|
||||
const removeBackground = useMemo(() => async () => runAction(
|
||||
{
|
||||
title: '正在删除背景',
|
||||
detail: '移除已上传的壁纸',
|
||||
successTitle: '背景已删除',
|
||||
successDetail: '已恢复为无壁纸状态',
|
||||
errorTitle: '背景删除失败',
|
||||
},
|
||||
async () => {
|
||||
const token = authToken();
|
||||
if (!token) throw new Error('未登录');
|
||||
const res = await fetch('/api/ui/background', { method: 'DELETE', headers: { Authorization: `Bearer ${token}` } });
|
||||
if (!res.ok) throw new Error('删除失败');
|
||||
const data = (await res.json()) as { config?: { appearance?: UiAppearance } };
|
||||
const ap = data.config?.appearance;
|
||||
if (ap) { appearanceRef.current = ap; setAppearanceState(ap); writeCache(ap); }
|
||||
},
|
||||
), [runAction]);
|
||||
|
||||
const formatClock = useMemo(() => (input: string | number | Date) => {
|
||||
const d = input instanceof Date ? input : new Date(input);
|
||||
|
||||
Reference in new issue
Block a user