feat(sidebar): pin Platform section to top during reorder
- normalizeSidebarOrder moves pinned sections (Platform) to the front, so even hand-crafted stored orders cannot displace it; enforced server-side in updateSidebarOrder via the same helper - Customize mode renders Platform statically with a pin marker instead of a drag grip; only the remaining sections are sortable - Spec updated: user order preserved after the pinned head, new pinning test
This commit is contained in:
parent
0ca35ec251
commit
3604c1d146
3 changed files with 66 additions and 28 deletions
|
|
@ -13,6 +13,7 @@ import {
|
|||
SparkleIcon,
|
||||
CheckIcon,
|
||||
GripVerticalIcon,
|
||||
PinIcon,
|
||||
RotateCcwIcon,
|
||||
SlidersHorizontalIcon,
|
||||
} from "lucide-react";
|
||||
|
|
@ -63,6 +64,7 @@ import { sidebarData } from "@/components/frontend/blocks/sidebarData";
|
|||
import type { RegistryGame } from "@/lib/minigames/registry";
|
||||
import {
|
||||
DEFAULT_SIDEBAR_ORDER,
|
||||
PINNED_SIDEBAR_SECTION_IDS,
|
||||
SIDEBAR_SECTION_LABELS,
|
||||
normalizeSidebarOrder,
|
||||
type SidebarSectionId,
|
||||
|
|
@ -217,6 +219,13 @@ export function AppSidebar({
|
|||
// Feature-flagged-off sections skip their slot entirely; hidden ids keep
|
||||
// their stored positions so re-enabling a flag restores the user's layout.
|
||||
const visibleOrder = sectionOrder.filter((id) => sectionNodes[id] !== null);
|
||||
// Pinned sections (Platform) always render first and are not draggable.
|
||||
const pinnedVisible = visibleOrder.filter((id) =>
|
||||
PINNED_SIDEBAR_SECTION_IDS.includes(id),
|
||||
);
|
||||
const sortableOrder = visibleOrder.filter(
|
||||
(id) => !PINNED_SIDEBAR_SECTION_IDS.includes(id),
|
||||
);
|
||||
|
||||
const persistOrder = React.useCallback(
|
||||
async (order: SidebarSectionId[] | null) => {
|
||||
|
|
@ -235,13 +244,14 @@ export function AppSidebar({
|
|||
(event: DragEndEvent) => {
|
||||
const { active, over } = event;
|
||||
if (!over || active.id === over.id) return;
|
||||
const from = visibleOrder.indexOf(active.id as SidebarSectionId);
|
||||
const to = visibleOrder.indexOf(over.id as SidebarSectionId);
|
||||
const from = sortableOrder.indexOf(active.id as SidebarSectionId);
|
||||
const to = sortableOrder.indexOf(over.id as SidebarSectionId);
|
||||
if (from < 0 || to < 0) return;
|
||||
|
||||
const reorderedVisible = arrayMove(visibleOrder, from, to);
|
||||
// Re-insert hidden (feature-flagged) sections at their original indices.
|
||||
const merged = [...reorderedVisible];
|
||||
const reorderedSortable = arrayMove(sortableOrder, from, to);
|
||||
// Pinned sections stay on top; hidden (feature-flagged) sections are
|
||||
// re-inserted at their original indices.
|
||||
const merged: SidebarSectionId[] = [...pinnedVisible, ...reorderedSortable];
|
||||
const hiddenIds = sectionOrder.filter((id) => !visibleOrder.includes(id));
|
||||
hiddenIds.forEach((id) => {
|
||||
const originalIndex = sectionOrder.indexOf(id);
|
||||
|
|
@ -251,7 +261,7 @@ export function AppSidebar({
|
|||
setSectionOrder(merged);
|
||||
void persistOrder(merged);
|
||||
},
|
||||
[persistOrder, sectionOrder, visibleOrder],
|
||||
[persistOrder, pinnedVisible, sectionOrder, sortableOrder, visibleOrder],
|
||||
);
|
||||
|
||||
const handleResetOrder = React.useCallback(() => {
|
||||
|
|
@ -327,19 +337,33 @@ export function AppSidebar({
|
|||
</div>
|
||||
<div className="flex min-h-0 flex-1 flex-col gap-2 overflow-y-auto group-data-[collapsible=icon]:overflow-hidden">
|
||||
{isReordering ? (
|
||||
<DndContext
|
||||
sensors={sensors}
|
||||
collisionDetection={closestCenter}
|
||||
onDragEnd={handleDragEnd}
|
||||
>
|
||||
<SortableContext items={visibleOrder} strategy={verticalListSortingStrategy}>
|
||||
{visibleOrder.map((id) => (
|
||||
<SortableSection key={id} id={id}>
|
||||
{sectionNodes[id]}
|
||||
</SortableSection>
|
||||
))}
|
||||
</SortableContext>
|
||||
</DndContext>
|
||||
<>
|
||||
{pinnedVisible.map((id) => (
|
||||
<div key={id} className="relative">
|
||||
<span
|
||||
aria-hidden="true"
|
||||
title="Pinned to top"
|
||||
className="absolute right-1 top-0 flex size-6 items-center justify-center text-muted-foreground/60"
|
||||
>
|
||||
<PinIcon className="size-3.5" />
|
||||
</span>
|
||||
{sectionNodes[id]}
|
||||
</div>
|
||||
))}
|
||||
<DndContext
|
||||
sensors={sensors}
|
||||
collisionDetection={closestCenter}
|
||||
onDragEnd={handleDragEnd}
|
||||
>
|
||||
<SortableContext items={sortableOrder} strategy={verticalListSortingStrategy}>
|
||||
{sortableOrder.map((id) => (
|
||||
<SortableSection key={id} id={id}>
|
||||
{sectionNodes[id]}
|
||||
</SortableSection>
|
||||
))}
|
||||
</SortableContext>
|
||||
</DndContext>
|
||||
</>
|
||||
) : (
|
||||
visibleOrder.map((id) => <React.Fragment key={id}>{sectionNodes[id]}</React.Fragment>)
|
||||
)}
|
||||
|
|
|
|||
|
|
@ -40,11 +40,14 @@ export function isSidebarSectionId(value: unknown): value is SidebarSectionId {
|
|||
);
|
||||
}
|
||||
|
||||
/** Sections that always render first and cannot be reordered by users. */
|
||||
export const PINNED_SIDEBAR_SECTION_IDS: readonly SidebarSectionId[] = ["platform"];
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* unknown/duplicate ids, keeps known ones in the user's order, appends any
|
||||
* missing sections at the end in default order, and moves pinned sections
|
||||
* (Platform) to the front. Non-array input falls back to the default order.
|
||||
*/
|
||||
export function normalizeSidebarOrder(raw: unknown): SidebarSectionId[] {
|
||||
if (!Array.isArray(raw)) {
|
||||
|
|
@ -64,5 +67,8 @@ export function normalizeSidebarOrder(raw: unknown): SidebarSectionId[] {
|
|||
ordered.push(id);
|
||||
}
|
||||
}
|
||||
return ordered;
|
||||
|
||||
const pinned = PINNED_SIDEBAR_SECTION_IDS.filter((id) => ordered.includes(id));
|
||||
const rest = ordered.filter((id) => !PINNED_SIDEBAR_SECTION_IDS.includes(id));
|
||||
return [...pinned, ...rest];
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,10 +20,16 @@ describe("normalizeSidebarOrder", () => {
|
|||
expect(normalizeSidebarOrder({ 0: "platform" })).toEqual([...DEFAULT_SIDEBAR_ORDER]);
|
||||
});
|
||||
|
||||
it("keeps known ids in the user's order", () => {
|
||||
const custom = ["personal", "platform", "map"];
|
||||
it("keeps known ids in the user's order (after the pinned Platform section)", () => {
|
||||
const custom = ["personal", "platform", "commerce", "map"];
|
||||
const result = normalizeSidebarOrder(custom);
|
||||
expect(result.slice(0, 3)).toEqual(["personal", "platform", "map"]);
|
||||
expect(result.slice(0, 4)).toEqual(["platform", "personal", "commerce", "map"]);
|
||||
});
|
||||
|
||||
it("pins Platform to the front even when stored elsewhere or missing", () => {
|
||||
expect(normalizeSidebarOrder(["personal", "platform", "map"])[0]).toBe("platform");
|
||||
expect(normalizeSidebarOrder([...DEFAULT_SIDEBAR_ORDER].reverse())[0]).toBe("platform");
|
||||
expect(normalizeSidebarOrder(["commerce", "map"])[0]).toBe("platform");
|
||||
});
|
||||
|
||||
it("drops unknown and duplicate ids", () => {
|
||||
|
|
@ -42,8 +48,10 @@ describe("normalizeSidebarOrder", () => {
|
|||
|
||||
it("appends missing sections in default order so the list is always complete", () => {
|
||||
const result = normalizeSidebarOrder(["personal"]);
|
||||
expect(result[0]).toBe("personal");
|
||||
expect(result.slice(1)).toEqual(DEFAULT_SIDEBAR_ORDER.filter((id) => id !== "personal"));
|
||||
expect(result.slice(0, 2)).toEqual(["platform", "personal"]);
|
||||
expect(result.slice(2)).toEqual(
|
||||
DEFAULT_SIDEBAR_ORDER.filter((id) => id !== "platform" && id !== "personal"),
|
||||
);
|
||||
});
|
||||
|
||||
it("always returns every section id exactly once", () => {
|
||||
|
|
|
|||
Loading…
Reference in a new issue