plugin-picker-order.fixture.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import { useRef, useState } from "react";
  2. import { createRoot } from "react-dom/client";
  3. import { LexicalComposer } from "@lexical/react/LexicalComposer";
  4. import { PlainTextPlugin } from "@lexical/react/LexicalPlainTextPlugin";
  5. import { ContentEditable } from "@lexical/react/LexicalContentEditable";
  6. import { LexicalErrorBoundary } from "@lexical/react/LexicalErrorBoundary";
  7. import { MentionPlugin } from "@/mentions/MentionPlugin.js";
  8. import { PromptMentionNode } from "@/mentions/nodes/PromptMentionNode.js";
  9. import { ChatPromptActionMenu } from "@/prompt-editor/ChatPromptActionMenu.js";
  10. import type { LexicalChatInputHandle } from "@/LexicalChatInput.js";
  11. import { ServiceProvider } from "@/hooks/useServices.js";
  12. import { StoreContext } from "@/store/StoreProvider.js";
  13. import { createZCodeStore } from "@/store/index.js";
  14. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  15. import { TooltipProvider } from "@/components/ui/tooltip.js";
  16. import type { IBroadcastService, IServiceAccessor } from "@zcode/services";
  17. import "@/styles.css";
  18. const params = new URLSearchParams(location.search);
  19. const store = createZCodeStore({
  20. send: async () => {},
  21. onMessage: () => ({ dispose() {} }),
  22. } as unknown as IBroadcastService);
  23. store.getState().setInterfaceMode("coding");
  24. store.getState().setTheme(params.get("theme") === "light" ? "light" : "dark");
  25. const services = {
  26. pluginManagementService: {
  27. getPluginReferenceCatalog: async (params: unknown) => {
  28. const response = await fetch("/catalog", { method: "POST", body: JSON.stringify(params) });
  29. if (!response.ok) throw new Error("catalog unavailable");
  30. return response.json();
  31. },
  32. },
  33. zcodeAgentService: { onAgentRuntimeRestarted: () => ({ dispose() {} }) },
  34. clientConfigService: {
  35. getSnapshot: async () => {
  36. const response = await fetch("/order");
  37. if (!response.ok) throw new Error("config unavailable");
  38. return { pluginStoreOrder: await response.json() };
  39. },
  40. },
  41. } as unknown as IServiceAccessor;
  42. function Fixture() {
  43. const [container, setContainer] = useState<HTMLDivElement | null>(null);
  44. const [sessionId, setSessionId] = useState<string | null>("frozen-session");
  45. const [selected, setSelected] = useState("");
  46. const input = useRef({
  47. getText: () => "",
  48. focus: () => document.querySelector<HTMLElement>('[data-testid="editor"]')?.focus(),
  49. getEditorState: () => undefined,
  50. insertMention: (item: { value: string }) => setSelected(item.value),
  51. } as unknown as LexicalChatInputHandle);
  52. return (
  53. <main className="min-h-screen bg-background p-4 text-foreground">
  54. <button data-testid="code" onClick={() => store.getState().setInterfaceMode("coding")}>
  55. Code
  56. </button>
  57. <button data-testid="work" onClick={() => store.getState().setInterfaceMode("general")}>
  58. Work
  59. </button>
  60. <button data-testid="draft" onClick={() => setSessionId(null)}>
  61. Draft
  62. </button>
  63. <output data-testid="selected">{selected}</output>
  64. <div ref={setContainer} className="mt-80 w-full">
  65. <LexicalComposer
  66. initialConfig={{
  67. namespace: "picker-order",
  68. nodes: [PromptMentionNode],
  69. onError: (error) => {
  70. throw error;
  71. },
  72. }}
  73. >
  74. <PlainTextPlugin
  75. contentEditable={
  76. <ContentEditable data-testid="editor" className="min-h-12 border p-2" />
  77. }
  78. placeholder={null}
  79. ErrorBoundary={LexicalErrorBoundary}
  80. />
  81. <MentionPlugin
  82. workspacePath="/fixture"
  83. workspaceIdentity="ssh:fixture"
  84. sessionId={sessionId}
  85. provider="zcode"
  86. container={container}
  87. />
  88. </LexicalComposer>
  89. <ChatPromptActionMenu
  90. actionMenuTitle="Add"
  91. workspacePath="/fixture"
  92. workspaceIdentity="ssh:fixture"
  93. sessionId={sessionId}
  94. inputApiRef={input}
  95. container={container}
  96. showPlugins
  97. />
  98. </div>
  99. </main>
  100. );
  101. }
  102. createRoot(document.getElementById("root")!).render(
  103. <ServiceProvider services={services}>
  104. <StoreContext.Provider value={store}>
  105. <ZCodeIntlProvider initialLocale={params.get("locale") === "zh-CN" ? "zh-CN" : "en-US"}>
  106. <TooltipProvider>
  107. <Fixture />
  108. </TooltipProvider>
  109. </ZCodeIntlProvider>
  110. </StoreContext.Provider>
  111. </ServiceProvider>,
  112. );