1
0
Fork 0
polaris-task-force/src/lib/sidebar/order.ts
Z8MB1E 0ca35ec251 feat(sidebar): add server-persisted section reorder with customize mode
- preferences.display.sidebarOrder json field on Users + named migration
  add_sidebar_order_preference + regenerated types
- New pure helper src/lib/sidebar/order.ts (section ids, labels, default
  order, normalizeSidebarOrder) modeled on lib/dashboard/layout.ts, with
  tests/int/sidebar-order.int.spec.ts covering fallback/dupes/missing ids
- AppSidebar renders sections from the normalized order; feature-flagged-off
  sections skip their slot and keep stored positions
- Customize mode: dnd-kit sortable grips on group headers, save-on-drop via
  new updateSidebarOrder action, Reset clears the preference, Done exits;
  hidden while the sidebar is collapsed
- updateSidebarOrder sanitizes server-side and rewrites full preferences
- Fix sibling clobbering: saveDashboardLayout now preserves sidebarOrder,
  and updatePreferences now preserves both sidebarOrder and dashboard.layout
2026-10-12 13:46:48 -04:00

68 lines
1.8 KiB
TypeScript

/**
* Sidebar section registry and order vocabulary. Pure module (no React, no
* Payload import) so both the sidebar client component and server actions can
* use it. Modeled on src/lib/dashboard/layout.ts.
*/
export const SIDEBAR_SECTION_IDS = [
"platform",
"map",
"intelligence",
"operations",
"infrastructure",
"chronicle",
"surveys",
"helpdesk",
"commerce",
"personal",
] as const;
export type SidebarSectionId = (typeof SIDEBAR_SECTION_IDS)[number];
export const DEFAULT_SIDEBAR_ORDER: readonly SidebarSectionId[] = [...SIDEBAR_SECTION_IDS];
export const SIDEBAR_SECTION_LABELS: Record<SidebarSectionId, string> = {
platform: "Platform",
map: "Map",
intelligence: "Intelligence",
operations: "Operations",
infrastructure: "Infrastructure",
chronicle: "Chronicle",
surveys: "Surveys",
helpdesk: "Helpdesk",
commerce: "Commerce",
personal: "Personal",
};
export function isSidebarSectionId(value: unknown): value is SidebarSectionId {
return (
typeof value === "string" && (SIDEBAR_SECTION_IDS as readonly string[]).includes(value)
);
}
/**
* Sanitizes an arbitrary stored order into a valid full section list: drops
* unknown/duplicate ids, keeps known ones in the user's order, and appends any
* missing sections at the end in default order. Non-array input falls back to
* the default order.
*/
export function normalizeSidebarOrder(raw: unknown): SidebarSectionId[] {
if (!Array.isArray(raw)) {
return [...DEFAULT_SIDEBAR_ORDER];
}
const seen = new Set<SidebarSectionId>();
const ordered: SidebarSectionId[] = [];
for (const entry of raw) {
if (isSidebarSectionId(entry) && !seen.has(entry)) {
seen.add(entry);
ordered.push(entry);
}
}
for (const id of DEFAULT_SIDEBAR_ORDER) {
if (!seen.has(id)) {
ordered.push(id);
}
}
return ordered;
}