refactor: simplify mobile board layouts

This commit is contained in:
ajnart committed 2026-08-08 13:54:42 +02:00
1 parent 7e7b338d18
commit fa9444323b
51 files changed
+383 -2472

No files matched your search

-47
View File
@@ -1,47 +0,0 @@
import { describe, expect, test } from "vitest";
import type { RouterOutputs } from "@homarr/api";
import { getBoardLayouts, getCurrentLayout, getDesktopLayout } from "./context";
const createBoard = () =>
({
layouts: [
{ id: "small", name: "Small", columnCount: 4, breakpoint: 0 },
{ id: "medium", name: "Medium", columnCount: 8, breakpoint: 768 },
{ id: "large", name: "Large", columnCount: 12, breakpoint: 1200 },
],
}) as RouterOutputs["board"]["getBoardByName"];
describe("board layout selection", () => {
test("automatic mode always uses the desktop layout", () => {
const board = createBoard();
expect(getDesktopLayout(board).id).toBe("large");
expect(getCurrentLayout(board, true, 390)).toBe("large");
});
test.each([
[390, "small"],
[768, "medium"],
[1024, "medium"],
[1440, "large"],
])("legacy mode selects the matching responsive layout at %spx", (viewportWidth, expectedLayoutId) => {
expect(getCurrentLayout(createBoard(), false, viewportWidth)).toBe(expectedLayoutId);
});
test("legacy mode falls back to the desktop layout before the client viewport is known", () => {
expect(getCurrentLayout(createBoard(), false, undefined)).toBe("large");
});
test("rejects boards without a layout", () => {
const board = { ...createBoard(), layouts: [] };
expect(() => getDesktopLayout(board)).toThrow("Board must have a layout");
expect(() => getCurrentLayout(board, false, 390)).toThrow("Board must have a layout");
});
test("keeps every saved layout available for reversible mutations", () => {
expect(getBoardLayouts(createBoard())).toEqual(["small", "medium", "large"]);
});
});
+14 -47
View File
@@ -6,7 +6,6 @@ import { usePathname } from "next/navigation";
import type { RouterOutputs } from "@homarr/api";
import { clientApi } from "@homarr/api/client";
import { useSettings } from "@homarr/settings";
import { updateBoardName } from "./updater";
@@ -63,65 +62,33 @@ export const useOptionalBoard = () => {
return context?.board ?? null;
};
export const getDesktopLayout = (board: RouterOutputs["board"]["getBoardByName"]) => {
const layout = board.layouts
.toSorted(
(layoutA, layoutB) => layoutB.breakpoint - layoutA.breakpoint || layoutB.columnCount - layoutA.columnCount,
)
.at(0);
if (!layout) {
throw new Error("Board must have a layout");
}
return layout;
};
export const getCurrentLayout = (
board: RouterOutputs["board"]["getBoardByName"],
enableAutomaticMobileLayout: boolean,
viewportWidth?: number,
) => {
if (enableAutomaticMobileLayout || viewportWidth === undefined) {
return getDesktopLayout(board).id;
}
export const getCurrentLayout = (board: RouterOutputs["board"]["getBoardByName"]) => {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
if (typeof window === "undefined") return board.layouts.at(0)!.id;
const sortedLayouts = board.layouts.toSorted((layoutA, layoutB) => layoutB.breakpoint - layoutA.breakpoint);
const layout = sortedLayouts.find((candidate) => candidate.breakpoint <= viewportWidth) ?? sortedLayouts.at(-1);
if (!layout) {
throw new Error("Board must have a layout");
}
return layout.id;
// Fallback to smallest if none exists with breakpoint smaller than window width
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return sortedLayouts.find((layout) => layout.breakpoint <= window.innerWidth)?.id ?? sortedLayouts.at(0)!.id;
};
export const useCurrentLayout = () => {
const board = useRequiredBoard();
const { enableAutomaticMobileLayout } = useSettings();
const [currentLayout, setCurrentLayout] = useState(() =>
getCurrentLayout(
board,
enableAutomaticMobileLayout,
!enableAutomaticMobileLayout && typeof window !== "undefined" ? window.innerWidth : undefined,
),
);
const [currentLayout, setCurrentLayout] = useState(getCurrentLayout(board));
const updateCurrentLayout = useCallback(() => {
setCurrentLayout(getCurrentLayout(board, enableAutomaticMobileLayout, window.innerWidth));
}, [board, enableAutomaticMobileLayout]);
const onResize = useCallback(() => {
setCurrentLayout(getCurrentLayout(board));
}, [board]);
useEffect(() => {
updateCurrentLayout();
if (enableAutomaticMobileLayout) return;
window.addEventListener("resize", updateCurrentLayout);
if (typeof window === "undefined") return;
window.addEventListener("resize", onResize);
return () => {
window.removeEventListener("resize", updateCurrentLayout);
window.removeEventListener("resize", onResize);
};
}, [enableAutomaticMobileLayout, updateCurrentLayout]);
}, [onResize]);
return currentLayout;
};
-9
View File
@@ -6,21 +6,12 @@ import { useDisclosure } from "@mantine/hooks";
const EditModeContext = createContext<ReturnType<typeof useDisclosure> | null>(null);
const noop = () => undefined;
const readOnlyEditMode = [false, { open: noop, close: noop, toggle: noop, set: noop }] satisfies ReturnType<
typeof useDisclosure
>;
export const EditModeProvider = ({ children }: PropsWithChildren) => {
const editModeDisclosure = useDisclosure(false);
return <EditModeContext.Provider value={editModeDisclosure}>{children}</EditModeContext.Provider>;
};
export const ReadOnlyEditModeProvider = ({ children }: PropsWithChildren) => (
<EditModeContext.Provider value={readOnlyEditMode}>{children}</EditModeContext.Provider>
);
export const useEditMode = () => {
const context = useContext(EditModeContext);