plugin-store-mode-order.fixture.tsx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import { useState } from "react";
  2. import { createRoot } from "react-dom/client";
  3. import type { IBroadcastService, IServiceAccessor } from "@zcode/services";
  4. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  5. import { TooltipProvider } from "@/components/ui/tooltip.js";
  6. import { ServiceProvider } from "@/hooks/useServices.js";
  7. import { usePluginStoreOrder } from "@/hooks/usePluginStoreOrder.js";
  8. import { StoreContext } from "@/store/StoreProvider.js";
  9. import { createZCodeStore } from "@/store/index.js";
  10. import { PluginStoreListView, type PluginStoreSegment } from "@/settings/PluginStoreListView.js";
  11. import { buildStoreItems, type StorePluginItem } from "@/settings/pluginStoreListing.js";
  12. import "@/styles.css";
  13. const params = new URLSearchParams(location.search);
  14. const broadcast: IBroadcastService = {
  15. send: async () => {},
  16. onMessage: () => ({ dispose() {} }),
  17. acquireClaim: async () => ({ status: "unavailable" }),
  18. commitClaim: async () => {},
  19. releaseClaim: async () => {},
  20. tryClaim: async () => false,
  21. };
  22. const store = createZCodeStore(broadcast);
  23. store.getState().setInterfaceMode("coding");
  24. store.getState().setTheme(params.get("theme") === "light" ? "light" : "dark");
  25. const official = "zcode-plugins-official";
  26. function item(name: string, category: string, marketplace = official): StorePluginItem {
  27. return {
  28. id: `${name}@${marketplace}`,
  29. name,
  30. marketplace,
  31. installed: false,
  32. restorable: false,
  33. orphaned: false,
  34. listing: { category },
  35. };
  36. }
  37. const items =
  38. params.get("documentOrder") === "1"
  39. ? ["aaa", "documents", "spreadsheets", "presentations", "pdf"].map((name) => ({
  40. ...item(name, "productivity"),
  41. installed: true,
  42. }))
  43. : params.get("missingConfig") === "1"
  44. ? buildStoreItems({
  45. marketplaces: [],
  46. marketplaceAvailabilityKnown: true,
  47. installedPlugins: [],
  48. restorableBuiltins: [],
  49. availablePlugins: [
  50. "plugin-creator",
  51. "documents",
  52. "pdf",
  53. "presentations",
  54. "spreadsheets",
  55. "image-search",
  56. "repairable",
  57. ].map((name) => ({
  58. id: `${name}@${official}`,
  59. name,
  60. marketplace: official,
  61. installed: name !== "repairable",
  62. listing: { category: "productivity" },
  63. })),
  64. plugins: ["document-skills", "repairable"].map((name) => ({
  65. id: `${name}@${official}`,
  66. name,
  67. marketplace: official,
  68. enabled: true,
  69. source: "missing" as const,
  70. packageStatus: "missing" as const,
  71. rootPath: "",
  72. skillCount: 0,
  73. skillRootCount: 0,
  74. commandRootCount: 0,
  75. components: [],
  76. declaredMcpServerNames: [],
  77. mcpServerNames: [],
  78. enabledSource: "user" as const,
  79. })),
  80. })
  81. : [
  82. item("dev", "developer-tools"),
  83. item("other", "other"),
  84. item("legacy-guide", "guides"),
  85. { ...item("restore-legacy-sessions", "utilities"), installed: true },
  86. ...(params.get("retired") === "1"
  87. ? [item("restore-legacy-sessions", "utilities", "personal")]
  88. : []),
  89. ...(params.get("legal") === "1" ? [item("legal-example", "legal")] : []),
  90. ...Array.from({ length: 8 }, (_, i) => item(`p${i}`, "productivity")),
  91. item("personal-b", "productivity", "personal"),
  92. item("personal-a", "productivity", "personal"),
  93. ];
  94. let requests = 0;
  95. const services = {
  96. clientConfigService: {
  97. getSnapshot: async () => {
  98. requests += 1;
  99. const response = await fetch("/order");
  100. if (!response.ok) throw new Error("offline");
  101. return { pluginStoreOrder: await response.json() };
  102. },
  103. },
  104. } as unknown as IServiceAccessor;
  105. function Fixture() {
  106. const { order, refresh } = usePluginStoreOrder();
  107. const [query, setQuery] = useState("");
  108. const [segment, setSegment] = useState<PluginStoreSegment>("public");
  109. return (
  110. <main className="min-h-screen bg-background p-4 text-foreground">
  111. <button data-testid="code" onClick={() => store.getState().setInterfaceMode("coding")}>
  112. Code
  113. </button>
  114. <button data-testid="work" onClick={() => store.getState().setInterfaceMode("general")}>
  115. Work
  116. </button>
  117. <button data-testid="refresh" onClick={() => void refresh(true)}>
  118. Refresh
  119. </button>
  120. <output data-testid="requests">{requests}</output>
  121. <PluginStoreListView
  122. items={items}
  123. order={order}
  124. marketplaces={[
  125. {
  126. id: official,
  127. name: official,
  128. source: {},
  129. pluginCount: 10,
  130. featured: ["p1", "restore-legacy-sessions", "p0"],
  131. },
  132. ]}
  133. loading={false}
  134. query={query}
  135. onQueryChange={setQuery}
  136. segment={segment}
  137. onSegmentChange={setSegment}
  138. onOpenManage={() => {}}
  139. actions={{
  140. onOpenDetail: () => {},
  141. onUpdate: () => {},
  142. onInstall: () => {},
  143. onUninstall: () => {},
  144. onSetEnabled: () => {},
  145. operationId: null,
  146. togglingPluginId: null,
  147. }}
  148. />
  149. </main>
  150. );
  151. }
  152. createRoot(document.getElementById("root")!).render(
  153. <ServiceProvider services={services}>
  154. <StoreContext.Provider value={store}>
  155. <ZCodeIntlProvider initialLocale={params.get("locale") === "zh-CN" ? "zh-CN" : "en-US"}>
  156. <TooltipProvider>
  157. <Fixture />
  158. </TooltipProvider>
  159. </ZCodeIntlProvider>
  160. </StoreContext.Provider>
  161. </ServiceProvider>,
  162. );