A desktop coding agent with free access to frontier models.
TypeScript
fix: library item page no longer traps navigation
ogsamratbbe70ad59a4c
diff --git a/apps/app/src/react-app/domains/settings/pages/mcp-view.tsx b/apps/app/src/react-app/domains/settings/pages/mcp-view.tsxindex 0a73d754..bbf7d9cf 100644--- a/apps/app/src/react-app/domains/settings/pages/mcp-view.tsx+++ b/apps/app/src/react-app/domains/settings/pages/mcp-view.tsx@@ -1,5 +1,5 @@/** @jsxImportSource react */-import { useEffect, useReducer, useRef, useState, type ReactNode, type SetStateAction } from "react";+import { useEffect, useMemo, useReducer, useRef, useState, type ReactNode, type SetStateAction } from "react";import {BookOpen,ArrowUpRight,@@ -377,7 +377,7 @@ function isToggleOnlyExtension(entry: McpDirectoryInfo) {) === true;}-type ExtensionDetailTarget =+export type ExtensionDetailTarget =| { kind: "entry"; entry: McpDirectoryInfo }| { kind: "skill"; skill: SkillItem }| { kind: "command"; command: LibraryCommandItem }@@ -410,6 +410,40 @@ function extensionDetailIdForTarget(target: ExtensionDetailTarget): string {}}+/** Whether two detail targets show the same item object (a list refresh hands out new objects). */+export function sameExtensionDetailTarget(+ a: ExtensionDetailTarget | null,+ b: ExtensionDetailTarget | null,+): boolean {+ if (a === b) return true;+ if (!a || !b || a.kind !== b.kind) return false;+ switch (a.kind) {+ case "entry":+ case "connect-mcp":+ return a.entry === (b as typeof a).entry;+ case "skill":+ return a.skill === (b as typeof a).skill;+ case "command":+ return a.command === (b as typeof a).command;+ case "agent":+ return a.agent === (b as typeof a).agent;+ case "plugin":+ return a.plugin === (b as typeof a).plugin;+ case "plugin-file":+ return a.plugin === (b as typeof a).plugin && a.file === (b as typeof a).file;+ case "org-mcp":+ return a.item === (b as typeof a).item;+ }+}++// Stable empty lists: a fresh [] per render would re-run the routed detail effect on every render.+const NO_SKILLS: SkillItem[] = [];+const NO_COMMANDS: LibraryCommandItem[] = [];+const NO_AGENTS: LibraryAgentItem[] = [];+const NO_PLUGINS: CloudImportedPlugin[] = [];+const NO_CONNECT_MCPS: McpServerEntry[] = [];+const NO_ORG_MCP_ITEMS: ExtensionItem[] = [];+function resolveExtensionDetailTarget(detailId: string,lists: {@@ -478,6 +512,8 @@ export function McpView(props: McpViewProps) {const skillCount = props.installedSkills?.length ?? 0;const useRoutedDetail = typeof props.onDetailIdChange === "function";const [detailTarget, setDetailTarget] = useState<ExtensionDetailTarget | null>(null);+ const detailTargetRef = useRef(detailTarget);+ detailTargetRef.current = detailTarget;const [mcpConnectFailure, setMcpConnectFailure] = useState<{ id: string; message: string } | null>(null);const [pendingPlugin, setPendingPlugin] = useState<CloudImportedPlugin | null>(null);const [detailSkillContent, setDetailSkillContent] = useState<string | null>(null);@@ -543,13 +579,22 @@ export function McpView(props: McpViewProps) {// MCP servers come from the account's supported list (McpSupportedPanel).// The quick-connect services, Connect and organization MCPs are remote and// account-linked: never listed (a session that used them could not be repeated).- const quickConnectList = props.quickConnect.filter((entry) => !isMcpDirectoryEntry(entry));- const installedSkills = props.installedSkills ?? [];- const installedCommands = props.installedCommands ?? [];- const installedAgents = props.installedAgents ?? [];- const availableConnectMcpServers: McpServerEntry[] = [];- const installedPlugins = (props.installedPlugins ?? []).filter((plugin) => !plugin.files.some((file) => file.objectType === "mcp"));- const orgMcpItems: ExtensionItem[] = [];+ // These lists feed the routed detail effect's dependencies, so each keeps its identity between+ // renders: a new array every render re-ran it on every render, and its state write rendered again,+ // a loop that starved route changes (the Library item page could not be left).+ const quickConnectList = useMemo(+ () => props.quickConnect.filter((entry) => !isMcpDirectoryEntry(entry)),+ [props.quickConnect],+ );+ const installedSkills = props.installedSkills ?? NO_SKILLS;+ const installedCommands = props.installedCommands ?? NO_COMMANDS;+ const installedAgents = props.installedAgents ?? NO_AGENTS;+ const availableConnectMcpServers = NO_CONNECT_MCPS;+ const installedPlugins = useMemo(+ () => (props.installedPlugins ?? NO_PLUGINS).filter((plugin) => !plugin.files.some((file) => file.objectType === "mcp")),+ [props.installedPlugins],+ );+ const orgMcpItems = NO_ORG_MCP_ITEMS;// The supported-servers panel stands for the MCP part of the library.const mcpPanelShown = Boolean(mcpPolicy) && (filter === "all" || filter === "mcp") && inventoryState === "all";const addSupportedMcp = async (preset: OmniRushMcpPreset) => {@@ -780,6 +825,8 @@ export function McpView(props: McpViewProps) {return;}const resolved = resolveExtensionDetailTarget(detailId, libraryDetailLists);+ // The same item as before: nothing to set or read again.+ if (resolved && sameExtensionDetailTarget(detailTargetRef.current, resolved)) return;setDetailTarget(resolved);if (resolved?.kind === "skill") {setDetailSkillContent(resolved.skill.content ?? null);diff --git a/apps/app/tests/library-detail-route.test.tsx b/apps/app/tests/library-detail-route.test.tsxnew file mode 100644index 00000000..0e0eb576--- /dev/null+++ b/apps/app/tests/library-detail-route.test.tsx@@ -0,0 +1,103 @@+/** @jsxImportSource react */+import { afterAll, describe, expect, mock, test } from "bun:test";+import { GlobalRegistrator } from "@happy-dom/global-registrator";++const registered = typeof globalThis.window === "undefined";+if (registered) GlobalRegistrator.register({ url: "http://localhost/" });+afterAll(async () => {+ if (registered) await GlobalRegistrator.unregister();+});++// McpView reads the Cloud session and Den sign-in; signed out is enough here.+const signedOutClient = new Proxy({}, { get: () => async () => [] });+mock.module("../src/react-app/domains/settings/cloud/cloud-session-provider", () => ({+ useCloudSession: () => ({+ isSignedIn: false,+ authToken: "",+ activeOrganization: null,+ activeOrgName: null,+ client: signedOutClient,+ }),+}));+mock.module("../src/react-app/domains/cloud/den-auth-provider", () => ({+ useDenAuth: () => ({ isSignedIn: false, status: "signed_out" }),+}));++const { Profiler, createElement } = await import("react");+const { createRoot } = await import("react-dom/client");+const { flushSync } = await import("react-dom");+const { McpView, sameExtensionDetailTarget } = await import("../src/react-app/domains/settings/pages/mcp-view");+const { libraryCommandDetailId } = await import("../src/react-app/domains/settings/library");++type McpViewProps = Parameters<typeof McpView>[0];++const initCommand = { id: "cmd:init", name: "init", description: "guided AGENTS.md setup", template: "Create AGENTS.md" };+const reviewCommand = { id: "cmd:review", name: "review", description: "review changes", template: "Review" };++function props(detailId: string | null, onDetailIdChange: (id: string | null) => void): McpViewProps {+ return {+ busy: false,+ selectedWorkspaceRoot: "/tmp/project",+ isRemoteWorkspace: false,+ mcpServers: [],+ mcpStatus: null,+ mcpLastUpdatedAt: null,+ mcpStatuses: {},+ mcpConnectingName: null,+ allowManageExtensions: true,+ selectedMcp: null,+ setSelectedMcp: () => {},+ quickConnect: [],+ connectMcp: async () => ({ ok: true }) as never,+ authorizeMcp: () => {},+ logoutMcpAuth: () => {},+ removeMcp: () => {},+ // The parent's lists keep their identity between renders (state and memos in settings-route).+ installedCommands: COMMANDS,+ installedAgents: [],+ detailId,+ onDetailIdChange,+ };+}+const COMMANDS = [initCommand, reviewCommand];++const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));++describe("Library item page", () => {+ test("an item page settles instead of rendering again and again", async () => {+ const host = document.createElement("div");+ document.body.appendChild(host);+ const root = createRoot(host);+ let commits = 0;+ const view = (detailId: string | null) =>+ createElement(Profiler, { id: "library", onRender: () => { commits += 1; } }, createElement(McpView, props(detailId, () => {})));++ flushSync(() => root.render(view(libraryCommandDetailId(initCommand))));+ await sleep(400);+ const settled = commits;+ await sleep(400);+ // Before the fix every render made new list arrays, the routed detail effect ran again and+ // set a new target object: hundreds of commits a second, and route changes never got through.+ expect(commits - settled).toBe(0);+ expect(settled).toBeLessThan(20);+ expect(host.textContent).toContain("/init");++ // Leaving the page (the back link or any navigation) shows the list again.+ flushSync(() => root.render(view(null)));+ await sleep(100);+ expect(host.textContent).not.toContain("guided AGENTS.md setupAvailable in composer");+ flushSync(() => root.unmount());+ host.remove();+ // Let the unmount's passive effects run while the DOM globals are still registered.+ await sleep(50);+ });++ test("the same item object is the same target; another object is not", () => {+ const a = { kind: "command" as const, command: initCommand };+ expect(sameExtensionDetailTarget(a, { kind: "command", command: initCommand })).toBe(true);+ expect(sameExtensionDetailTarget(a, { kind: "command", command: { ...initCommand } })).toBe(false);+ expect(sameExtensionDetailTarget(a, { kind: "command", command: reviewCommand })).toBe(false);+ expect(sameExtensionDetailTarget(a, null)).toBe(false);+ expect(sameExtensionDetailTarget(null, null)).toBe(true);+ });+});