lint
This commit is contained in:
977 files changed
+198426
-158324
No files matched your search
@@ -1,7 +1,20 @@
|
||||
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 { actionErrorMessage, useActionFeedback } from '@/contexts/ActionFeedbackContext';
|
||||
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';
|
||||
|
||||
// Client-side layout customization (the "C" half). The server stores the
|
||||
@@ -14,11 +27,11 @@ import type { UiLayout, UiLayoutItem, UiPages } from '@/types';
|
||||
// block / nav item later doesn't need a context change — it just appears.
|
||||
|
||||
const DEFAULT_NAV_ITEMS: UiLayoutItem[] = [
|
||||
{ id: '/', visible: true },
|
||||
{ id: '/processes', visible: true },
|
||||
{ id: '/config', visible: true },
|
||||
{ id: '/logs', visible: true },
|
||||
{ id: '/settings', visible: true },
|
||||
{ id: '/', visible: true },
|
||||
{ id: '/processes', visible: true },
|
||||
{ id: '/config', visible: true },
|
||||
{ id: '/logs', visible: true },
|
||||
{ id: '/settings', visible: true },
|
||||
];
|
||||
|
||||
// Toggleable top-bar elements (essential ones are pinned in the consumer). The
|
||||
@@ -27,23 +40,30 @@ export const TOPBAR_ITEM_IDS = ['status', 'theme', 'kiosk'] as const;
|
||||
const DEFAULT_TOPBAR_ITEMS: UiLayoutItem[] = TOPBAR_ITEM_IDS.map((id) => ({ id, visible: true }));
|
||||
|
||||
export const DEFAULT_LAYOUT: UiLayout = {
|
||||
// Overview blocks are the positioned grid widgets (the catalogue owns the
|
||||
// default placement + the legacy `stats`→tiles migration).
|
||||
overviewBlocks: defaultOverviewGrid(),
|
||||
overviewMobile: defaultOverviewMobile(),
|
||||
navItems: DEFAULT_NAV_ITEMS.map((i) => ({ ...i })),
|
||||
topbarItems: DEFAULT_TOPBAR_ITEMS.map((i) => ({ ...i })),
|
||||
// Overview blocks are the positioned grid widgets (the catalogue owns the
|
||||
// default placement + the legacy `stats`→tiles migration).
|
||||
overviewBlocks: defaultOverviewGrid(),
|
||||
overviewMobile: defaultOverviewMobile(),
|
||||
navItems: DEFAULT_NAV_ITEMS.map((i) => ({ ...i })),
|
||||
topbarItems: DEFAULT_TOPBAR_ITEMS.map((i) => ({ ...i })),
|
||||
};
|
||||
|
||||
const ALL_LEVELS = ['trace', 'debug', 'info', 'success', 'warn', 'error'];
|
||||
|
||||
export function defaultPages(): UiPages {
|
||||
return {
|
||||
defaultRoute: '/',
|
||||
logs: { visibleLevels: [...ALL_LEVELS], maxLines: 1000, autoScroll: true, wrap: true, highlightRules: [], preset: 'custom' },
|
||||
processesSort: 'pid',
|
||||
configTab: '',
|
||||
};
|
||||
return {
|
||||
defaultRoute: '/',
|
||||
logs: {
|
||||
visibleLevels: [...ALL_LEVELS],
|
||||
maxLines: 1000,
|
||||
autoScroll: true,
|
||||
wrap: true,
|
||||
highlightRules: [],
|
||||
preset: 'custom',
|
||||
},
|
||||
processesSort: 'pid',
|
||||
configTab: '',
|
||||
};
|
||||
}
|
||||
|
||||
export const DEFAULT_PAGES: UiPages = defaultPages();
|
||||
@@ -55,198 +75,275 @@ export const DEFAULT_PAGES: UiPages = defaultPages();
|
||||
* `hiddenByDefault` are appended hidden (opt-in widgets), otherwise visible.
|
||||
*/
|
||||
export function reconcileLayoutItems(
|
||||
stored: UiLayoutItem[] | undefined,
|
||||
known: readonly string[],
|
||||
pinned: readonly string[] = [],
|
||||
hiddenByDefault: ReadonlySet<string> = new Set(),
|
||||
stored: UiLayoutItem[] | undefined,
|
||||
known: readonly string[],
|
||||
pinned: readonly string[] = [],
|
||||
hiddenByDefault: ReadonlySet<string> = new Set(),
|
||||
): UiLayoutItem[] {
|
||||
const knownSet = new Set(known);
|
||||
const seen = new Set<string>();
|
||||
const out: UiLayoutItem[] = [];
|
||||
for (const item of stored ?? []) {
|
||||
if (!item || !knownSet.has(item.id) || seen.has(item.id)) continue;
|
||||
seen.add(item.id);
|
||||
out.push({ id: item.id, visible: pinned.includes(item.id) ? true : item.visible !== false });
|
||||
}
|
||||
for (const id of known) {
|
||||
if (!seen.has(id)) out.push({ id, visible: !hiddenByDefault.has(id) });
|
||||
}
|
||||
return out;
|
||||
const knownSet = new Set(known);
|
||||
const seen = new Set<string>();
|
||||
const out: UiLayoutItem[] = [];
|
||||
|
||||
for (const item of stored ?? []) {
|
||||
if (!item || !knownSet.has(item.id) || seen.has(item.id)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
seen.add(item.id);
|
||||
out.push({
|
||||
id: item.id,
|
||||
visible: pinned.includes(item.id) ? true : item.visible !== false,
|
||||
});
|
||||
}
|
||||
|
||||
for (const id of known) {
|
||||
if (!seen.has(id)) {
|
||||
out.push({ id, visible: !hiddenByDefault.has(id) });
|
||||
}
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
interface LayoutContextValue {
|
||||
overviewBlocks: UiLayoutItem[];
|
||||
overviewMobile: UiLayoutItem[];
|
||||
navItems: UiLayoutItem[];
|
||||
topbarItems: UiLayoutItem[];
|
||||
/** Persist a new overview-block order/visibility (desktop 2D grid). */
|
||||
setOverviewBlocks: (items: UiLayoutItem[]) => void;
|
||||
/** Persist a new single-column mobile order/visibility. */
|
||||
setOverviewMobile: (items: UiLayoutItem[]) => void;
|
||||
/** Persist a new nav order/visibility. */
|
||||
setNavItems: (items: UiLayoutItem[]) => void;
|
||||
/** Persist a new top-bar show/hide order. */
|
||||
setTopbarItems: (items: UiLayoutItem[]) => void;
|
||||
/** Reset layout (blocks + mobile + nav + topbar) to defaults. */
|
||||
resetLayout: () => void;
|
||||
/** Per-page preferences (default route, logs/processes/config prefs). */
|
||||
pages: UiPages;
|
||||
/** Merge a partial pages-prefs update (debounced-persisted). */
|
||||
setPages: (patch: Partial<UiPages>) => void;
|
||||
/** True once the authed config load has resolved (gates the default-route redirect). */
|
||||
ready: boolean;
|
||||
/** Shared "编辑布局" mode — the dashboard grid AND the sidebar read this so both
|
||||
* show in-place edit affordances from a single toggle. */
|
||||
editing: boolean;
|
||||
setEditing: (v: boolean) => void;
|
||||
overviewBlocks: UiLayoutItem[];
|
||||
overviewMobile: UiLayoutItem[];
|
||||
navItems: UiLayoutItem[];
|
||||
topbarItems: UiLayoutItem[];
|
||||
/** Persist a new overview-block order/visibility (desktop 2D grid). */
|
||||
setOverviewBlocks: (items: UiLayoutItem[]) => void;
|
||||
/** Persist a new single-column mobile order/visibility. */
|
||||
setOverviewMobile: (items: UiLayoutItem[]) => void;
|
||||
/** Persist a new nav order/visibility. */
|
||||
setNavItems: (items: UiLayoutItem[]) => void;
|
||||
/** Persist a new top-bar show/hide order. */
|
||||
setTopbarItems: (items: UiLayoutItem[]) => void;
|
||||
/** Reset layout (blocks + mobile + nav + topbar) to defaults. */
|
||||
resetLayout: () => void;
|
||||
/** Per-page preferences (default route, logs/processes/config prefs). */
|
||||
pages: UiPages;
|
||||
/** Merge a partial pages-prefs update (debounced-persisted). */
|
||||
setPages: (patch: Partial<UiPages>) => void;
|
||||
/** True once the authed config load has resolved (gates the default-route redirect). */
|
||||
ready: boolean;
|
||||
/** Shared "编辑布局" mode — the dashboard grid AND the sidebar read this so both
|
||||
* show in-place edit affordances from a single toggle. */
|
||||
editing: boolean;
|
||||
setEditing: (v: boolean) => void;
|
||||
}
|
||||
|
||||
const LayoutContext = createContext<LayoutContextValue | null>(null);
|
||||
|
||||
export function LayoutProvider({ children }: { children: ReactNode }) {
|
||||
const api = useApi();
|
||||
const { startAction } = useActionFeedback();
|
||||
const [layout, setLayout] = useState<UiLayout>(DEFAULT_LAYOUT);
|
||||
const [pages, setPagesState] = useState<UiPages>(DEFAULT_PAGES);
|
||||
const [ready, setReady] = useState(false);
|
||||
const [editing, setEditing] = useState(false);
|
||||
const layoutRef = useRef(layout);
|
||||
const pagesRef = useRef(pages);
|
||||
// Set once the user edits, so the initial GET (which may resolve AFTER the
|
||||
// first edit) never clobbers their change.
|
||||
const dirtyRef = useRef(false);
|
||||
const saveTimer = useRef<ReturnType<typeof setTimeout> | null>(null);
|
||||
const pagesTimer = useRef<ReturnType<typeof setTimeout> | null>(null);
|
||||
const api = useApi();
|
||||
const { startAction } = useActionFeedback();
|
||||
const [layout, setLayout] = useState<UiLayout>(DEFAULT_LAYOUT);
|
||||
const [pages, setPagesState] = useState<UiPages>(DEFAULT_PAGES);
|
||||
const [ready, setReady] = useState(false);
|
||||
const [editing, setEditing] = useState(false);
|
||||
const layoutRef = useRef(layout);
|
||||
const pagesRef = useRef(pages);
|
||||
// Set once the user edits, so the initial GET (which may resolve AFTER the
|
||||
// first edit) never clobbers their change.
|
||||
const dirtyRef = useRef(false);
|
||||
const saveTimer = useRef<ReturnType<typeof setTimeout> | null>(null);
|
||||
const pagesTimer = useRef<ReturnType<typeof setTimeout> | null>(null);
|
||||
|
||||
useEffect(() => { layoutRef.current = layout; }, [layout]);
|
||||
useEffect(() => { pagesRef.current = pages; }, [pages]);
|
||||
useEffect(() => {
|
||||
layoutRef.current = layout;
|
||||
}, [layout]);
|
||||
useEffect(() => {
|
||||
pagesRef.current = pages;
|
||||
}, [pages]);
|
||||
|
||||
useEffect(() => {
|
||||
let cancelled = false;
|
||||
(async () => {
|
||||
try {
|
||||
const config = await api.ui.get();
|
||||
// Seed from the server only if the user hasn't already edited (the
|
||||
// overview + editor are interactive immediately, before this resolves).
|
||||
// Overview blocks are migrated to the current grid catalogue (legacy
|
||||
// `stats`→tiles, default coords for new/coordless widgets).
|
||||
if (!cancelled && !dirtyRef.current && config) {
|
||||
if (config.layout) {
|
||||
setLayout({
|
||||
overviewBlocks: migrateOverviewBlocks(config.layout.overviewBlocks),
|
||||
// Mobile/topbar are stored raw; consumers reconcile against their
|
||||
// known catalogues (so a new widget/element "just appears").
|
||||
overviewMobile: config.layout.overviewMobile ?? defaultOverviewMobile(),
|
||||
navItems: config.layout.navItems,
|
||||
topbarItems: config.layout.topbarItems ?? DEFAULT_TOPBAR_ITEMS.map((i) => ({ ...i })),
|
||||
});
|
||||
}
|
||||
if (config.pages) setPagesState(config.pages);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('initial UI preferences load failed', error);
|
||||
} finally {
|
||||
if (!cancelled) setReady(true);
|
||||
}
|
||||
})();
|
||||
return () => { cancelled = true; };
|
||||
}, [api]);
|
||||
useEffect(() => {
|
||||
let cancelled = false;
|
||||
|
||||
// On unmount (e.g. logout), flush pending debounced saves so a last-moment
|
||||
// edit isn't lost; best-effort (a cleared token just 401s harmlessly).
|
||||
useEffect(() => () => {
|
||||
if (saveTimer.current) {
|
||||
clearTimeout(saveTimer.current);
|
||||
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((error) => {
|
||||
console.error('page preferences unmount flush failed', error);
|
||||
});
|
||||
}
|
||||
}, [api]);
|
||||
(async () => {
|
||||
try {
|
||||
const config = await api.ui.get();
|
||||
|
||||
const persist = useCallback((next: UiLayout) => {
|
||||
dirtyRef.current = true;
|
||||
layoutRef.current = next;
|
||||
setLayout(next);
|
||||
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((error) => {
|
||||
console.error('persist layout failed', error);
|
||||
startAction({ title: '界面布局同步失败' })
|
||||
.fail(`当前修改未保存:${actionErrorMessage(error)}`);
|
||||
});
|
||||
}, 300);
|
||||
}, [api, startAction]);
|
||||
// Seed from the server only if the user hasn't already edited (the
|
||||
// overview + editor are interactive immediately, before this resolves).
|
||||
// Overview blocks are migrated to the current grid catalogue (legacy
|
||||
// `stats`→tiles, default coords for new/coordless widgets).
|
||||
if (!cancelled && !dirtyRef.current && config) {
|
||||
if (config.layout) {
|
||||
setLayout({
|
||||
overviewBlocks: migrateOverviewBlocks(config.layout.overviewBlocks),
|
||||
// Mobile/topbar are stored raw; consumers reconcile against their
|
||||
// known catalogues (so a new widget/element "just appears").
|
||||
overviewMobile: config.layout.overviewMobile ?? defaultOverviewMobile(),
|
||||
navItems: config.layout.navItems,
|
||||
topbarItems:
|
||||
config.layout.topbarItems ??
|
||||
DEFAULT_TOPBAR_ITEMS.map((i) => ({ ...i })),
|
||||
});
|
||||
}
|
||||
|
||||
const setOverviewBlocks = useCallback((items: UiLayoutItem[]) => {
|
||||
persist({ ...layoutRef.current, overviewBlocks: items });
|
||||
}, [persist]);
|
||||
if (config.pages) {
|
||||
setPagesState(config.pages);
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('initial UI preferences load failed', error);
|
||||
} finally {
|
||||
if (!cancelled) {
|
||||
setReady(true);
|
||||
}
|
||||
}
|
||||
})();
|
||||
|
||||
const setOverviewMobile = useCallback((items: UiLayoutItem[]) => {
|
||||
persist({ ...layoutRef.current, overviewMobile: items });
|
||||
}, [persist]);
|
||||
return () => {
|
||||
cancelled = true;
|
||||
};
|
||||
}, [api]);
|
||||
|
||||
const setNavItems = useCallback((items: UiLayoutItem[]) => {
|
||||
persist({ ...layoutRef.current, navItems: items });
|
||||
}, [persist]);
|
||||
// On unmount (e.g. logout), flush pending debounced saves so a last-moment
|
||||
// edit isn't lost; best-effort (a cleared token just 401s harmlessly).
|
||||
useEffect(
|
||||
() => () => {
|
||||
if (saveTimer.current) {
|
||||
clearTimeout(saveTimer.current);
|
||||
void api.ui.save({ layout: layoutRef.current }).catch((error) => {
|
||||
console.error('layout unmount flush failed', error);
|
||||
});
|
||||
}
|
||||
|
||||
const setTopbarItems = useCallback((items: UiLayoutItem[]) => {
|
||||
persist({ ...layoutRef.current, topbarItems: items });
|
||||
}, [persist]);
|
||||
if (pagesTimer.current) {
|
||||
clearTimeout(pagesTimer.current);
|
||||
void api.ui.save({ pages: pagesRef.current }).catch((error) => {
|
||||
console.error('page preferences unmount flush failed', error);
|
||||
});
|
||||
}
|
||||
},
|
||||
[api],
|
||||
);
|
||||
|
||||
const resetLayout = useCallback(() => {
|
||||
persist({
|
||||
overviewBlocks: defaultOverviewGrid(),
|
||||
overviewMobile: defaultOverviewMobile(),
|
||||
navItems: DEFAULT_NAV_ITEMS.map((i) => ({ ...i })),
|
||||
topbarItems: DEFAULT_TOPBAR_ITEMS.map((i) => ({ ...i })),
|
||||
});
|
||||
}, [persist]);
|
||||
const persist = useCallback(
|
||||
(next: UiLayout) => {
|
||||
dirtyRef.current = true;
|
||||
layoutRef.current = next;
|
||||
setLayout(next);
|
||||
|
||||
const setPages = useCallback((patch: Partial<UiPages>) => {
|
||||
dirtyRef.current = true;
|
||||
const next = { ...pagesRef.current, ...patch };
|
||||
pagesRef.current = next;
|
||||
setPagesState(next);
|
||||
if (pagesTimer.current) clearTimeout(pagesTimer.current);
|
||||
pagesTimer.current = setTimeout(() => {
|
||||
pagesTimer.current = null;
|
||||
void api.ui.save({ pages: next }).catch((error) => {
|
||||
console.error('persist page preferences failed', error);
|
||||
startAction({ title: '页面偏好同步失败' })
|
||||
.fail(`当前修改未保存:${actionErrorMessage(error)}`);
|
||||
});
|
||||
}, 300);
|
||||
}, [api, startAction]);
|
||||
if (saveTimer.current) {
|
||||
clearTimeout(saveTimer.current);
|
||||
}
|
||||
|
||||
const value = useMemo<LayoutContextValue>(() => ({
|
||||
overviewBlocks: layout.overviewBlocks,
|
||||
overviewMobile: layout.overviewMobile,
|
||||
navItems: layout.navItems,
|
||||
topbarItems: layout.topbarItems,
|
||||
setOverviewBlocks,
|
||||
setOverviewMobile,
|
||||
setNavItems,
|
||||
setTopbarItems,
|
||||
resetLayout,
|
||||
pages,
|
||||
setPages,
|
||||
ready,
|
||||
editing,
|
||||
setEditing,
|
||||
}), [layout, setOverviewBlocks, setOverviewMobile, setNavItems, setTopbarItems, resetLayout, pages, setPages, ready, editing]);
|
||||
saveTimer.current = setTimeout(() => {
|
||||
saveTimer.current = null; // mark not-pending so the unmount flush can't double-fire
|
||||
void api.ui.save({ layout: next }).catch((error) => {
|
||||
console.error('persist layout failed', error);
|
||||
startAction({ title: '界面布局同步失败' }).fail(
|
||||
`当前修改未保存:${actionErrorMessage(error)}`,
|
||||
);
|
||||
});
|
||||
}, 300);
|
||||
},
|
||||
[api, startAction],
|
||||
);
|
||||
|
||||
return <LayoutContext.Provider value={value}>{children}</LayoutContext.Provider>;
|
||||
const setOverviewBlocks = useCallback(
|
||||
(items: UiLayoutItem[]) => {
|
||||
persist({ ...layoutRef.current, overviewBlocks: items });
|
||||
},
|
||||
[persist],
|
||||
);
|
||||
|
||||
const setOverviewMobile = useCallback(
|
||||
(items: UiLayoutItem[]) => {
|
||||
persist({ ...layoutRef.current, overviewMobile: items });
|
||||
},
|
||||
[persist],
|
||||
);
|
||||
|
||||
const setNavItems = useCallback(
|
||||
(items: UiLayoutItem[]) => {
|
||||
persist({ ...layoutRef.current, navItems: items });
|
||||
},
|
||||
[persist],
|
||||
);
|
||||
|
||||
const setTopbarItems = useCallback(
|
||||
(items: UiLayoutItem[]) => {
|
||||
persist({ ...layoutRef.current, topbarItems: items });
|
||||
},
|
||||
[persist],
|
||||
);
|
||||
|
||||
const resetLayout = useCallback(() => {
|
||||
persist({
|
||||
overviewBlocks: defaultOverviewGrid(),
|
||||
overviewMobile: defaultOverviewMobile(),
|
||||
navItems: DEFAULT_NAV_ITEMS.map((i) => ({ ...i })),
|
||||
topbarItems: DEFAULT_TOPBAR_ITEMS.map((i) => ({ ...i })),
|
||||
});
|
||||
}, [persist]);
|
||||
|
||||
const setPages = useCallback(
|
||||
(patch: Partial<UiPages>) => {
|
||||
dirtyRef.current = true;
|
||||
const next = { ...pagesRef.current, ...patch };
|
||||
pagesRef.current = next;
|
||||
setPagesState(next);
|
||||
|
||||
if (pagesTimer.current) {
|
||||
clearTimeout(pagesTimer.current);
|
||||
}
|
||||
|
||||
pagesTimer.current = setTimeout(() => {
|
||||
pagesTimer.current = null;
|
||||
void api.ui.save({ pages: next }).catch((error) => {
|
||||
console.error('persist page preferences failed', error);
|
||||
startAction({ title: '页面偏好同步失败' }).fail(
|
||||
`当前修改未保存:${actionErrorMessage(error)}`,
|
||||
);
|
||||
});
|
||||
}, 300);
|
||||
},
|
||||
[api, startAction],
|
||||
);
|
||||
|
||||
const value = useMemo<LayoutContextValue>(
|
||||
() => ({
|
||||
overviewBlocks: layout.overviewBlocks,
|
||||
overviewMobile: layout.overviewMobile,
|
||||
navItems: layout.navItems,
|
||||
topbarItems: layout.topbarItems,
|
||||
setOverviewBlocks,
|
||||
setOverviewMobile,
|
||||
setNavItems,
|
||||
setTopbarItems,
|
||||
resetLayout,
|
||||
pages,
|
||||
setPages,
|
||||
ready,
|
||||
editing,
|
||||
setEditing,
|
||||
}),
|
||||
[
|
||||
layout,
|
||||
setOverviewBlocks,
|
||||
setOverviewMobile,
|
||||
setNavItems,
|
||||
setTopbarItems,
|
||||
resetLayout,
|
||||
pages,
|
||||
setPages,
|
||||
ready,
|
||||
editing,
|
||||
],
|
||||
);
|
||||
|
||||
return <LayoutContext.Provider value={value}>{children}</LayoutContext.Provider>;
|
||||
}
|
||||
|
||||
export function useLayout(): LayoutContextValue {
|
||||
const ctx = useContext(LayoutContext);
|
||||
if (!ctx) throw new Error('useLayout must be used within LayoutProvider');
|
||||
return ctx;
|
||||
const ctx = useContext(LayoutContext);
|
||||
|
||||
if (!ctx) {
|
||||
throw new Error('useLayout must be used within LayoutProvider');
|
||||
}
|
||||
|
||||
return ctx;
|
||||
}
|
||||
Reference in new issue
Block a user