- 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
143 lines
4.9 KiB
TypeScript
143 lines
4.9 KiB
TypeScript
"use server";
|
|
|
|
import config from "@payload-config";
|
|
import { isPayloadUser } from "@/utils/access-control/isPayloadUser";
|
|
import { getPayload } from "payload";
|
|
import { normalizeDashboardLayout } from "@/lib/dashboard/layout";
|
|
import { normalizeSidebarOrder } from "@/lib/sidebar/order";
|
|
|
|
export interface ActionResult<T = undefined> {
|
|
success: boolean;
|
|
error?: string;
|
|
data?: T;
|
|
}
|
|
|
|
async function authenticate() {
|
|
const payloadConfig = await config;
|
|
const payload = await getPayload({ config: payloadConfig });
|
|
const { headers } = await import("next/headers");
|
|
const hdrs = await headers();
|
|
const { user } = await payload.auth({ headers: hdrs, canSetHeaders: false });
|
|
|
|
if (!isPayloadUser(user)) {
|
|
throw new Error("Unauthorized");
|
|
}
|
|
|
|
return { payload, user };
|
|
}
|
|
|
|
export async function saveDashboardLayout(
|
|
layout: unknown,
|
|
): Promise<ActionResult<boolean>> {
|
|
try {
|
|
const { payload, user } = await authenticate();
|
|
const userId = user.id as number;
|
|
|
|
// Write the full preferences object (same approach as updatePreferences in
|
|
// account/actions.ts) so sibling preference groups can never be clobbered
|
|
// by a partial nested update. The layout itself is sanitized server-side;
|
|
// unknown ids are dropped and missing widgets fall back to defaults.
|
|
const normalized = normalizeDashboardLayout(layout);
|
|
const fullUser = await payload.findByID({
|
|
collection: "users",
|
|
id: userId,
|
|
depth: 0,
|
|
overrideAccess: true,
|
|
});
|
|
const preferences = fullUser.preferences ?? {};
|
|
|
|
await payload.update({
|
|
collection: "users",
|
|
id: userId,
|
|
data: {
|
|
preferences: {
|
|
notifications: {
|
|
mutedAll: preferences.notifications?.mutedAll ?? false,
|
|
mutedTypes: preferences.notifications?.mutedTypes ?? [],
|
|
},
|
|
display: {
|
|
showCallsign: preferences.display?.showCallsign ?? false,
|
|
showVersionOverlay: preferences.display?.showVersionOverlay ?? true,
|
|
showBackgroundTexture: preferences.display?.showBackgroundTexture ?? true,
|
|
leadOrActual: preferences.display?.leadOrActual ?? "lead",
|
|
xpDisplayMini: preferences.display?.xpDisplayMini ?? false,
|
|
sidebarOrder: preferences.display?.sidebarOrder ?? null,
|
|
},
|
|
discord: {
|
|
enabled: preferences.discord?.enabled ?? false,
|
|
mutedTypes: preferences.discord?.mutedTypes ?? [],
|
|
},
|
|
dashboard: { layout: normalized as unknown as Record<string, unknown> },
|
|
},
|
|
},
|
|
overrideAccess: true,
|
|
});
|
|
|
|
return { success: true, data: true };
|
|
} catch (error) {
|
|
const message = error instanceof Error ? error.message : "Unknown error";
|
|
if (message === "Unauthorized") {
|
|
return { success: false, error: "You must be logged in." };
|
|
}
|
|
return { success: false, error: message };
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Persists the sidebar section order. Pass `null` to reset back to the
|
|
* default order (clears the stored preference). Like saveDashboardLayout,
|
|
* the full preferences object is rewritten so sibling groups survive, and
|
|
* the order itself is sanitized server-side.
|
|
*/
|
|
export async function updateSidebarOrder(order: unknown): Promise<ActionResult<boolean>> {
|
|
try {
|
|
const { payload, user } = await authenticate();
|
|
const userId = user.id as number;
|
|
|
|
const normalized = order === null || order === undefined ? null : normalizeSidebarOrder(order);
|
|
const fullUser = await payload.findByID({
|
|
collection: "users",
|
|
id: userId,
|
|
depth: 0,
|
|
overrideAccess: true,
|
|
});
|
|
const preferences = fullUser.preferences ?? {};
|
|
|
|
await payload.update({
|
|
collection: "users",
|
|
id: userId,
|
|
data: {
|
|
preferences: {
|
|
notifications: {
|
|
mutedAll: preferences.notifications?.mutedAll ?? false,
|
|
mutedTypes: preferences.notifications?.mutedTypes ?? [],
|
|
},
|
|
display: {
|
|
showCallsign: preferences.display?.showCallsign ?? false,
|
|
showVersionOverlay: preferences.display?.showVersionOverlay ?? true,
|
|
showBackgroundTexture: preferences.display?.showBackgroundTexture ?? true,
|
|
leadOrActual: preferences.display?.leadOrActual ?? "lead",
|
|
xpDisplayMini: preferences.display?.xpDisplayMini ?? false,
|
|
sidebarOrder: normalized,
|
|
},
|
|
discord: {
|
|
enabled: preferences.discord?.enabled ?? false,
|
|
mutedTypes: preferences.discord?.mutedTypes ?? [],
|
|
},
|
|
dashboard: {
|
|
layout: preferences.dashboard?.layout ?? null,
|
|
},
|
|
},
|
|
},
|
|
overrideAccess: true,
|
|
});
|
|
|
|
return { success: true, data: true };
|
|
} catch (error) {
|
|
const message = error instanceof Error ? error.message : "Unknown error";
|
|
if (message === "Unauthorized") {
|
|
return { success: false, error: "You must be logged in." };
|
|
}
|
|
return { success: false, error: message };
|
|
}
|
|
}
|