Skip to content

Commit 54d797e

Browse files
committed
fix: align mobile root section ordering
1 parent 612f2d7 commit 54d797e

3 files changed

Lines changed: 28 additions & 11 deletions

File tree

apps/nextjs/src/components/board/mobile/mobile-board.tsx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,13 @@ import type { GridItemHTMLElement, GridStack as GridStackInstance } from "@homar
1111
import { BoardItemContent } from "../items/item-content";
1212
import { SectionProvider } from "../sections/section-context";
1313
import classes from "./mobile-board.module.css";
14-
import { createMobileBoardItems, mobileColumnCount } from "./mobile-layout";
14+
import { createMobileBoardItems, getMobileRootSection, mobileColumnCount } from "./mobile-layout";
1515

1616
export const MobileBoard = () => {
1717
const board = useRequiredBoard();
1818
const desktopLayout = getDesktopLayout(board);
1919
const items = useMemo(() => createMobileBoardItems(board, desktopLayout.id), [board, desktopLayout.id]);
20-
const rootSection = board.sections.find((section) => section.kind !== "dynamic") ?? {
21-
id: "mobile",
22-
kind: "empty" as const,
23-
xOffset: 0,
24-
yOffset: 0,
25-
};
20+
const rootSection = getMobileRootSection(board);
2621
const wrapperRef = useRef<HTMLDivElement | null>(null);
2722
const itemRefs = useRef<Record<string, RefObject<GridItemHTMLElement | null>>>({});
2823
const gridstackRef = useRef<GridStackInstance | null>(null);

apps/nextjs/src/components/board/mobile/mobile-layout.spec.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,20 @@ import { BoardMockBuilder } from "../items/actions/test/mocks/board-mock";
55
import { DynamicSectionMockBuilder } from "../items/actions/test/mocks/dynamic-section-mock";
66
import { EmptySectionMockBuilder } from "../items/actions/test/mocks/empty-section-mock";
77
import { ItemMockBuilder } from "../items/actions/test/mocks/item-mock";
8-
import { createMobileBoardItems } from "./mobile-layout";
8+
import { createMobileBoardItems, getMobileRootSection } from "./mobile-layout";
99

1010
describe("createMobileBoardItems", () => {
11+
test("selects the first root section in visual order", () => {
12+
const board = new BoardMockBuilder().build();
13+
board.sections.push(
14+
new EmptySectionMockBuilder({ id: "stored-first", xOffset: 0, yOffset: 1 }).build(),
15+
new EmptySectionMockBuilder({ id: "visually-first", xOffset: 1, yOffset: 0 }).build(),
16+
new EmptySectionMockBuilder({ id: "visually-second", xOffset: 2, yOffset: 0 }).build(),
17+
);
18+
19+
expect(getMobileRootSection(board).id).toBe("visually-first");
20+
});
21+
1122
test("flattens categories and dynamic sections in visual order", () => {
1223
const board = new BoardMockBuilder().build();
1324
const desktopLayoutId = board.layouts.at(0)?.id;

apps/nextjs/src/components/board/mobile/mobile-layout.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,19 @@ type PositionedLayout = {
1515
const comparePosition = (elementA: PositionedElement, elementB: PositionedElement) =>
1616
elementA.yOffset - elementB.yOffset || elementA.xOffset - elementB.xOffset;
1717

18+
const getMobileRootSections = (board: Board) =>
19+
board.sections
20+
.filter((section) => section.kind !== "dynamic")
21+
.toSorted((sectionA, sectionB) => sectionA.yOffset - sectionB.yOffset || sectionA.xOffset - sectionB.xOffset);
22+
23+
export const getMobileRootSection = (board: Board) =>
24+
getMobileRootSections(board)[0] ?? {
25+
id: "mobile",
26+
kind: "empty" as const,
27+
xOffset: 0,
28+
yOffset: 0,
29+
};
30+
1831
const getLayout = <TLayout extends PositionedLayout>(
1932
layouts: TLayout[],
2033
desktopLayoutId: string,
@@ -26,9 +39,7 @@ const getLayout = <TLayout extends PositionedLayout>(
2639
)[0];
2740

2841
export const createMobileBoardItems = (board: Board, desktopLayoutId: string): SectionItem[] => {
29-
const rootSections = board.sections
30-
.filter((section) => section.kind !== "dynamic")
31-
.toSorted((sectionA, sectionB) => sectionA.yOffset - sectionB.yOffset || sectionA.xOffset - sectionB.xOffset);
42+
const rootSections = getMobileRootSections(board);
3243
const defaultSectionId = rootSections[0]?.id ?? "mobile";
3344
const layoutPriority = new Map(
3445
board.layouts

0 commit comments

Comments
 (0)