AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
fix(frontend): guard AppSidebar keydown listener against undefined event.key (#14260)
seer-by-sentry[bot]3ce229d373b0
diff --git a/autogpt_platform/frontend/src/components/layout/AppSidebar/AppSidebar.tsx b/autogpt_platform/frontend/src/components/layout/AppSidebar/AppSidebar.tsxindex c71df26650..d7fe06f9a4 100644--- a/autogpt_platform/frontend/src/components/layout/AppSidebar/AppSidebar.tsx+++ b/autogpt_platform/frontend/src/components/layout/AppSidebar/AppSidebar.tsx@@ -261,7 +261,7 @@ export function AppSidebar(props: Props) {useEffect(() => {function handleNewTaskShortcut(event: KeyboardEvent) {if (event.repeat) return;- if (event.key.toLocaleLowerCase() !== "o") return;+ if (!event.key || event.key.toLocaleLowerCase() !== "o") return;if (!event.metaKey && !event.ctrlKey) return;if (!event.shiftKey) return;if (isEditableElement(document.activeElement)) return;diff --git a/autogpt_platform/frontend/src/components/layout/AppSidebar/__tests__/AppSidebar.test.tsx b/autogpt_platform/frontend/src/components/layout/AppSidebar/__tests__/AppSidebar.test.tsxindex 1e0452ce7a..1f7f0f6619 100644--- a/autogpt_platform/frontend/src/components/layout/AppSidebar/__tests__/AppSidebar.test.tsx+++ b/autogpt_platform/frontend/src/components/layout/AppSidebar/__tests__/AppSidebar.test.tsx@@ -11,6 +11,8 @@ import { SidebarProvider } from "@/components/ui/sidebar";import type { ReactNode } from "react";import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";+import { fireEvent } from "@testing-library/react";+import { AppSidebar } from "../AppSidebar";function dashboardWith(agents: HomeAgentStatus[]): HomeDashboardResponse {@@ -44,6 +46,18 @@ vi.mock("next/link", () => ({useLinkStatus: () => ({ pending: false }),}));+const routerPush = vi.hoisted(() => vi.fn());++vi.mock("next/navigation", async (importOriginal) => {+ const actual = await importOriginal<typeof import("next/navigation")>();+ return {+ ...actual,+ useRouter: () => ({ push: routerPush, prefetch: vi.fn() }),+ usePathname: () => "/marketplace",+ useSearchParams: () => new URLSearchParams(),+ };+});+const useGetFlagMock = vi.hoisted(() => vi.fn(() => false));vi.mock("@/services/feature-flags/use-get-flag", async (importOriginal) => {@@ -66,6 +80,7 @@ function renderSidebar() {}beforeEach(() => {+ routerPush.mockClear();useGetFlagMock.mockReturnValue(false);server.use(getGetV2ListSessionsMockHandler200({ sessions: [], total: 0 }));});@@ -75,6 +90,23 @@ afterEach(() => {});describe("AppSidebar", () => {+ it("ignores a keydown with no key and preserves the new-task shortcut", () => {+ renderSidebar();+ const event = new KeyboardEvent("keydown", {+ ctrlKey: true,+ shiftKey: true,+ cancelable: true,+ });+ Object.defineProperty(event, "key", { value: undefined });++ expect(() => fireEvent(document, event)).not.toThrow();+ expect(routerPush).not.toHaveBeenCalled();+ expect(event.defaultPrevented).toBe(false);++ fireEvent.keyDown(document, { key: "O", ctrlKey: true, shiftKey: true });+ expect(routerPush).toHaveBeenCalledWith("/copilot");+ });+it("renders the primary navigation links", () => {renderSidebar();expect(screen.getByText("Agents")).toBeDefined();