diff --git a/doc/plugins/PLUGIN_AUTHORING_GUIDE.md b/doc/plugins/PLUGIN_AUTHORING_GUIDE.md index 091f90e81c..2d18119ea6 100644 --- a/doc/plugins/PLUGIN_AUTHORING_GUIDE.md +++ b/doc/plugins/PLUGIN_AUTHORING_GUIDE.md @@ -629,6 +629,9 @@ logout callback; authenticate remote account requests at their owning service. resolve its external account/organization label itself; the host does not fetch that portfolio on the plugin's behalf. The slot props and `useHostContext()` are display context, not proof of identity. +The host reserves the trigger with a neutral placeholder while account, company, +and plugin discovery load. Plugins should reserve the same space while their +external label loads and retain resolved labels during same-account refreshes. The host resets plugin state on account/company changes and keeps its built-in menu when no unique contribution exists, discovery fails, the module is missing, or rendering throws. The slot is a React-only contract; do not use a custom diff --git a/packages/plugins/sdk/src/ui/types.ts b/packages/plugins/sdk/src/ui/types.ts index 64a9ce2d06..5a5644acf6 100644 --- a/packages/plugins/sdk/src/ui/types.ts +++ b/packages/plugins/sdk/src/ui/types.ts @@ -272,7 +272,8 @@ export interface PluginDetailTabProps { } /** A single installed contribution replaces the organization menu. The host - * retains its built-in menu while loading, on ambiguity, or on render failure. + * reserves the trigger while loading and uses its built-in menu when absent, + * ambiguous, or on render failure. * These values/callbacks are presentation context, never authorization. */ export interface PluginOrganizationSwitcherProps { organizationSwitcher: { diff --git a/ui/src/api/companies-query.ts b/ui/src/api/companies-query.ts index 606d1d2bd5..b43562a5e3 100644 --- a/ui/src/api/companies-query.ts +++ b/ui/src/api/companies-query.ts @@ -73,9 +73,9 @@ const sessionQueryOptions = { * So: settled on success only. A failed session lookup leaves the list unfetched * until the query recovers, which it does on the next refetch. */ -export function useAccountIdentity(): { userId: string | null; settled: boolean } { - const { data: session, isSuccess } = useQuery(sessionQueryOptions); - return { userId: session?.user.id ?? null, settled: isSuccess }; +export function useAccountIdentity(): { userId: string | null; settled: boolean; failed: boolean } { + const { data: session, isSuccess, isError } = useQuery(sessionQueryOptions); + return { userId: session?.user.id ?? null, settled: isSuccess, failed: isError }; } /** diff --git a/ui/src/components/PluginOrganizationSwitcher.test.tsx b/ui/src/components/PluginOrganizationSwitcher.test.tsx index 866b984b02..c31cd1e31b 100644 --- a/ui/src/components/PluginOrganizationSwitcher.test.tsx +++ b/ui/src/components/PluginOrganizationSwitcher.test.tsx @@ -8,8 +8,8 @@ import type { PluginOrganizationSwitcherProps } from "@paperclipai/plugin-sdk/ui import { PluginOrganizationSwitcher } from "./PluginOrganizationSwitcher"; import { registerPluginReactComponent, registerPluginWebComponent, type ResolvedPluginSlot } from "@/plugins/slots"; -const state = vi.hoisted(() => ({ userId: "alice", companyId: "company-a", settled: true, companyListReady: true, companyIds: ["company-a", "company-b"], slots: [] as ResolvedPluginSlot[], errorMessage: null as string | null, mobile: false, collapsed: false, close: vi.fn(), signOut: vi.fn(), props: null as PluginOrganizationSwitcherProps | null })); -vi.mock("@/api/companies-query", () => ({ useAccountIdentity: () => state, useCompanyListQuery: () => ({ isSuccess: state.companyListReady, data: { unauthorized: false, companies: state.companyIds.map(id => ({ id, name: "Acme", issuePrefix: "ACME", logoUrl: "/logo" })) } }) })); +const state = vi.hoisted(() => ({ userId: "alice", companyId: "company-a", settled: true, failed: false, isLoading: false, companyListError: false, companyListReady: true, companyIds: ["company-a", "company-b"], slots: [] as ResolvedPluginSlot[], errorMessage: null as string | null, mobile: false, collapsed: false, close: vi.fn(), signOut: vi.fn(), props: null as PluginOrganizationSwitcherProps | null })); +vi.mock("@/api/companies-query", () => ({ useAccountIdentity: () => state, useCompanyListQuery: () => ({ isSuccess: state.companyListReady, isError: state.companyListError, data: { unauthorized: false, companies: state.companyIds.map(id => ({ id, name: "Acme", issuePrefix: "ACME", logoUrl: "/logo" })) } }) })); vi.mock("@/api/auth", () => ({ authApi: { getSession: async () => ({ user: { id: state.userId } }) } })); vi.mock("@/context/CompanyContext", () => ({ useCompany: () => ({ selectedCompanyId: state.companyId, selectedCompany: { name: "Acme", issuePrefix: "ACME", logoUrl: "/logo" } }) })); vi.mock("@/context/SidebarContext", () => ({ useSidebar: () => ({ isMobile: state.mobile, setSidebarOpen: state.close, collapsed: state.collapsed, peeking: false }) })); @@ -26,7 +26,7 @@ function render() { } afterEach(() => { if (root) flushSync(() => root!.unmount()); root = undefined; container?.remove(); client.clear(); vi.restoreAllMocks(); - Object.assign(state, { userId: "alice", companyId: "company-a", settled: true, companyListReady: true, companyIds: ["company-a", "company-b"], slots: [], errorMessage: null, mobile: false, collapsed: false, props: null }); + Object.assign(state, { userId: "alice", companyId: "company-a", settled: true, failed: false, isLoading: false, companyListError: false, companyListReady: true, companyIds: ["company-a", "company-b"], slots: [], errorMessage: null, mobile: false, collapsed: false, props: null }); state.close.mockClear(); state.signOut.mockClear(); }); function register() { @@ -39,11 +39,29 @@ function register() { state.slots = [slot]; } describe("organization navigation replacement", () => { - it("keeps built-in navigation for absent, ambiguous, failed or unsettled discovery", () => { + it("reserves the trigger until identity, companies, discovery and the module are ready", () => { + state.settled = false; + render(); + expect(container.textContent).toBe(""); + expect(container.querySelector('[aria-busy="true"]')).not.toBeNull(); + state.settled = true; state.companyListReady = false; render(); + expect(container.textContent).toBe(""); + state.companyListReady = true; state.isLoading = true; render(); + expect(container.textContent).toBe(""); + state.slots = [{ ...slot, exportName: "Delayed" }]; render(); + expect(container.textContent).toBe(""); + registerPluginReactComponent(slot.pluginKey, "Delayed", () => ); + state.isLoading = false; render(); + expect(container.textContent).toBe("Resolved organization"); + expect(container.querySelector('[aria-busy="true"]')).toBeNull(); + }); + it("keeps built-in navigation for absent, ambiguous or failed discovery", () => { render(); expect(container.textContent).toBe("Built-in organizations"); register(); state.slots = [slot, { ...slot, id: "other" }]; render(); expect(container.textContent).toBe("Built-in organizations"); state.slots = [slot]; state.errorMessage = "offline"; render(); expect(container.textContent).toBe("Built-in organizations"); - state.errorMessage = null; state.settled = false; render(); expect(container.textContent).toBe("Built-in organizations"); + state.errorMessage = null; state.settled = false; state.failed = true; render(); expect(container.textContent).toBe("Built-in organizations"); + state.failed = false; state.settled = true; state.companyListReady = false; state.companyListError = true; + render(); expect(container.textContent).toBe("Built-in organizations"); }); it("falls back when a declared module is missing or rendering fails", () => { state.slots = [{ ...slot, exportName: "Missing" }]; render(); expect(container.textContent).toBe("Built-in organizations"); @@ -57,12 +75,14 @@ describe("organization navigation replacement", () => { state.companyListReady = false; state.props = null; render(); - expect(container.textContent).toBe("Built-in organizations"); + expect(container.querySelector('[aria-label="Loading organization"]')).not.toBeNull(); + expect(container.textContent).toBe(""); expect(state.props).toBeNull(); state.companyListReady = true; state.companyIds = ["company-b"]; render(); - expect(container.textContent).toBe("Built-in organizations"); + expect(container.querySelector('[aria-label="Loading organization"]')).not.toBeNull(); + expect(container.textContent).toBe(""); expect(state.props).toBeNull(); state.companyId = "company-b"; render(); diff --git a/ui/src/components/PluginOrganizationSwitcher.tsx b/ui/src/components/PluginOrganizationSwitcher.tsx index 19028a0d5a..baf3eb56aa 100644 --- a/ui/src/components/PluginOrganizationSwitcher.tsx +++ b/ui/src/components/PluginOrganizationSwitcher.tsx @@ -1,4 +1,5 @@ import { useState, type ReactNode } from "react"; +import { ChevronsUpDown } from "lucide-react"; import type { PluginOrganizationSwitcherProps } from "@paperclipai/plugin-sdk/ui"; import { useAccountIdentity, useCompanyListQuery } from "@/api/companies-query"; import { useCompany } from "@/context/CompanyContext"; @@ -6,14 +7,27 @@ import { useSidebar } from "@/context/SidebarContext"; import { useSignOut } from "@/hooks/useSignOut"; import { PluginSlotMount, usePluginSlots } from "@/plugins/slots"; import { CompanyPatternIcon } from "./CompanyPatternIcon"; +import { Skeleton } from "./ui/skeleton"; -/** Optional replacement; the built-in menu stays usable throughout rollout. */ +/** Reserve the trigger's space until its owner is known. Never flash another name. */ +function OrganizationSwitcherLoading({ collapsed }: { collapsed: boolean }) { + return