diff --git a/packages/app/e2e/browser/plugin-sidebar-items.spec.ts b/packages/app/e2e/browser/plugin-sidebar-items.spec.ts index 3acf19a860..e9858c1ab4 100644 --- a/packages/app/e2e/browser/plugin-sidebar-items.spec.ts +++ b/packages/app/e2e/browser/plugin-sidebar-items.spec.ts @@ -362,6 +362,16 @@ test.describe("Plugin sidebar items", () => { }); }); + test("a plugin screen reads workspaces through the state hooks", async ({ page }) => { + await page.setViewportSize(WIDE); + await gotoWorkspace(page, workspaceId); + await expect(headerRow(page, SHOWCASE_PLUGIN_ID, "deploys")).toBeVisible({ timeout: 30_000 }); + await runCommand(page, "Open plugin workspaces"); + await expect(screenTitle(page)).toHaveText("Workspaces"); + await expect(page.getByText(`Workspace ${workspaceId}`, { exact: true })).toBeVisible(); + await expect(page.getByText("Plugin failed", { exact: false })).toHaveCount(0); + }); + test("a header row's shortcut hint presses the row", async ({ page }) => { await page.setViewportSize(WIDE); await gotoWorkspace(page, workspaceId); diff --git a/packages/app/e2e/support/helpers/plugin-sidebar-items.ts b/packages/app/e2e/support/helpers/plugin-sidebar-items.ts index 0722bd8317..cdc6de753f 100644 --- a/packages/app/e2e/support/helpers/plugin-sidebar-items.ts +++ b/packages/app/e2e/support/helpers/plugin-sidebar-items.ts @@ -15,8 +15,18 @@ export const COMPACT = { width: 390, height: 844 }; /** A plugin written against the current API: a screen, header and footer items, a command. */ const SHOWCASE_SOURCE = `import React from "react"; import { Pressable, Text, View } from "react-native"; +import { useWorkspaces } from "@getpaseo/plugin/client"; import { SidebarRow } from "@getpaseo/plugin/client/ui"; +function WorkspacesScreen({ theme }) { + const workspaces = useWorkspaces(); + return ( + + {workspaces.map((ref) => {"Workspace " + ref.id})} + + ); +} + function DeploysScreen({ theme }) { return Deploys screen body; } @@ -63,11 +73,13 @@ function Broken() { export default function contribute(client) { client.addScreen({ id: "deploys", title: "Deploys", Component: DeploysScreen }); + client.addScreen({ id: "workspaces", title: "Workspaces", Component: WorkspacesScreen }); client.addSidebarHeaderItem({ id: "deploys", title: "Deploys", Component: DeploysItem }); client.addSidebarHeaderItem({ id: "broken", title: "Broken header", Component: Broken }); client.addSidebarFooterItem({ id: "sync", title: "Sync", Component: SyncItem }); client.addSidebarFooterItem({ id: "broken", title: "Broken footer", Component: Broken }); client.addCommandCenterItem({ id: "open-deploys", title: "Open deploys", icon: "Rocket", context: "global", onSelect: (ctx) => ctx.openScreen({ screenId: "deploys" }) }); + client.addCommandCenterItem({ id: "open-workspaces", title: "Open plugin workspaces", icon: "List", context: "global", onSelect: (ctx) => ctx.openScreen({ screenId: "workspaces" }) }); return () => {}; } `; diff --git a/packages/app/src/file-panels/plugin-host.tsx b/packages/app/src/file-panels/plugin-host.tsx index 4cc96cdc3d..d62a17cade 100644 --- a/packages/app/src/file-panels/plugin-host.tsx +++ b/packages/app/src/file-panels/plugin-host.tsx @@ -1,7 +1,4 @@ -import { PluginClientStateProvider } from "@getpaseo/plugin/client/host"; import type { PluginFilePanelProps } from "@getpaseo/plugin/client"; -import { useMemo } from "react"; -import { createPluginClientStateSource } from "@/plugins/client-state/source"; import { PluginInstallationProvider } from "@/plugins/installation-provider"; import { SurfaceErrorBoundary } from "@/plugins/surface-error-boundary"; import type { PluginFilePanel } from "./panels"; @@ -11,17 +8,11 @@ export function PluginFilePanelHost({ ...props }: PluginFilePanelProps & { panel: PluginFilePanel }) { const { plugin, contribution } = panel; - const stateSource = useMemo( - () => createPluginClientStateSource(plugin.serverId), - [plugin.serverId], - ); const Component = contribution.Component; return ( - - - + ); diff --git a/packages/app/src/plugins/installation-provider.test.tsx b/packages/app/src/plugins/installation-provider.test.tsx index 9248221704..0428e3bb2f 100644 --- a/packages/app/src/plugins/installation-provider.test.tsx +++ b/packages/app/src/plugins/installation-provider.test.tsx @@ -1,5 +1,5 @@ import { PluginTranslationRuntime } from "@getpaseo/plugin/client/host"; -import { usePaseo, useRpc } from "@getpaseo/plugin/client"; +import { usePaseo, useRpc, useWorkspace, useWorkspaces } from "@getpaseo/plugin/client"; import { defineRpc } from "@getpaseo/plugin"; import { createPaseoApi, type PaseoApi } from "@getpaseo/client"; import { DaemonClient } from "@getpaseo/client/internal/daemon-client"; @@ -8,7 +8,11 @@ import React from "react"; import { renderToStaticMarkup } from "react-dom/server"; import { expect, it } from "vitest"; import { z } from "zod"; +import type { PluginWorkspaceSnapshot } from "@getpaseo/plugin"; +import type { PluginClientStateSource } from "@getpaseo/plugin/client/host"; +import type { ToastApi } from "@/components/toast-host"; import { PluginInstallationProvider } from "./installation-provider"; +import { PluginEnvironmentProvider } from "./popover"; import type { InstalledPlugin } from "./types"; const status = defineRpc({ @@ -89,3 +93,53 @@ it("gives every surface the installation's client and RPCs on its first frame", plugin.lifetime.abort(); await plugin.paseo.dispose(); }); + +it("lets every surface read state through the plugin state hooks", async () => { + const plugin = installation([]); + function Surface() { + return {useWorkspaces().length} workspaces; + } + + const markup = renderToStaticMarkup( + + + , + ); + + expect(markup).toBe("0 workspaces"); + plugin.lifetime.abort(); + await plugin.paseo.dispose(); +}); + +function pluginEnvironment(plugin: InstalledPlugin, state: PluginClientStateSource) { + return { installation: plugin, state, toast: {} as ToastApi }; +} + +it("lets a surface read the state source its host passes", async () => { + const plugin = installation([]); + // A sidebar view spans every connected host, so its source is not the installation's host. + const workspace = { id: "ws-on-another-host" } as PluginWorkspaceSnapshot; + const state: PluginClientStateSource = { + subscribe: () => () => undefined, + getWorkspace: () => workspace, + getAgent: () => null, + getProject: () => null, + getTerminal: () => null, + getSnapshot: () => ({ workspaces: [], agents: [], projects: [], terminals: [] }), + }; + function Surface() { + return {useWorkspace("ws-on-another-host")?.id}; + } + + const environment = pluginEnvironment(plugin, state); + + const markup = renderToStaticMarkup( + + + , + ); + + expect(markup).toBe("ws-on-another-host"); + plugin.lifetime.abort(); + await plugin.paseo.dispose(); +}); diff --git a/packages/app/src/plugins/installation-provider.tsx b/packages/app/src/plugins/installation-provider.tsx index 19fb3bc8ab..d5faf75b8b 100644 --- a/packages/app/src/plugins/installation-provider.tsx +++ b/packages/app/src/plugins/installation-provider.tsx @@ -2,29 +2,42 @@ import { getPluginClientApi } from "./client-state/api"; import { QueryClientProvider } from "@tanstack/react-query"; import { PaseoApiProvider, + PluginClientStateProvider, PluginComposersProvider, PluginRpcProvider, PluginTranslationProvider, + type PluginClientStateSource, } from "@getpaseo/plugin/client/host"; -import React, { type ReactNode } from "react"; +import React, { useMemo, type ReactNode } from "react"; +import { createPluginClientStateSource } from "./client-state/source"; import { pluginComposersSource } from "./composer-items/source"; import type { InstalledPlugin } from "./types"; -/** Every plugin surface renders under its installation: one query cache, one Paseo client, RPCs. */ +/** + * Every plugin surface renders under its installation: one query cache, one Paseo client, RPCs, + * and the state the plugin state hooks read. That state is scoped to the installation's host + * unless the surface passes its own, as a sidebar view does to span every connected host. + */ export function PluginInstallationProvider({ plugin, + state: surfaceState, children, }: { plugin: InstalledPlugin; + state?: PluginClientStateSource; children: ReactNode; }) { + const state = useMemo( + () => surfaceState ?? createPluginClientStateSource(plugin.serverId), + [plugin.serverId, surfaceState], + ); return ( - {children} + {children} diff --git a/packages/app/src/plugins/popover.tsx b/packages/app/src/plugins/popover.tsx index f9680ebb29..d56b69f0d5 100644 --- a/packages/app/src/plugins/popover.tsx +++ b/packages/app/src/plugins/popover.tsx @@ -1,4 +1,3 @@ -import { PluginClientStateProvider } from "@getpaseo/plugin/client/host"; import type { ReactNode } from "react"; import { View } from "react-native"; import { StyleSheet } from "react-native-unistyles"; @@ -30,8 +29,8 @@ export function PluginEnvironmentProvider({ }: PluginEnvironmentProviderProps) { return ( - - {children} + + {children} ); diff --git a/packages/app/src/plugins/timeline/view.tsx b/packages/app/src/plugins/timeline/view.tsx index 90f6e0e1a2..8a3d522a2b 100644 --- a/packages/app/src/plugins/timeline/view.tsx +++ b/packages/app/src/plugins/timeline/view.tsx @@ -1,4 +1,3 @@ -import { PluginClientStateProvider } from "@getpaseo/plugin/client/host"; import type { PluginTimelineItemProps } from "@getpaseo/plugin/client"; import type { PluginTheme } from "@getpaseo/plugin"; import React, { type ComponentType, useMemo } from "react"; @@ -8,7 +7,6 @@ import { useIsCompactFormFactor } from "@/constants/layout"; import { useHostRuntimeClient, useHosts } from "@/runtime/host-runtime"; import type { Theme } from "@/styles/theme"; import type { PluginTimelineStreamItem } from "@/types/stream"; -import { createPluginClientStateSource } from "../client-state/source"; import { useInstalledPlugin } from "../registry"; import { PluginInstallationProvider } from "../installation-provider"; import { SurfaceErrorBoundary } from "../surface-error-boundary"; @@ -59,7 +57,6 @@ function PluginTimelineItemBody({ const hostLabel = hosts.find((host) => host.serverId === serverId)?.label ?? serverId; const host = useMemo(() => ({ id: serverId, label: hostLabel }), [hostLabel, serverId]); const layout = useMemo(() => ({ compact, platform: resolvePluginPlatform() }), [compact]); - const stateSource = useMemo(() => createPluginClientStateSource(serverId), [serverId]); if (!plugin || !renderer || !parsed || !client) { return ; @@ -82,9 +79,7 @@ function PluginTimelineItemBody({ return ( - - - + ); diff --git a/packages/app/src/plugins/workspace-panels/panel.tsx b/packages/app/src/plugins/workspace-panels/panel.tsx index 096400fb66..1500f1cdf1 100644 --- a/packages/app/src/plugins/workspace-panels/panel.tsx +++ b/packages/app/src/plugins/workspace-panels/panel.tsx @@ -5,7 +5,6 @@ import type { PluginTab, } from "@getpaseo/plugin/client"; import type { PluginTheme } from "@getpaseo/plugin"; -import { PluginClientStateProvider } from "@getpaseo/plugin/client/host"; import { CircleAlert } from "lucide-react-native"; import { useMemo, useState } from "react"; import { Text, View } from "react-native"; @@ -20,7 +19,6 @@ import { useWorkspaceExists } from "@/stores/session-store-hooks"; import type { Theme } from "@/styles/theme"; import { normalizeWorkspaceOpaqueId } from "@/utils/workspace-identity"; import { usePluginHostNavigation } from "../host-navigation"; -import { createPluginClientStateSource } from "../client-state/source"; import { toPluginTheme } from "../theme"; import { pluginIconComponent } from "../icons"; import { useInstalledPlugin } from "../registry"; @@ -67,7 +65,6 @@ function PluginPanelBody({ theme }: { theme: PluginTheme }) { const hostLabel = hosts.find((host) => host.serverId === serverId)?.label ?? serverId; const host = useMemo(() => ({ id: serverId, label: hostLabel }), [hostLabel, serverId]); const layout = useMemo(() => ({ compact, platform: resolvePluginPlatform() }), [compact]); - const stateSource = useMemo(() => createPluginClientStateSource(serverId), [serverId]); const navigation = usePluginHostNavigation(serverId); if (!plugin || !contribution || !workspaceExists) { @@ -123,9 +120,7 @@ function PluginPanelBody({ theme }: { theme: PluginTheme }) { Surface={Surface} key={`${serverId}/${target.pluginId}/${target.panelId}/${target.context}`} > - - {panel} - + {panel} ); }