Compare commits

...
Author SHA1 Message Date
ajnart 048335fd73 style: auto-fix lint and format 2026-08-03 15:28:57 +00:00
Thomas Camlong 2845f61d1b fix: guard arrow hotkeys when current board is not in cache
Applies CodeRabbit review feedback: only register the arrow shortcuts
when the current board is found in the boards cache (currentIndex !== -1),
otherwise the modulo arithmetic would jump to an unexpected board.

Extracts the wraparound index calculation into a pure getNextBoardIndex
helper with unit tests.
2026-08-03 17:27:42 +02:00
Thomas Camlong 16091290e6 feat(boards): add keyboard shortcuts for board switching
- mod+shift+ArrowRight / mod+shift+ArrowLeft to cycle boards
- mod+shift+1-9 to jump to board by index
- Uses Mantine useHotkeys (ignores inputs, already in use in the codebase)
2026-07-18 18:48:45 +02:00
4 changed files with 93 additions and 14 deletions

No files matched your search

@@ -0,0 +1,23 @@
import { describe, expect, test } from "vitest";
import { getNextBoardIndex } from "./_board-navigation";
describe("get next board index", () => {
test.each([
[3, 0, 1, 1],
[3, 2, 1, 0],
[3, 2, -1, 1],
[3, 0, -1, 2],
] as const)("moves through boards with wraparound", (boardCount, currentIndex, direction, expected) => {
expect(getNextBoardIndex(boardCount, currentIndex, direction)).toBe(expected);
});
test.each([
[0, 0],
[1, 0],
[3, -1],
[3, 3],
])("does not navigate with an invalid current board", (boardCount, currentIndex) => {
expect(getNextBoardIndex(boardCount, currentIndex, 1)).toBeNull();
});
});
@@ -0,0 +1,4 @@
export const getNextBoardIndex = (boardCount: number, currentIndex: number, direction: -1 | 1) => {
if (boardCount <= 1 || currentIndex < 0 || currentIndex >= boardCount) return null;
return (currentIndex + direction + boardCount) % boardCount;
};
@@ -1,6 +1,6 @@
"use client";
import { useCallback, useEffect } from "react";
import { useCallback, useEffect, useMemo } from "react";
import { useRouter } from "next/navigation";
import { OnboardingTour } from "@gfazioli/mantine-onboarding-tour";
import { Box, Group, Menu, ScrollArea } from "@mantine/core";
@@ -40,28 +40,29 @@ import { CategoryEditModal } from "~/components/board/sections/category/category
import { useDynamicSectionActions } from "~/components/board/sections/dynamic/dynamic-actions";
import { IntegrationSelectModal } from "~/components/integration/integration-select-modal";
import { HeaderButton } from "~/components/layout/header/button";
import { getNextBoardIndex } from "./_board-navigation";
export const BoardContentHeaderActions = ({ demoReadOnly }: { demoReadOnly: boolean }) => {
const [isEditMode] = useEditMode();
const board = useRequiredBoard();
const { hasChangeAccess } = useBoardPermissions(board);
if (!hasChangeAccess) {
return <SelectBoardsMenu />;
}
return (
<>
{isEditMode && <AddMenu />}
<BoardNavigationHotkeys />
<EditModeMenu demoReadOnly={demoReadOnly} />
{!demoReadOnly && (
<OnboardingTour.Target id="board-settings">
<HeaderButton href={`/boards/${board.name}/settings`}>
<IconSettings stroke={1.5} />
</HeaderButton>
</OnboardingTour.Target>
{hasChangeAccess && (
<>
{isEditMode && <AddMenu />}
<EditModeMenu demoReadOnly={demoReadOnly} />
{!demoReadOnly && (
<OnboardingTour.Target id="board-settings">
<HeaderButton href={`/boards/${board.name}/settings`}>
<IconSettings stroke={1.5} />
</HeaderButton>
</OnboardingTour.Target>
)}
</>
)}
<SelectBoardsMenu />
@@ -205,6 +206,56 @@ const EditModeMenu = ({ demoReadOnly }: { demoReadOnly: boolean }) => {
);
};
const BoardNavigationHotkeys = () => {
const router = useRouter();
const { data: boards = [] } = clientApi.board.getAllBoards.useQuery();
const board = useRequiredBoard();
const currentIndex = useMemo(() => boards.findIndex((b) => b.id === board.id), [boards, board.id]);
const navigateToBoard = useCallback(
(index: number) => {
const target = boards[index];
if (target) router.push(`/boards/${target.name}`);
},
[boards, router],
);
const hotkeyEntries = useMemo(() => {
const entries: [string, () => void][] = [];
// Only register the arrow shortcuts when the current board is known. If the
// board hasn't synced with the getAllBoards cache yet, currentIndex is -1 and
// the modulo arithmetic would otherwise jump to an unexpected board.
if (boards.length > 1 && currentIndex !== -1) {
entries.push([
"mod+shift+ArrowRight",
() => {
const nextIndex = getNextBoardIndex(boards.length, currentIndex, 1);
if (nextIndex !== null) navigateToBoard(nextIndex);
},
]);
entries.push([
"mod+shift+ArrowLeft",
() => {
const nextIndex = getNextBoardIndex(boards.length, currentIndex, -1);
if (nextIndex !== null) navigateToBoard(nextIndex);
},
]);
}
for (let i = 0; i < Math.min(boards.length, 9); i++) {
entries.push([`mod+shift+${i + 1}`, () => navigateToBoard(i)]);
}
return entries;
}, [boards, currentIndex, navigateToBoard]);
useHotkeys(hotkeyEntries);
return null;
};
const SelectBoardsMenu = () => {
const { data: boards = [] } = clientApi.board.getAllBoards.useQuery();
@@ -119,6 +119,7 @@ export type HomarrDocumentationPath =
| "/docs/tags/variables"
| "/docs/tags/widgets"
| "/docs/advanced/command-line"
| "/docs/advanced/command-line/development"
| "/docs/advanced/command-line/fix-usernames"
| "/docs/advanced/command-line/integrations"
| "/docs/advanced/command-line/password-recovery"