1
0
Fork 0

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:
Jason Fraley 2026-10-12 14:02:21 -04:00
parent 0ca35ec251
commit 3604c1d146
3 changed files with 66 additions and 28 deletions

View file

@ -13,6 +13,7 @@ import {
SparkleIcon, SparkleIcon,
CheckIcon, CheckIcon,
GripVerticalIcon, GripVerticalIcon,
PinIcon,
RotateCcwIcon, RotateCcwIcon,
SlidersHorizontalIcon, SlidersHorizontalIcon,
} from "lucide-react"; } from "lucide-react";
@ -63,6 +64,7 @@ import { sidebarData } from "@/components/frontend/blocks/sidebarData";
import type { RegistryGame } from "@/lib/minigames/registry"; import type { RegistryGame } from "@/lib/minigames/registry";
import { import {
DEFAULT_SIDEBAR_ORDER, DEFAULT_SIDEBAR_ORDER,
PINNED_SIDEBAR_SECTION_IDS,
SIDEBAR_SECTION_LABELS, SIDEBAR_SECTION_LABELS,
normalizeSidebarOrder, normalizeSidebarOrder,
type SidebarSectionId, type SidebarSectionId,
@ -217,6 +219,13 @@ export function AppSidebar({
// Feature-flagged-off sections skip their slot entirely; hidden ids keep // Feature-flagged-off sections skip their slot entirely; hidden ids keep
// their stored positions so re-enabling a flag restores the user's layout. // their stored positions so re-enabling a flag restores the user's layout.
const visibleOrder = sectionOrder.filter((id) => sectionNodes[id] !== null); 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( const persistOrder = React.useCallback(
async (order: SidebarSectionId[] | null) => { async (order: SidebarSectionId[] | null) => {
@ -235,13 +244,14 @@ export function AppSidebar({
(event: DragEndEvent) => { (event: DragEndEvent) => {
const { active, over } = event; const { active, over } = event;
if (!over || active.id === over.id) return; if (!over || active.id === over.id) return;
const from = visibleOrder.indexOf(active.id as SidebarSectionId); const from = sortableOrder.indexOf(active.id as SidebarSectionId);
const to = visibleOrder.indexOf(over.id as SidebarSectionId); const to = sortableOrder.indexOf(over.id as SidebarSectionId);
if (from < 0 || to < 0) return; if (from < 0 || to < 0) return;
const reorderedVisible = arrayMove(visibleOrder, from, to); const reorderedSortable = arrayMove(sortableOrder, from, to);
// Re-insert hidden (feature-flagged) sections at their original indices. // Pinned sections stay on top; hidden (feature-flagged) sections are
const merged = [...reorderedVisible]; // re-inserted at their original indices.
const merged: SidebarSectionId[] = [...pinnedVisible, ...reorderedSortable];
const hiddenIds = sectionOrder.filter((id) => !visibleOrder.includes(id)); const hiddenIds = sectionOrder.filter((id) => !visibleOrder.includes(id));
hiddenIds.forEach((id) => { hiddenIds.forEach((id) => {
const originalIndex = sectionOrder.indexOf(id); const originalIndex = sectionOrder.indexOf(id);
@ -251,7 +261,7 @@ export function AppSidebar({
setSectionOrder(merged); setSectionOrder(merged);
void persistOrder(merged); void persistOrder(merged);
}, },
[persistOrder, sectionOrder, visibleOrder], [persistOrder, pinnedVisible, sectionOrder, sortableOrder, visibleOrder],
); );
const handleResetOrder = React.useCallback(() => { const handleResetOrder = React.useCallback(() => {
@ -327,19 +337,33 @@ export function AppSidebar({
</div> </div>
<div className="flex min-h-0 flex-1 flex-col gap-2 overflow-y-auto group-data-[collapsible=icon]:overflow-hidden"> <div className="flex min-h-0 flex-1 flex-col gap-2 overflow-y-auto group-data-[collapsible=icon]:overflow-hidden">
{isReordering ? ( {isReordering ? (
<>
{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 <DndContext
sensors={sensors} sensors={sensors}
collisionDetection={closestCenter} collisionDetection={closestCenter}
onDragEnd={handleDragEnd} onDragEnd={handleDragEnd}
> >
<SortableContext items={visibleOrder} strategy={verticalListSortingStrategy}> <SortableContext items={sortableOrder} strategy={verticalListSortingStrategy}>
{visibleOrder.map((id) => ( {sortableOrder.map((id) => (
<SortableSection key={id} id={id}> <SortableSection key={id} id={id}>
{sectionNodes[id]} {sectionNodes[id]}
</SortableSection> </SortableSection>
))} ))}
</SortableContext> </SortableContext>
</DndContext> </DndContext>
</>
) : ( ) : (
visibleOrder.map((id) => <React.Fragment key={id}>{sectionNodes[id]}</React.Fragment>) visibleOrder.map((id) => <React.Fragment key={id}>{sectionNodes[id]}</React.Fragment>)
)} )}

View file

@ -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 * 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 * unknown/duplicate ids, keeps known ones in the user's order, appends any
* missing sections at the end in default order. Non-array input falls back to * missing sections at the end in default order, and moves pinned sections
* the default order. * (Platform) to the front. Non-array input falls back to the default order.
*/ */
export function normalizeSidebarOrder(raw: unknown): SidebarSectionId[] { export function normalizeSidebarOrder(raw: unknown): SidebarSectionId[] {
if (!Array.isArray(raw)) { if (!Array.isArray(raw)) {
@ -64,5 +67,8 @@ export function normalizeSidebarOrder(raw: unknown): SidebarSectionId[] {
ordered.push(id); 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];
} }

View file

@ -20,10 +20,16 @@ describe("normalizeSidebarOrder", () => {
expect(normalizeSidebarOrder({ 0: "platform" })).toEqual([...DEFAULT_SIDEBAR_ORDER]); expect(normalizeSidebarOrder({ 0: "platform" })).toEqual([...DEFAULT_SIDEBAR_ORDER]);
}); });
it("keeps known ids in the user's order", () => { it("keeps known ids in the user's order (after the pinned Platform section)", () => {
const custom = ["personal", "platform", "map"]; const custom = ["personal", "platform", "commerce", "map"];
const result = normalizeSidebarOrder(custom); 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", () => { 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", () => { it("appends missing sections in default order so the list is always complete", () => {
const result = normalizeSidebarOrder(["personal"]); const result = normalizeSidebarOrder(["personal"]);
expect(result[0]).toBe("personal"); expect(result.slice(0, 2)).toEqual(["platform", "personal"]);
expect(result.slice(1)).toEqual(DEFAULT_SIDEBAR_ORDER.filter((id) => id !== "personal")); expect(result.slice(2)).toEqual(
DEFAULT_SIDEBAR_ORDER.filter((id) => id !== "platform" && id !== "personal"),
);
}); });
it("always returns every section id exactly once", () => { it("always returns every section id exactly once", () => {