Skip to content

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): the app sidebar keeps the user's open/closed choice across reloads (#15574)

Reinier van der Leer8d1c72914181
diff --git a/autogpt_platform/frontend/src/app/(platform)/PlatformChrome/PlatformChrome.tsx b/autogpt_platform/frontend/src/app/(platform)/PlatformChrome/PlatformChrome.tsx
index 3ab0c214be..d2a96b817b 100644
--- a/autogpt_platform/frontend/src/app/(platform)/PlatformChrome/PlatformChrome.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/PlatformChrome/PlatformChrome.tsx
@@ -9,6 +9,7 @@ import { cn } from "@/lib/utils";
import { Navbar } from "@/components/layout/Navbar/Navbar";
import { TopUpPromptProvider } from "@/components/layout/TopUpPrompt/TopUpPromptProvider";
import {
+ getPersistedSidebarOpen,
SidebarInset,
SidebarProvider,
SidebarTrigger,
@@ -26,6 +27,8 @@ import { usePlatformChrome } from "./usePlatformChrome";
interface Props {
children: ReactNode;
+ // The request's sidebar cookie, for a server render.
+ persistedSidebarOpen?: boolean;
}
// Mobile header buttons float over the content, so they get a pill surface to
@@ -33,7 +36,7 @@ interface Props {
const mobileTriggerClassName =
"rounded-full border border-[#DADADC] bg-white shadow-md hover:bg-[#F5F5F6]";
-export function PlatformChrome({ children }: Props) {
+export function PlatformChrome({ children, persistedSidebarOpen }: Props) {
const {
showNewLayout,
isNewLayoutActive,
@@ -70,12 +73,16 @@ export function PlatformChrome({ children }: Props) {
}
if (showNewLayout) {
+ // The browser's cookie stays current across client navigation; the layout's does not.
+ const openOutsideBuilder =
+ getPersistedSidebarOpen() ?? persistedSidebarOpen ?? !isBrainDumpEnabled;
return (
<SidebarProvider
- defaultOpen={!isBuilderRoute && !isBrainDumpEnabled}
+ persistState
+ defaultOpen={!isBuilderRoute && openOutsideBuilder}
style={{ "--sidebar-width": "18.25rem" } as CSSProperties}
>
- <BuilderSidebarAutoClose />
+ <BuilderSidebarAutoClose openOutsideBuilder={openOutsideBuilder} />
<ArtifactsPanelNavCollapse />
<AppSidebar />
<SidebarInset className="bg-[#f9f9f9]">
diff --git a/autogpt_platform/frontend/src/app/(platform)/PlatformChrome/__tests__/sidebarState.test.tsx b/autogpt_platform/frontend/src/app/(platform)/PlatformChrome/__tests__/sidebarState.test.tsx
new file mode 100644
index 0000000000..3397014bf6
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/(platform)/PlatformChrome/__tests__/sidebarState.test.tsx
@@ -0,0 +1,292 @@
+import {
+ act,
+ fireEvent,
+ render,
+ screen,
+} from "@/tests/integrations/test-utils";
+import type { ReactNode } from "react";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+
+import { useCopilotUIStore } from "@/app/(platform)/copilot/store";
+import { usePanelSidebarCollapse } from "@/app/(platform)/team/components/ExpertSidePanel/usePanelSidebarCollapse";
+import { Sidebar, useSidebar } from "@/components/ui/sidebar";
+import { SIDEBAR_COOKIE_NAME } from "@/components/ui/sidebar-cookie";
+import { PlatformChrome } from "../PlatformChrome";
+
+let pathname = "/home";
+let isBrainDumpEnabled = false;
+let isSoulPanelOpen = false;
+
+vi.mock("next/navigation", () => ({
+ usePathname: () => pathname,
+ useSearchParams: () => new URLSearchParams(),
+ useRouter: () => ({ push: vi.fn(), replace: vi.fn(), prefetch: vi.fn() }),
+}));
+
+vi.mock("../usePlatformChrome", () => ({
+ usePlatformChrome: () => ({
+ showNewLayout: true,
+ isBuilderRoute: pathname.startsWith("/build"),
+ }),
+}));
+
+vi.mock("@/services/feature-flags/use-get-flag", async (importOriginal) => {
+ const actual =
+ await importOriginal<
+ typeof import("@/services/feature-flags/use-get-flag")
+ >();
+ return {
+ ...actual,
+ useGetFlag: (flag: string) =>
+ flag === actual.Flag.ONBOARDING_BRAIN_DUMP ? isBrainDumpEnabled : false,
+ };
+});
+
+// The real Sidebar, so its rendered open state is what the test reads.
+vi.mock("@/components/layout/AppSidebar/AppSidebar", () => ({
+ AppSidebar: () => (
+ <Sidebar collapsible="icon">
+ <nav aria-label="App navigation" />
+ </Sidebar>
+ ),
+}));
+vi.mock("@/components/layout/TopUpPrompt/TopUpPromptProvider", () => ({
+ TopUpPromptProvider: ({ children }: { children: ReactNode }) => (
+ <>{children}</>
+ ),
+}));
+vi.mock("../../PaywallGate/PaywallGate", () => ({
+ PaywallGate: ({ children }: { children: ReactNode }) => <>{children}</>,
+}));
+vi.mock("../../admin/components/AdminImpersonationBanner", () => ({
+ AdminImpersonationBanner: () => null,
+}));
+vi.mock("../../components/GlobalSearchModal/GlobalSearchOverlay", () => ({
+ GlobalSearchOverlay: () => null,
+}));
+
+const DESKTOP_WIDTH = window.innerWidth;
+
+function load(persistedSidebarOpen?: boolean) {
+ const paintedStates: string[] = [];
+ function StateLog() {
+ paintedStates.push(useSidebar().state);
+ return null;
+ }
+ function SoulPanel() {
+ usePanelSidebarCollapse(isSoulPanelOpen);
+ return null;
+ }
+ function tree() {
+ return (
+ <PlatformChrome persistedSidebarOpen={persistedSidebarOpen}>
+ <StateLog />
+ <SoulPanel />
+ </PlatformChrome>
+ );
+ }
+ const view = render(tree());
+ function navigate(to: string) {
+ pathname = to;
+ view.rerender(tree());
+ }
+ function setSoulPanelOpen(isOpen: boolean) {
+ isSoulPanelOpen = isOpen;
+ view.rerender(tree());
+ }
+ return { ...view, paintedStates, navigate, setSoulPanelOpen };
+}
+
+function setArtifactsPanelOpen(isOpen: boolean) {
+ act(() =>
+ useCopilotUIStore.setState((s) => ({
+ artifactPanel: {
+ ...s.artifactPanel,
+ isOpen,
+ activeTab: isOpen ? "artifacts" : s.artifactPanel.activeTab,
+ activeArtifact: null,
+ },
+ })),
+ );
+}
+
+function sidebarState() {
+ return document
+ .querySelector("[data-collapsible]")
+ ?.getAttribute("data-state");
+}
+
+function rememberSidebar(open: boolean) {
+ document.cookie = `${SIDEBAR_COOKIE_NAME}=${open}; path=/`;
+}
+
+beforeEach(() => {
+ pathname = "/home";
+ isBrainDumpEnabled = false;
+ isSoulPanelOpen = false;
+ setArtifactsPanelOpen(false);
+ document.cookie = `${SIDEBAR_COOKIE_NAME}=; path=/; max-age=0`;
+});
+
+afterEach(() => {
+ Object.defineProperty(window, "innerWidth", {
+ value: DESKTOP_WIDTH,
+ configurable: true,
+ });
+});
+
+describe("the app sidebar across reloads", () => {
+ it.each([
+ { cohort: "brain-dump", brainDump: true, toggledTo: "expanded" },
+ { cohort: "default", brainDump: false, toggledTo: "collapsed" },
+ ])(
+ "keeps the $cohort cohort's toggle after a reload",
+ ({ brainDump, toggledTo }) => {
+ isBrainDumpEnabled = brainDump;
+ const first = load();
+ fireEvent.click(screen.getByRole("button", { name: "Toggle Sidebar" }));
+ expect(sidebarState()).toBe(toggledTo);
+ first.unmount();
+
+ load();
+
+ expect(sidebarState()).toBe(toggledTo);
+ },
+ );
+
+ it.each([
+ { cohort: "brain-dump", brainDump: true, expected: "collapsed" },
+ { cohort: "default", brainDump: false, expected: "expanded" },
+ ])(
+ "starts the $cohort cohort $expected when nothing is remembered",
+ ({ brainDump, expected }) => {
+ isBrainDumpEnabled = brainDump;
+
+ load();
+
+ expect(sidebarState()).toBe(expected);
+ },
+ );
+
+ it("falls back to the cookie the server read when the browser has none", () => {
+ isBrainDumpEnabled = true;
+
+ load(true);
+
+ expect(sidebarState()).toBe("expanded");
+ });
+
+ it("never paints the builder with the sidebar open, whatever was remembered", () => {
+ rememberSidebar(true);
+ pathname = "/build";
+
+ const { paintedStates } = load();
+
+ expect(paintedStates.length).toBeGreaterThan(0);
+ expect(paintedStates).not.toContain("expanded");
+ expect(sidebarState()).toBe("collapsed");
+ });
+
+ it("does not let the builder's own collapse overwrite the remembered choice", () => {
+ isBrainDumpEnabled = true;
+ rememberSidebar(true);
+ pathname = "/build";
+ load().unmount();
+
+ pathname = "/home";
+ load();
+
+ expect(sidebarState()).toBe("expanded");
+ });
+
+ it.each([
+ { before: "closed", remembered: false, toggles: 1, expected: "expanded" },
+ { before: "open", remembered: true, toggles: 2, expected: "collapsed" },
+ ])(
+ "leaving the builder after toggling there ($before before, $toggles toggles) shows what a reload would",
+ ({ remembered, toggles, expected }) => {
+ rememberSidebar(remembered);
+ const view = load();
+ view.navigate("/build");
+ for (let i = 0; i < toggles; i++) {
+ fireEvent.click(screen.getByRole("button", { name: "Toggle Sidebar" }));
+ }
+
+ view.navigate("/home");
+
+ expect(sidebarState()).toBe(expected);
+ view.unmount();
+ load();
+ expect(sidebarState()).toBe(expected);
+ },
+ );
+
+ it.each([
+ {
+ panel: "artifacts",
+ before: "closed",
+ remembered: false,
+ toggles: 1,
+ expected: "expanded",
+ },
+ {
+ panel: "artifacts",
+ before: "open",
+ remembered: true,
+ toggles: 2,
+ expected: "collapsed",
+ },
+ {
+ panel: "Soul",
+ before: "closed",
+ remembered: false,
+ toggles: 1,
+ expected: "expanded",
+ },
+ {
+ panel: "Soul",
+ before: "open",
+ remembered: true,
+ toggles: 2,
+ expected: "collapsed",
+ },
+ ])(
+ "closing the $panel panel after toggling under it ($before before, $toggles toggles) shows what a reload would",
+ ({ panel, remembered, toggles, expected }) => {
+ rememberSidebar(remembered);
+ const view = load();
+ const setPanelOpen =
+ panel === "Soul" ? view.setSoulPanelOpen : setArtifactsPanelOpen;
+ setPanelOpen(true);
+ expect(sidebarState()).toBe("collapsed");
+ for (let i = 0; i < toggles; i++) {
+ fireEvent.click(screen.getByRole("button", { name: "Toggle Sidebar" }));
+ }
+
+ setPanelOpen(false);
+
+ expect(sidebarState()).toBe(expected);
+ view.unmount();
+ isSoulPanelOpen = false;
+ setArtifactsPanelOpen(false);
+ load();
+ expect(sidebarState()).toBe(expected);
+ },
+ );
+
+ it("does not open the mobile sheet over the page when open is remembered", () => {
+ Object.defineProperty(window, "innerWidth", {
+ value: 375,
+ configurable: true,
+ });
+ rememberSidebar(true);
+
+ load();
+
+ // The desktop sidebar always renders its nav, so its absence proves the mobile path.
+ expect(
+ screen.queryByRole("navigation", { name: "App navigation" }),
+ ).toBeNull();
+ expect(screen.queryByRole("dialog")).toBeNull();
+ });
+});
diff --git a/autogpt_platform/frontend/src/app/(platform)/PlatformChrome/__tests__/useArtifactsPanelNavCollapse.test.ts b/autogpt_platform/frontend/src/app/(platform)/PlatformChrome/__tests__/useArtifactsPanelNavCollapse.test.ts
index 4ac3a795cd..2acac6af94 100644
--- a/autogpt_platform/frontend/src/app/(platform)/PlatformChrome/__tests__/useArtifactsPanelNavCollapse.test.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/PlatformChrome/__tests__/useArtifactsPanelNavCollapse.test.ts
@@ -5,7 +5,8 @@ import { useArtifactsPanelNavCollapse } from "../components/ArtifactsPanelNavCol
import { useCopilotUIStore } from "@/app/(platform)/copilot/store";
const mockUsePathname = vi.fn<() => string | null>();
-const mockSetOpen = vi.fn<(open: boolean) => void>();
+const mockSetOpen =
+ vi.fn<(open: boolean, options?: { persist?: boolean }) => void>();
let sidebarOpen = true;
vi.mock("next/navigation", () => ({
@@ -14,6 +15,7 @@ vi.mock("next/navigation", () => ({
vi.mock("@/components/ui/sidebar", () => ({
useSidebar: () => ({ open: sidebarOpen, setOpen: mockSetOpen }),
+ getPersistedSidebarOpen: () => undefined,
}));
/** The docked panel — an open artifact preview, or the artifacts tab. */
@@ -48,7 +50,7 @@ describe("useArtifactsPanelNavCollapse", () => {
setPanelOpen(true);
rerender();
- expect(mockSetOpen).toHaveBeenCalledWith(false);
+ expect(mockSetOpen).toHaveBeenCalledWith(false, { persist: false });
});
it("restores the nav when the panel closes", () => {
@@ -61,7 +63,7 @@ describe("useArtifactsPanelNavCollapse", () => {
setPanelOpen(false);
rerender();
- expect(mockSetOpen).toHaveBeenCalledWith(true);
+ expect(mockSetOpen).toHaveBeenCalledWith(true, { persist: false });
});
it("leaves a nav that was already collapsed before the panel opened", () => {
@@ -74,7 +76,7 @@ describe("useArtifactsPanelNavCollapse", () => {
setPanelOpen(false);
rerender();
- expect(mockSetOpen).toHaveBeenCalledWith(false);
+ expect(mockSetOpen).toHaveBeenCalledWith(false, { persist: false });
});
it("keeps a nav the user re-opened by hand while the panel was up", () => {
@@ -103,7 +105,7 @@ describe("useArtifactsPanelNavCollapse", () => {
mockUsePathname.mockReturnValue("/library");
rerender();
- expect(mockSetOpen).toHaveBeenCalledWith(true);
+ expect(mockSetOpen).toHaveBeenCalledWith(true, { persist: false });
});
it("ignores an artifacts panel left open on another route", () => {
diff --git a/autogpt_platform/frontend/src/app/(platform)/PlatformChrome/__tests__/useBuilderSidebarAutoClose.test.ts b/autogpt_platform/frontend/src/app/(platform)/PlatformChrome/__tests__/useBuilderSidebarAutoClose.test.ts
index b71b451a8a..713f2f7b76 100644
--- a/autogpt_platform/frontend/src/app/(platform)/PlatformChrome/__tests__/useBuilderSidebarAutoClose.test.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/PlatformChrome/__tests__/useBuilderSidebarAutoClose.test.ts
@@ -4,7 +4,8 @@ import { beforeEach, describe, expect, it, vi } from "vitest";
import { useBuilderSidebarAutoClose } from "../components/BuilderSidebarAutoClose/useBuilderSidebarAutoClose";
const mockUsePathname = vi.fn<() => string | null>();
-const mockSetOpen = vi.fn<(open: boolean) => void>();
+const mockSetOpen =
+ vi.fn<(open: boolean, options?: { persist?: boolean }) => void>();
let sidebarOpen = true;
vi.mock("next/navigation", () => ({
@@ -13,11 +14,12 @@ vi.mock("next/navigation", () => ({
vi.mock("@/components/ui/sidebar", () => ({
useSidebar: () => ({ open: sidebarOpen, setOpen: mockSetOpen }),
+ getPersistedSidebarOpen: () => undefined,
}));
-function renderAt(pathname: string) {
+function renderAt(pathname: string, openOutsideBuilder = true) {
mockUsePathname.mockReturnValue(pathname);
- return renderHook(() => useBuilderSidebarAutoClose());
+ return renderHook(() => useBuilderSidebarAutoClose(openOutsideBuilder));
}
beforeEach(() => {
@@ -34,7 +36,7 @@ describe("useBuilderSidebarAutoClose", () => {
mockUsePathname.mockReturnValue("/build");
rerender();
- expect(mockSetOpen).toHaveBeenCalledWith(false);
+ expect(mockSetOpen).toHaveBeenCalledWith(false, { persist: false });
});
it("restores the pre-builder state when navigating away", () => {
@@ -48,7 +50,7 @@ describe("useBuilderSidebarAutoClose", () => {
mockUsePathname.mockReturnValue("/library");
rerender();
- expect(mockSetOpen).toHaveBeenCalledWith(true);
+ expect(mockSetOpen).toHaveBeenCalledWith(true, { persist: false });
});
it("leaves a sidebar that was already closed before the builder closed", () => {
@@ -62,12 +64,12 @@ describe("useBuilderSidebarAutoClose", () => {
mockUsePathname.mockReturnValue("/marketplace");
rerender();
- expect(mockSetOpen).toHaveBeenCalledWith(false);
+ expect(mockSetOpen).toHaveBeenCalledWith(false, { persist: false });
});
it("does not re-close the sidebar when it is manually re-opened on /build", () => {
const { rerender } = renderAt("/build");
- expect(mockSetOpen).toHaveBeenCalledWith(false);
+ expect(mockSetOpen).toHaveBeenCalledWith(false, { persist: false });
mockSetOpen.mockClear();
sidebarOpen = true;
@@ -76,18 +78,23 @@ describe("useBuilderSidebarAutoClose", () => {
expect(mockSetOpen).not.toHaveBeenCalled();
});
- it("restores the app default after a hard load straight into the builder", () => {
- // A hard load seeds the provider closed (SidebarProvider defaultOpen), so
- // the seeded `false` must not be mistaken for a user preference.
- sidebarOpen = false;
- const { rerender } = renderAt("/build");
-
- mockSetOpen.mockClear();
- mockUsePathname.mockReturnValue("/marketplace");
- rerender();
-
- expect(mockSetOpen).toHaveBeenCalledWith(true);
- });
+ it.each([true, false])(
+ "restores what other routes open with (%s) after a hard load straight into the builder",
+ (openOutsideBuilder) => {
+ // A hard load seeds the provider closed (SidebarProvider defaultOpen), so
+ // the seeded `false` must not be mistaken for the user's choice.
+ sidebarOpen = false;
+ const { rerender } = renderAt("/build", openOutsideBuilder);
+
+ mockSetOpen.mockClear();
+ mockUsePathname.mockReturnValue("/marketplace");
+ rerender();
+
+ expect(mockSetOpen).toHaveBeenCalledWith(openOutsideBuilder, {
+ persist: false,
+ });
+ },
+ );
it("ignores repeated navigation between builder sub-routes", () => {
const { rerender } = renderAt("/build");
diff --git a/autogpt_platform/frontend/src/app/(platform)/PlatformChrome/components/ArtifactsPanelNavCollapse/useArtifactsPanelNavCollapse.ts b/autogpt_platform/frontend/src/app/(platform)/PlatformChrome/components/ArtifactsPanelNavCollapse/useArtifactsPanelNavCollapse.ts
index 0d68a34a24..cd76b49667 100644
--- a/autogpt_platform/frontend/src/app/(platform)/PlatformChrome/components/ArtifactsPanelNavCollapse/useArtifactsPanelNavCollapse.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/PlatformChrome/components/ArtifactsPanelNavCollapse/useArtifactsPanelNavCollapse.ts
@@ -1,5 +1,5 @@
import { useCopilotUIStore } from "@/app/(platform)/copilot/store";
-import { useSidebar } from "@/components/ui/sidebar";
+import { getPersistedSidebarOpen, useSidebar } from "@/components/ui/sidebar";
import { matchesRoute } from "@/lib/utils";
import { usePathname } from "next/navigation";
import { useEffect, useRef } from "react";
@@ -34,14 +34,15 @@ export function useArtifactsPanelNavCollapse() {
if (collapsesNav) {
openBeforePanelRef.current = openRef.current;
- setOpen(false);
+ setOpen(false, { persist: false });
return;
}
- // Restore only what this hook collapsed. If the user re-opened the nav
- // by hand while the panel was up, that is a deliberate choice and closing
- // it again on panel-close would undo it in front of them.
+ // Restore only what this hook collapsed, and prefer the remembered choice:
+ // a toggle made while the panel was up is the user's, and a reload shows it.
if (openBeforePanelRef.current !== null && openRef.current === false) {
- setOpen(openBeforePanelRef.current);
+ setOpen(getPersistedSidebarOpen() ?? openBeforePanelRef.current, {
+ persist: false,
+ });
}
openBeforePanelRef.current = null;
}, [collapsesNav, setOpen]);
diff --git a/autogpt_platform/frontend/src/app/(platform)/PlatformChrome/components/BuilderSidebarAutoClose/BuilderSidebarAutoClose.tsx b/autogpt_platform/frontend/src/app/(platform)/PlatformChrome/components/BuilderSidebarAutoClose/BuilderSidebarAutoClose.tsx
index 90f8322978..cd8e333643 100644
--- a/autogpt_platform/frontend/src/app/(platform)/PlatformChrome/components/BuilderSidebarAutoClose/BuilderSidebarAutoClose.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/PlatformChrome/components/BuilderSidebarAutoClose/BuilderSidebarAutoClose.tsx
@@ -2,11 +2,15 @@
import { useBuilderSidebarAutoClose } from "./useBuilderSidebarAutoClose";
+interface Props {
+ openOutsideBuilder: boolean;
+}
+
// The builder needs the full canvas, so entering /build always starts with the
// sidebar collapsed — the user can still re-open it manually. Leaving /build
// restores whatever state the sidebar was in before.
-export function BuilderSidebarAutoClose() {
- useBuilderSidebarAutoClose();
+export function BuilderSidebarAutoClose({ openOutsideBuilder }: Props) {
+ useBuilderSidebarAutoClose(openOutsideBuilder);
return null;
}
diff --git a/autogpt_platform/frontend/src/app/(platform)/PlatformChrome/components/BuilderSidebarAutoClose/useBuilderSidebarAutoClose.ts b/autogpt_platform/frontend/src/app/(platform)/PlatformChrome/components/BuilderSidebarAutoClose/useBuilderSidebarAutoClose.ts
index 1327db2f69..76f5c227ba 100644
--- a/autogpt_platform/frontend/src/app/(platform)/PlatformChrome/components/BuilderSidebarAutoClose/useBuilderSidebarAutoClose.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/PlatformChrome/components/BuilderSidebarAutoClose/useBuilderSidebarAutoClose.ts
@@ -1,9 +1,9 @@
-import { useSidebar } from "@/components/ui/sidebar";
+import { getPersistedSidebarOpen, useSidebar } from "@/components/ui/sidebar";
import { matchesRoute } from "@/lib/utils";
import { usePathname } from "next/navigation";
import { useEffect, useRef } from "react";
-export function useBuilderSidebarAutoClose() {
+export function useBuilderSidebarAutoClose(openOutsideBuilder: boolean) {
const pathname = usePathname();
const { open, setOpen } = useSidebar();
const isBuildRoute = matchesRoute(pathname, "/build");
@@ -23,16 +23,19 @@ export function useBuilderSidebarAutoClose() {
wasBuildRouteRef.current = isBuildRoute;
if (isBuildRoute) {
- // On a hard load straight into /build the provider was seeded closed
- // (defaultOpen), so "state before build" is the app default (open),
- // not the seeded value.
- openBeforeBuildRef.current = prev === null ? true : openRef.current;
- setOpen(false);
+ // A hard load into /build was seeded closed, so restore what any other
+ // route would have opened with rather than that seed.
+ openBeforeBuildRef.current =
+ prev === null ? openOutsideBuilder : openRef.current;
+ setOpen(false, { persist: false });
return;
}
if (openBeforeBuildRef.current !== null) {
- setOpen(openBeforeBuildRef.current);
+ // A choice made on /build is already remembered; showing it keeps a reload consistent.
+ setOpen(getPersistedSidebarOpen() ?? openBeforeBuildRef.current, {
+ persist: false,
+ });
openBeforeBuildRef.current = null;
}
- }, [isBuildRoute, setOpen]);
+ }, [isBuildRoute, setOpen, openOutsideBuilder]);
}
diff --git a/autogpt_platform/frontend/src/app/(platform)/layout.tsx b/autogpt_platform/frontend/src/app/(platform)/layout.tsx
index 2d8a76a36e..e3fdc8193b 100644
--- a/autogpt_platform/frontend/src/app/(platform)/layout.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/layout.tsx
@@ -1,4 +1,9 @@
+import {
+ parseSidebarCookie,
+ SIDEBAR_COOKIE_NAME,
+} from "@/components/ui/sidebar-cookie";
import { NetworkStatusMonitor } from "@/services/network-status/NetworkStatusMonitor";
+import { cookies } from "next/headers";
import { PushNotificationProvider } from "@/services/push-notifications/PushNotificationProvider";
import { ReactNode } from "react";
import { AutoPilotBridgeProvider } from "@/contexts/AutoPilotBridgeContext";
@@ -6,14 +11,23 @@ import { PlatformChrome } from "./PlatformChrome/PlatformChrome";
import { ProActivationProvider } from "@/services/pro-activation/ProActivationProvider";
import { TrialStatusSync } from "@/services/trials/TrialStatusSync";
-export default function PlatformLayout({ children }: { children: ReactNode }) {
+export default async function PlatformLayout({
+ children,
+}: {
+ children: ReactNode;
+}) {
+ const sidebarCookie = (await cookies()).get(SIDEBAR_COOKIE_NAME)?.value;
return (
<AutoPilotBridgeProvider>
<NetworkStatusMonitor />
<PushNotificationProvider />
<TrialStatusSync />
<ProActivationProvider>
- <PlatformChrome>{children}</PlatformChrome>
+ <PlatformChrome
+ persistedSidebarOpen={parseSidebarCookie(sidebarCookie)}
+ >
+ {children}
+ </PlatformChrome>
</ProActivationProvider>
</AutoPilotBridgeProvider>
);
diff --git a/autogpt_platform/frontend/src/app/(platform)/team/components/ExpertSidePanel/usePanelSidebarCollapse.test.ts b/autogpt_platform/frontend/src/app/(platform)/team/components/ExpertSidePanel/usePanelSidebarCollapse.test.ts
index b37db43dfd..1f1fb9d1b2 100644
--- a/autogpt_platform/frontend/src/app/(platform)/team/components/ExpertSidePanel/usePanelSidebarCollapse.test.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/team/components/ExpertSidePanel/usePanelSidebarCollapse.test.ts
@@ -2,11 +2,13 @@ import { renderHook } from "@testing-library/react";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { usePanelSidebarCollapse } from "./usePanelSidebarCollapse";
-const setOpen = vi.fn<(open: boolean) => void>();
+const setOpen =
+ vi.fn<(open: boolean, options?: { persist?: boolean }) => void>();
const setOpenMobile = vi.fn<(open: boolean) => void>();
let sidebarOpen = true;
vi.mock("@/components/ui/sidebar", () => ({
+ getPersistedSidebarOpen: () => undefined,
useOptionalSidebar: () => ({
open: sidebarOpen,
setOpen,
@@ -24,7 +26,7 @@ describe("usePanelSidebarCollapse", () => {
it("closes the left sidebar when the Soul panel opens", () => {
renderHook(() => usePanelSidebarCollapse(true));
- expect(setOpen).toHaveBeenCalledWith(false);
+ expect(setOpen).toHaveBeenCalledWith(false, { persist: false });
expect(setOpenMobile).toHaveBeenCalledWith(false);
});
@@ -38,7 +40,7 @@ describe("usePanelSidebarCollapse", () => {
rerender({ isOpen: false });
- expect(setOpen).toHaveBeenCalledWith(true);
+ expect(setOpen).toHaveBeenCalledWith(true, { persist: false });
});
it("restores a sidebar it closed when the panel unmounts", () => {
const { rerender, unmount } = renderHook(() =>
@@ -48,7 +50,7 @@ describe("usePanelSidebarCollapse", () => {
rerender();
setOpen.mockClear();
unmount();
- expect(setOpen).toHaveBeenCalledExactlyOnceWith(true);
+ expect(setOpen).toHaveBeenCalledExactlyOnceWith(true, { persist: false });
});
it("does not open a sidebar that was already closed", () => {
diff --git a/autogpt_platform/frontend/src/app/(platform)/team/components/ExpertSidePanel/usePanelSidebarCollapse.ts b/autogpt_platform/frontend/src/app/(platform)/team/components/ExpertSidePanel/usePanelSidebarCollapse.ts
index 1d94dcbd62..d264fb5e5d 100644
--- a/autogpt_platform/frontend/src/app/(platform)/team/components/ExpertSidePanel/usePanelSidebarCollapse.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/team/components/ExpertSidePanel/usePanelSidebarCollapse.ts
@@ -1,4 +1,7 @@
-import { useOptionalSidebar } from "@/components/ui/sidebar";
+import {
+ getPersistedSidebarOpen,
+ useOptionalSidebar,
+} from "@/components/ui/sidebar";
import { useEffect, useLayoutEffect, useRef } from "react";
export function usePanelSidebarCollapse(isPanelOpen: boolean) {
@@ -13,12 +16,14 @@ export function usePanelSidebarCollapse(isPanelOpen: boolean) {
const current = sidebarRef.current;
if (!isPanelOpen || !current) return;
const wasOpen = current.open;
- current.setOpen(false);
+ current.setOpen(false, { persist: false });
current.setOpenMobile(false);
return () => {
const latest = sidebarRef.current;
- if (wasOpen && latest && !latest.open) latest.setOpen(true);
+ // A toggle made while the panel was up is remembered, and wins.
+ if (wasOpen && latest && !latest.open)
+ latest.setOpen(getPersistedSidebarOpen() ?? true, { persist: false });
};
}, [isPanelOpen, hasSidebar]);
}
diff --git a/autogpt_platform/frontend/src/components/ui/sidebar-cookie.ts b/autogpt_platform/frontend/src/components/ui/sidebar-cookie.ts
new file mode 100644
index 0000000000..f03456d1f1
--- /dev/null
+++ b/autogpt_platform/frontend/src/components/ui/sidebar-cookie.ts
@@ -0,0 +1,11 @@
+// Kept out of the "use client" sidebar module so server components can read it too.
+// Not `sidebar_state`: automatic collapses wrote that one too, so its value is no choice.
+export const SIDEBAR_COOKIE_NAME = "sidebar_open";
+
+export function parseSidebarCookie(
+ value: string | undefined,
+): boolean | undefined {
+ if (value === "true") return true;
+ if (value === "false") return false;
+ return undefined;
+}
diff --git a/autogpt_platform/frontend/src/components/ui/sidebar.tsx b/autogpt_platform/frontend/src/components/ui/sidebar.tsx
index d6b69a4355..0c1ecb23d6 100644
--- a/autogpt_platform/frontend/src/components/ui/sidebar.tsx
+++ b/autogpt_platform/frontend/src/components/ui/sidebar.tsx
@@ -25,18 +25,23 @@ import { useIsMobile } from "@/hooks/use-mobile";
import { cn } from "@/lib/utils";
import { SidebarLeftIcon } from "@hugeicons/core-free-icons";
import { Icon } from "@/components/atoms/Icon/Icon";
+import { parseSidebarCookie, SIDEBAR_COOKIE_NAME } from "./sidebar-cookie";
-const SIDEBAR_COOKIE_NAME = "sidebar_state";
-const SIDEBAR_COOKIE_MAX_AGE = 60 * 60 * 24 * 7;
+const SIDEBAR_COOKIE_MAX_AGE = 60 * 60 * 24 * 365;
const SIDEBAR_WIDTH = "20rem";
const SIDEBAR_WIDTH_MOBILE = "20rem";
const SIDEBAR_WIDTH_ICON = "3rem";
const SIDEBAR_KEYBOARD_SHORTCUT = "b";
+interface SetOpenOptions {
+ // False for the app's own collapses and restores: only the user's choice is remembered.
+ persist?: boolean;
+}
+
type SidebarContextProps = {
state: "expanded" | "collapsed";
open: boolean;
- setOpen: (open: boolean) => void;
+ setOpen: (open: boolean, options?: SetOpenOptions) => void;
openMobile: boolean;
setOpenMobile: (open: boolean) => void;
isMobile: boolean;
@@ -64,6 +69,8 @@ const SidebarProvider = React.forwardRef<
defaultOpen?: boolean;
open?: boolean;
onOpenChange?: (open: boolean) => void;
+ // Remember the open state in a cookie; `getPersistedSidebarOpen` reads it back.
+ persistState?: boolean;
}
>(
(
@@ -71,6 +78,7 @@ const SidebarProvider = React.forwardRef<
defaultOpen = true,
open: openProp,
onOpenChange: setOpenProp,
+ persistState = false,
className,
style,
children,
@@ -86,7 +94,10 @@ const SidebarProvider = React.forwardRef<
const [_open, _setOpen] = React.useState(defaultOpen);
const open = openProp ?? _open;
const setOpen = React.useCallback(
- (value: boolean | ((value: boolean) => boolean)) => {
+ (
+ value: boolean | ((value: boolean) => boolean),
+ { persist = true }: SetOpenOptions = {},
+ ) => {
const openState = typeof value === "function" ? value(open) : value;
if (setOpenProp) {
setOpenProp(openState);
@@ -94,10 +105,11 @@ const SidebarProvider = React.forwardRef<
_setOpen(openState);
}
- // This sets the cookie to keep the sidebar state.
- document.cookie = `${SIDEBAR_COOKIE_NAME}=${openState}; path=/; max-age=${SIDEBAR_COOKIE_MAX_AGE}`;
+ if (persistState && persist) {
+ document.cookie = `${SIDEBAR_COOKIE_NAME}=${openState}; path=/; max-age=${SIDEBAR_COOKIE_MAX_AGE}`;
+ }
},
- [setOpenProp, open],
+ [setOpenProp, open, persistState],
);
// Helper to toggle the sidebar.
@@ -175,6 +187,17 @@ const SidebarProvider = React.forwardRef<
);
SidebarProvider.displayName = "SidebarProvider";
+// The browser's copy of the cookie; undefined on the server.
+function getPersistedSidebarOpen(): boolean | undefined {
+ if (typeof document === "undefined") return undefined;
+ return parseSidebarCookie(
+ document.cookie
+ .split("; ")
+ .find((cookie) => cookie.startsWith(`${SIDEBAR_COOKIE_NAME}=`))
+ ?.slice(SIDEBAR_COOKIE_NAME.length + 1),
+ );
+}
+
const Sidebar = React.forwardRef<
HTMLDivElement,
React.ComponentProps<"div"> & {
@@ -770,6 +793,7 @@ const SidebarMenuSubButton = React.forwardRef<
SidebarMenuSubButton.displayName = "SidebarMenuSubButton";
export {
+ getPersistedSidebarOpen,
Sidebar,
SidebarContent,
SidebarFooter,