fix(webui): keep preference sync quiet
Keep routine layout, page, and appearance changes free of success notices. Surface persistence failures without interrupting normal adjustments.
This commit is contained in:
2 files changed
+19
-37
No files matched your search
@@ -1,5 +1,5 @@
|
|||||||
import { createContext, useCallback, useContext, useEffect, useMemo, useRef, useState, type ReactNode } from 'react';
|
import { createContext, useCallback, useContext, useEffect, useMemo, useRef, useState, type ReactNode } from 'react';
|
||||||
import { useActionFeedback } from '@/contexts/ActionFeedbackContext';
|
import { actionErrorMessage, useActionFeedback } from '@/contexts/ActionFeedbackContext';
|
||||||
import { useApi } from '@/lib/api';
|
import { useApi } from '@/lib/api';
|
||||||
import { defaultOverviewGrid, defaultOverviewMobile, migrateOverviewBlocks } from '@/lib/dashboard-layout';
|
import { defaultOverviewGrid, defaultOverviewMobile, migrateOverviewBlocks } from '@/lib/dashboard-layout';
|
||||||
import type { UiLayout, UiLayoutItem, UiPages } from '@/types';
|
import type { UiLayout, UiLayoutItem, UiPages } from '@/types';
|
||||||
@@ -105,7 +105,7 @@ const LayoutContext = createContext<LayoutContextValue | null>(null);
|
|||||||
|
|
||||||
export function LayoutProvider({ children }: { children: ReactNode }) {
|
export function LayoutProvider({ children }: { children: ReactNode }) {
|
||||||
const api = useApi();
|
const api = useApi();
|
||||||
const { runAction } = useActionFeedback();
|
const { startAction } = useActionFeedback();
|
||||||
const [layout, setLayout] = useState<UiLayout>(DEFAULT_LAYOUT);
|
const [layout, setLayout] = useState<UiLayout>(DEFAULT_LAYOUT);
|
||||||
const [pages, setPagesState] = useState<UiPages>(DEFAULT_PAGES);
|
const [pages, setPagesState] = useState<UiPages>(DEFAULT_PAGES);
|
||||||
const [ready, setReady] = useState(false);
|
const [ready, setReady] = useState(false);
|
||||||
@@ -143,8 +143,8 @@ export function LayoutProvider({ children }: { children: ReactNode }) {
|
|||||||
}
|
}
|
||||||
if (config.pages) setPagesState(config.pages);
|
if (config.pages) setPagesState(config.pages);
|
||||||
}
|
}
|
||||||
} catch {
|
} catch (error) {
|
||||||
/* keep defaults — layout/pages are non-critical */
|
console.error('initial UI preferences load failed', error);
|
||||||
} finally {
|
} finally {
|
||||||
if (!cancelled) setReady(true);
|
if (!cancelled) setReady(true);
|
||||||
}
|
}
|
||||||
@@ -176,19 +176,13 @@ export function LayoutProvider({ children }: { children: ReactNode }) {
|
|||||||
if (saveTimer.current) clearTimeout(saveTimer.current);
|
if (saveTimer.current) clearTimeout(saveTimer.current);
|
||||||
saveTimer.current = setTimeout(() => {
|
saveTimer.current = setTimeout(() => {
|
||||||
saveTimer.current = null; // mark not-pending so the unmount flush can't double-fire
|
saveTimer.current = null; // mark not-pending so the unmount flush can't double-fire
|
||||||
void runAction(
|
void api.ui.save({ layout: next }).catch((error) => {
|
||||||
{
|
|
||||||
title: '正在更新界面布局',
|
|
||||||
detail: '正在同步仪表盘与导航布局',
|
|
||||||
successTitle: '界面布局已更新',
|
|
||||||
errorTitle: '界面布局更新失败',
|
|
||||||
},
|
|
||||||
() => api.ui.save({ layout: next }),
|
|
||||||
).catch((error) => {
|
|
||||||
console.error('persist layout failed', error);
|
console.error('persist layout failed', error);
|
||||||
|
startAction({ title: '界面布局同步失败' })
|
||||||
|
.fail(`当前修改未保存:${actionErrorMessage(error)}`);
|
||||||
});
|
});
|
||||||
}, 300);
|
}, 300);
|
||||||
}, [api, runAction]);
|
}, [api, startAction]);
|
||||||
|
|
||||||
const setOverviewBlocks = useCallback((items: UiLayoutItem[]) => {
|
const setOverviewBlocks = useCallback((items: UiLayoutItem[]) => {
|
||||||
persist({ ...layoutRef.current, overviewBlocks: items });
|
persist({ ...layoutRef.current, overviewBlocks: items });
|
||||||
@@ -223,19 +217,13 @@ export function LayoutProvider({ children }: { children: ReactNode }) {
|
|||||||
if (pagesTimer.current) clearTimeout(pagesTimer.current);
|
if (pagesTimer.current) clearTimeout(pagesTimer.current);
|
||||||
pagesTimer.current = setTimeout(() => {
|
pagesTimer.current = setTimeout(() => {
|
||||||
pagesTimer.current = null;
|
pagesTimer.current = null;
|
||||||
void runAction(
|
void api.ui.save({ pages: next }).catch((error) => {
|
||||||
{
|
|
||||||
title: '正在更新页面设置',
|
|
||||||
detail: '正在同步当前页面偏好',
|
|
||||||
successTitle: '页面设置已更新',
|
|
||||||
errorTitle: '页面设置更新失败',
|
|
||||||
},
|
|
||||||
() => api.ui.save({ pages: next }),
|
|
||||||
).catch((error) => {
|
|
||||||
console.error('persist page preferences failed', error);
|
console.error('persist page preferences failed', error);
|
||||||
|
startAction({ title: '页面偏好同步失败' })
|
||||||
|
.fail(`当前修改未保存:${actionErrorMessage(error)}`);
|
||||||
});
|
});
|
||||||
}, 300);
|
}, 300);
|
||||||
}, [api, runAction]);
|
}, [api, startAction]);
|
||||||
|
|
||||||
const value = useMemo<LayoutContextValue>(() => ({
|
const value = useMemo<LayoutContextValue>(() => ({
|
||||||
overviewBlocks: layout.overviewBlocks,
|
overviewBlocks: layout.overviewBlocks,
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { createContext, useCallback, useContext, useEffect, useLayoutEffect, useMemo, useRef, useState, type ReactNode } from 'react';
|
import { createContext, useCallback, useContext, useEffect, useLayoutEffect, useMemo, useRef, useState, type ReactNode } from 'react';
|
||||||
import { flushSync } from 'react-dom';
|
import { flushSync } from 'react-dom';
|
||||||
import { MotionConfig } from 'motion/react';
|
import { MotionConfig } from 'motion/react';
|
||||||
import { useActionFeedback } from '@/contexts/ActionFeedbackContext';
|
import { actionErrorMessage, useActionFeedback } from '@/contexts/ActionFeedbackContext';
|
||||||
import type { Palette, ThemeMode, UiAppearance, UiBackground } from '@/types';
|
import type { Palette, ThemeMode, UiAppearance, UiBackground } from '@/types';
|
||||||
|
|
||||||
// Re-export the appearance value types so consumers (settings page etc.) can
|
// Re-export the appearance value types so consumers (settings page etc.) can
|
||||||
@@ -635,7 +635,7 @@ function getSystemTheme(): 'light' | 'dark' {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function ThemeProvider({ children }: { children: ReactNode }) {
|
export function ThemeProvider({ children }: { children: ReactNode }) {
|
||||||
const { runAction } = useActionFeedback();
|
const { runAction, startAction } = useActionFeedback();
|
||||||
const [appearance, setAppearanceState] = useState<UiAppearance>(readCache);
|
const [appearance, setAppearanceState] = useState<UiAppearance>(readCache);
|
||||||
const [systemTheme, setSystemTheme] = useState<'light' | 'dark'>(getSystemTheme);
|
const [systemTheme, setSystemTheme] = useState<'light' | 'dark'>(getSystemTheme);
|
||||||
const [ready, setReady] = useState(false);
|
const [ready, setReady] = useState(false);
|
||||||
@@ -719,20 +719,14 @@ export function ThemeProvider({ children }: { children: ReactNode }) {
|
|||||||
writeCache(next);
|
writeCache(next);
|
||||||
if (saveTimer.current) clearTimeout(saveTimer.current);
|
if (saveTimer.current) clearTimeout(saveTimer.current);
|
||||||
saveTimer.current = setTimeout(() => {
|
saveTimer.current = setTimeout(() => {
|
||||||
void runAction(
|
saveTimer.current = null;
|
||||||
{
|
void persistAppearance(next).catch((error) => {
|
||||||
title: '正在更新外观',
|
|
||||||
detail: '正在同步界面设置',
|
|
||||||
successTitle: '外观已更新',
|
|
||||||
successDetail: '界面设置已同步',
|
|
||||||
errorTitle: '外观更新失败',
|
|
||||||
},
|
|
||||||
() => persistAppearance(next),
|
|
||||||
).catch((error) => {
|
|
||||||
console.error('persist appearance failed', error);
|
console.error('persist appearance failed', error);
|
||||||
|
startAction({ title: '外观同步失败' })
|
||||||
|
.fail(`当前修改未保存:${actionErrorMessage(error)}`);
|
||||||
});
|
});
|
||||||
}, 400);
|
}, 400);
|
||||||
}, [runAction]);
|
}, [startAction]);
|
||||||
|
|
||||||
const setAppearance = useCallback((patch: AppearancePatch) => {
|
const setAppearance = useCallback((patch: AppearancePatch) => {
|
||||||
const prev = appearanceRef.current;
|
const prev = appearanceRef.current;
|
||||||
|
|||||||
Reference in new issue
Block a user