Skip to content

The agent that grows with you

fix(dashboard): apply ?profile= from links that also change the path

Adolanium8c90004ec1d5
diff --git a/web/src/contexts/ProfileProvider.test.tsx b/web/src/contexts/ProfileProvider.test.tsx
new file mode 100644
index 00000000000..4751c68f245
--- /dev/null
+++ b/web/src/contexts/ProfileProvider.test.tsx
@@ -0,0 +1,94 @@
+// @vitest-environment jsdom
+// The management scope and its ?profile= URL param stay in sync. A link that
+// changes the path AND the param together (Profiles > "Manage skills & tools"
+// goes to /skills?profile=X) must switch the scope to X.
+
+import { afterEach, describe, expect, it, vi } from "vitest";
+import { act, useEffect } from "react";
+import { createRoot, type Root } from "react-dom/client";
+import {
+ MemoryRouter,
+ useLocation,
+ useNavigate,
+ type NavigateFunction,
+} from "react-router";
+
+vi.mock("@/lib/api", () => ({
+ api: {
+ getProfiles: () => new Promise(() => {}),
+ getActiveProfile: () => new Promise(() => {}),
+ },
+ setManagementProfile: () => {},
+}));
+
+import { ProfileProvider } from "./ProfileProvider";
+import { useProfileScope } from "./useProfileScope";
+
+(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT =
+ true;
+
+let container: HTMLDivElement;
+let root: Root;
+const router: { navigate?: NavigateFunction } = {};
+
+function Probe() {
+ const navigate = useNavigate();
+ const { profile, setProfile } = useProfileScope();
+ const { pathname, search } = useLocation();
+ useEffect(() => {
+ router.navigate = navigate;
+ }, [navigate]);
+ return (
+ <button
+ data-profile={profile}
+ data-url={pathname + search}
+ onClick={() => setProfile("gamma")}
+ />
+ );
+}
+
+function seen() {
+ const probe = container.querySelector("button")!;
+ return { profile: probe.dataset.profile, url: probe.dataset.url };
+}
+
+async function mount(initialEntry: string) {
+ container = document.createElement("div");
+ document.body.append(container);
+ root = createRoot(container);
+ await act(async () =>
+ root.render(
+ <MemoryRouter initialEntries={[initialEntry]}>
+ <ProfileProvider>
+ <Probe />
+ </ProfileProvider>
+ </MemoryRouter>,
+ ),
+ );
+}
+
+afterEach(async () => {
+ await act(async () => root?.unmount());
+ container?.remove();
+});
+
+describe("ProfileProvider URL sync", () => {
+ it("adopts ?profile= from a link that also changes the path", async () => {
+ await mount("/profiles?profile=alpha");
+ expect(seen()).toEqual({ profile: "alpha", url: "/profiles?profile=alpha" });
+
+ await act(async () => router.navigate!("/skills?profile=beta"));
+
+ expect(seen()).toEqual({ profile: "beta", url: "/skills?profile=beta" });
+ });
+
+ it("keeps the scope on the URL after a bare nav link and after the switcher", async () => {
+ await mount("/profiles?profile=alpha");
+
+ await act(async () => router.navigate!("/config"));
+ expect(seen()).toEqual({ profile: "alpha", url: "/config?profile=alpha" });
+
+ await act(async () => container.querySelector("button")!.click());
+ expect(seen()).toEqual({ profile: "gamma", url: "/config?profile=gamma" });
+ });
+});
diff --git a/web/src/contexts/ProfileProvider.tsx b/web/src/contexts/ProfileProvider.tsx
index 8c1adaff2a5..d6415fca1e1 100644
--- a/web/src/contexts/ProfileProvider.tsx
+++ b/web/src/contexts/ProfileProvider.tsx
@@ -52,27 +52,28 @@ export function ProfileProvider({ children }: { children: ReactNode }) {
() => initialProfileScope(searchParams, bootstrapProfile),
);
- // Mirror into the api module synchronously on every render where it
- // changed, so fetches fired by child effects in the same commit see it.
- setManagementProfile(profile);
-
- // A profile param arriving via in-app navigation (e.g. the Profiles
- // page's "Manage skills & tools" linking to /skills?profile=X) must win
- // over current state — it's an explicit scope request.
+ // A profile param that CHANGED (e.g. the Profiles page's "Manage skills &
+ // tools" linking to /skills?profile=X) is an explicit scope request and
+ // wins over current state. Adopt it during render, before any effect runs,
+ // so the URL sync below never sees the old state next to the new URL and
+ // writes it back.
const urlProfile = searchParams.get("profile");
- useEffect(() => {
+ const [seenUrlProfile, setSeenUrlProfile] = useState(urlProfile);
+ if (urlProfile !== seenUrlProfile) {
+ setSeenUrlProfile(urlProfile);
if (urlProfile !== null && urlProfile !== profile) {
- setManagementProfile(urlProfile);
setProfileState(urlProfile);
}
- // eslint-disable-next-line react-hooks/exhaustive-deps
- }, [urlProfile]);
+ }
+
+ // Mirror into the api module synchronously on every render where it
+ // changed, so fetches fired by child effects in the same commit see it.
+ setManagementProfile(profile);
- // Re-assert ?profile= after navigations that dropped it (bare nav links).
- // Runs on every pathname/profile change; no-ops when already in sync.
+ // Re-assert ?profile= after navigations that dropped it (bare nav links)
+ // and after state-only changes. No-ops when already in sync.
useEffect(() => {
- const inUrl = searchParams.get("profile") ?? "";
- if ((profile || "") === inUrl) return;
+ if ((profile || "") === (urlProfile ?? "")) return;
setSearchParams(
(prev) => {
const next = new URLSearchParams(prev);
@@ -83,7 +84,7 @@ export function ProfileProvider({ children }: { children: ReactNode }) {
{ replace: true },
);
// eslint-disable-next-line react-hooks/exhaustive-deps
- }, [pathname, profile]);
+ }, [pathname, urlProfile, profile]);
useEffect(() => {
let cancelled = false;