side-pane-plugin-menu.fixture.tsx 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. import { PlatformProvider } from "@/hooks/usePlatform.js";
  2. import { parseClientConfigSnapshot, type IPlatformService } from "@zcode/shared";
  3. import { useRef, useState } from "react";
  4. import { createRoot } from "react-dom/client";
  5. import type { IServiceAccessor } from "@zcode/services";
  6. import { AnimatedSidePanePanel } from "@/app-shell/AnimatedSidePanePanel.js";
  7. import { ResizablePanel, ResizablePanelGroup } from "@/components/ui/resizable.js";
  8. import { TooltipProvider } from "@/components/ui/tooltip.js";
  9. import { ServiceProvider } from "@/hooks/useServices.js";
  10. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  11. import { PluginUiWorkspaceProvider, openPluginUiSidePane } from "@/plugin-ui/index.js";
  12. import type { OpenPluginUiSideTabRequest } from "@/plugin-ui/contract.js";
  13. import {
  14. activateNewTabSidePane,
  15. stampSidePaneTabsOwnership,
  16. type WorkspaceSidePaneState,
  17. } from "@/lib/workspaceSidePane.js";
  18. import type { PanelImperativeHandle } from "react-resizable-panels";
  19. import "@/styles.css";
  20. const params = new URLSearchParams(location.search);
  21. document.documentElement.classList.add(`theme-zai-${params.get("theme") ?? "light"}`);
  22. const workspacePath = "/workspace/example";
  23. const workspaceIdentity = "fixture-workspace";
  24. const remoteSessionId = undefined;
  25. const noop = () => {};
  26. const readCatalog = async () => (await fetch("/catalog")).json();
  27. const mutatePlugin = async (path: string, input: unknown) => {
  28. const response = await fetch(path, { method: "POST", body: JSON.stringify(input) });
  29. if (!response.ok) throw new Error("plugin operation failed");
  30. return response.json();
  31. };
  32. const services = (params.has("noBridge")
  33. ? {}
  34. : {
  35. pluginManagementService: {
  36. listPlugins: readCatalog,
  37. getPluginsOverview: readCatalog,
  38. installPlugin: (input: unknown) => mutatePlugin("/install", input),
  39. setPluginEnabled: (input: unknown) => mutatePlugin("/enable", input),
  40. },
  41. skillsService: {},
  42. clientConfigService: {
  43. getSnapshot: async () => {
  44. const response = await fetch("/client-config");
  45. if (!response.ok) throw new Error("config unavailable");
  46. return parseClientConfigSnapshot(await response.json());
  47. },
  48. },
  49. pluginUiBridgeService: {
  50. listSurfaces: async (scope: unknown) => {
  51. const response = await fetch("/surfaces", {
  52. method: "POST",
  53. body: JSON.stringify(scope),
  54. });
  55. return response.json();
  56. },
  57. },
  58. }) as unknown as IServiceAccessor;
  59. const existingTab: WorkspaceSidePaneState = {
  60. activeTabId: "whiteboard:fixture",
  61. tabs: [
  62. {
  63. id: "whiteboard:fixture",
  64. type: "whiteboard",
  65. boardId: "fixture",
  66. title: "Existing",
  67. ownerTaskId: params.has("draft") ? null : "session-fixture",
  68. },
  69. ],
  70. };
  71. function Fixture() {
  72. const [sidePaneState, setSidePaneState] = useState<WorkspaceSidePaneState | null>(
  73. // 当前产品由面板 owner 在首次展开时创建 new-tab;夹具也提供相同的入口状态。
  74. () =>
  75. params.has("existing")
  76. ? existingTab
  77. : stampSidePaneTabsOwnership(activateNewTabSidePane(null), {
  78. workspaceKey: workspaceIdentity,
  79. ownerTaskId: params.has("draft") ? null : "session-fixture",
  80. }),
  81. );
  82. const [request, setRequest] = useState<OpenPluginUiSideTabRequest | null>(null);
  83. const [opened, setOpened] = useState<WorkspaceSidePaneState | null>(null);
  84. const panelRef = useRef<PanelImperativeHandle | null>(null);
  85. const panelElementRef = useRef<HTMLDivElement | null>(null);
  86. // 本测试在 tab owner 边界验收命令与去重;沙箱/Agent 不属于启动页交互回归。
  87. const open = (next: OpenPluginUiSideTabRequest) => {
  88. setRequest(next);
  89. setOpened((current) =>
  90. openPluginUiSidePane(current, { ...next, workspaceKey: workspaceIdentity }),
  91. );
  92. };
  93. const panel = (
  94. <AnimatedSidePanePanel
  95. services={services}
  96. isVisible={!params.has("hidden")}
  97. isDesktop={!params.has("mobile")}
  98. mobileOverlay={params.has("mobile")}
  99. mobileStacked={params.has("stacked")}
  100. fullView={
  101. params.has("fullView")
  102. ? { surface: "side-pane", conversationTitle: "Conversation", onSelectSurface: noop }
  103. : null
  104. }
  105. screenshotSurfaceRequest={
  106. params.has("screenshot")
  107. ? {
  108. requestId: "fixture-screenshot",
  109. webContentsId: 1,
  110. viewport: { width: 1280, height: 720 },
  111. workspaceKey: workspaceIdentity,
  112. sessionId: "session-fixture",
  113. tabId: "browser-fixture",
  114. browserId: "browser-fixture",
  115. browserGeneration: 1,
  116. }
  117. : null
  118. }
  119. sidePaneState={sidePaneState}
  120. recentClosedSidePaneTabs={[]}
  121. isBrowserOpen={false}
  122. supportsEmbeddedBrowser={false}
  123. workspaceAbsPath={workspacePath}
  124. workspaceIdentity={workspaceIdentity}
  125. workspaceRemoteSessionId={remoteSessionId}
  126. activeTaskId={params.has("draft") ? null : "session-fixture"}
  127. sidePaneOwnerId={params.has("draft") ? null : "session-fixture"}
  128. gitState={{} as Parameters<typeof AnimatedSidePanePanel>[0]["gitState"]}
  129. activeGitSourceId="unstaged"
  130. panelRef={panelRef}
  131. panelElementRef={panelElementRef}
  132. browserNavigationRequest={null}
  133. browserRestoreUrls={{}}
  134. fileChangeFindActiveIndex={0}
  135. fileChangeFindNavigationRequestId={0}
  136. fileChangeFindQuery=""
  137. onFileChangeFindMatchCountChange={noop}
  138. onCloseCodeViewer={noop}
  139. onCloseGit={noop}
  140. onActivateTab={(id) =>
  141. setSidePaneState((current) => (current ? { ...current, activeTabId: id } : current))
  142. }
  143. onOpenNewTab={() =>
  144. setSidePaneState((current) =>
  145. stampSidePaneTabsOwnership(activateNewTabSidePane(current), {
  146. workspaceKey: workspaceIdentity,
  147. ownerTaskId: params.has("draft") ? null : "session-fixture",
  148. }),
  149. )
  150. }
  151. onReorderTab={noop}
  152. onCloseTab={noop}
  153. onCloseOtherTabs={noop}
  154. onCloseAllTabs={noop}
  155. onReopenClosedTab={noop}
  156. onOpenBrowserTab={noop}
  157. onOpenWhiteboard={noop}
  158. onOpenDeveloperTools={noop}
  159. onOpenPluginMarket={noop}
  160. onOpenTerminalTab={noop}
  161. onOpenReviewTab={noop}
  162. onOpenSelectionSideConversation={noop}
  163. onOpenBrowserUrl={noop}
  164. onOpenCodeViewer={noop}
  165. onOpenSubagentSession={noop}
  166. onRefreshGit={noop}
  167. onBrowserNavigationRequestHandled={noop}
  168. onBrowserUrlChange={noop}
  169. onBrowserPageMetadataChange={noop}
  170. onSelectGitSource={noop}
  171. />
  172. );
  173. return (
  174. <main className="h-screen min-w-0 bg-background text-foreground">
  175. <button data-testid="show-tabs" onClick={() => setSidePaneState(existingTab)}>
  176. Show existing tab
  177. </button>
  178. <output data-testid="request" hidden>
  179. {JSON.stringify(request)}
  180. </output>
  181. <output data-testid="opened" hidden>
  182. {JSON.stringify(opened)}
  183. </output>
  184. <div data-testid="panel-fixture" className="h-5/6 min-w-0">
  185. {params.has("mobile") ? (
  186. renderPanel()
  187. ) : (
  188. <ResizablePanelGroup layoutId="plugin-menu-fixture" orientation="horizontal">
  189. <ResizablePanel defaultSize={40}>
  190. <div />
  191. </ResizablePanel>
  192. {renderPanel()}
  193. </ResizablePanelGroup>
  194. )}
  195. </div>
  196. </main>
  197. );
  198. function renderPanel() {
  199. return params.has("noHost") ? (
  200. panel
  201. ) : (
  202. <PlatformProvider platform={{ pluginSandbox: {} } as IPlatformService}>
  203. <PluginUiWorkspaceProvider onOpenSidePane={open}>{panel}</PluginUiWorkspaceProvider>
  204. </PlatformProvider>
  205. );
  206. }
  207. }
  208. createRoot(document.getElementById("root")!).render(
  209. <ServiceProvider services={services}>
  210. <ZCodeIntlProvider initialLocale={params.get("locale") === "zh-CN" ? "zh-CN" : "en-US"}>
  211. <TooltipProvider>
  212. <Fixture />
  213. </TooltipProvider>
  214. </ZCodeIntlProvider>
  215. </ServiceProvider>,
  216. );