feat(webui): layout schema for mobile overview + topbar items (B2)
Schema foundation for the B2 dashboard batch, mirroring the navItems shape: - `overviewMobile: UiLayoutItem[]` — a separate single-column (id+visible) ordering for phones, kept fully disjoint from the desktop 2D `overviewBlocks` grid so mobile ordering never pollutes grid coords. - `topbarItems: UiLayoutItem[]` — show/hide list for toggleable top-bar elements (status badge, theme toggle, kiosk button); essential elements stay pinned in the consumer. Server `normalizeLayout` round-trips both (idOr + dedup, total over corrupt input); defaults seed first run. `LayoutContext` exposes them with setOverviewMobile/setTopbarItems setters and reconcile-on-consume semantics, so adding a widget/element later just appears. Old configs lacking the fields fall back to defaults on load. Consumers (mobile render, topbar render + config, drag-sort panel) land in follow-up B2 commits.
This commit is contained in:
4 files changed
+83
-5
No files matched your search
@@ -1,6 +1,6 @@
|
||||
import { createContext, useCallback, useContext, useEffect, useMemo, useRef, useState, type ReactNode } from 'react';
|
||||
import { useApi } from '@/lib/api';
|
||||
import { defaultOverviewGrid, 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
|
||||
@@ -20,11 +20,18 @@ const DEFAULT_NAV_ITEMS: UiLayoutItem[] = [
|
||||
{ id: '/settings', visible: true },
|
||||
];
|
||||
|
||||
// Toggleable top-bar elements (essential ones are pinned in the consumer). The
|
||||
// catalogue (labels) lives with the top-bar; this is just the default order.
|
||||
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 })),
|
||||
};
|
||||
|
||||
const ALL_LEVELS = ['trace', 'debug', 'info', 'success', 'warn', 'error'];
|
||||
@@ -66,12 +73,18 @@ export function reconcileLayoutItems(
|
||||
|
||||
interface LayoutContextValue {
|
||||
overviewBlocks: UiLayoutItem[];
|
||||
overviewMobile: UiLayoutItem[];
|
||||
navItems: UiLayoutItem[];
|
||||
/** Persist a new overview-block order/visibility. */
|
||||
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;
|
||||
/** Reset layout (blocks + nav) to defaults. */
|
||||
/** 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;
|
||||
@@ -117,7 +130,11 @@ export function LayoutProvider({ children }: { children: ReactNode }) {
|
||||
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);
|
||||
@@ -159,14 +176,24 @@ export function LayoutProvider({ children }: { children: ReactNode }) {
|
||||
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]);
|
||||
|
||||
@@ -184,16 +211,20 @@ export function LayoutProvider({ children }: { children: ReactNode }) {
|
||||
|
||||
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, setNavItems, resetLayout, pages, setPages, ready, editing]);
|
||||
}), [layout, setOverviewBlocks, setOverviewMobile, setNavItems, setTopbarItems, resetLayout, pages, setPages, ready, editing]);
|
||||
|
||||
return <LayoutContext.Provider value={value}>{children}</LayoutContext.Provider>;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user