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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 defaultItems =
  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. const items =
  95. params.get("uiGroups") === "1"
  96. ? ["public", "community", "personal"].flatMap((segment) => {
  97. const marketplace = segment === "personal" ? "personal" : official;
  98. return Array.from({ length: 9 }, (_, index) => {
  99. const entry = item(
  100. `${segment}-${index < 8 ? `panel-${index}` : "regular"}`,
  101. "utilities",
  102. marketplace,
  103. );
  104. return {
  105. ...entry,
  106. listing: {
  107. ...entry.listing,
  108. publisherType:
  109. segment === "community" ? ("community" as const) : ("official" as const),
  110. },
  111. summary: {
  112. id: entry.id,
  113. name: entry.name,
  114. marketplace,
  115. installed: false,
  116. componentTypes: index < 8 ? ["ui" as const] : [],
  117. },
  118. };
  119. });
  120. })
  121. : defaultItems;
  122. let requests = 0;
  123. const services = {
  124. clientConfigService: {
  125. getSnapshot: async () => {
  126. requests += 1;
  127. const response = await fetch("/order");
  128. if (!response.ok) throw new Error("offline");
  129. return { pluginStoreOrder: await response.json() };
  130. },
  131. },
  132. } as unknown as IServiceAccessor;
  133. function Fixture() {
  134. const { order, refresh } = usePluginStoreOrder();
  135. const [query, setQuery] = useState("");
  136. const [segment, setSegment] = useState<PluginStoreSegment>("public");
  137. const [enabled, setEnabled] = useState(true);
  138. const [action, setAction] = useState("");
  139. const visibleItems =
  140. params.get("installedActions") === "1"
  141. ? [
  142. {
  143. ...item("installed-example", "productivity"),
  144. installed: true,
  145. info: { id: `installed-example@${official}`, name: "installed-example", enabled },
  146. } as StorePluginItem,
  147. item("new-example", "productivity"),
  148. ]
  149. : items;
  150. return (
  151. <main className="@container/plugin-store min-h-screen bg-background p-4 text-foreground">
  152. <button data-testid="code" onClick={() => store.getState().setInterfaceMode("coding")}>
  153. Code
  154. </button>
  155. <button data-testid="work" onClick={() => store.getState().setInterfaceMode("general")}>
  156. Work
  157. </button>
  158. <button data-testid="refresh" onClick={() => void refresh(true)}>
  159. Refresh
  160. </button>
  161. <output data-testid="requests">{requests}</output>
  162. <output data-testid="last-action">{action}</output>
  163. <PluginStoreListView
  164. items={visibleItems}
  165. order={order}
  166. marketplaces={[
  167. {
  168. id: official,
  169. name: official,
  170. source: {},
  171. pluginCount: 10,
  172. featured: ["p1", "restore-legacy-sessions", "p0"],
  173. },
  174. ]}
  175. loading={false}
  176. query={query}
  177. onQueryChange={setQuery}
  178. segment={segment}
  179. onSegmentChange={setSegment}
  180. actions={{
  181. onOpenDetail: () => setAction("detail"),
  182. onUpdate: () => {},
  183. onInstall: () => {},
  184. onUninstall: () => setAction("uninstall"),
  185. onSetEnabled: (_id, next) => setEnabled(next),
  186. activeOperations: [],
  187. togglingPluginId: null,
  188. }}
  189. />
  190. </main>
  191. );
  192. }
  193. createRoot(document.getElementById("root")!).render(
  194. <ServiceProvider services={services}>
  195. <StoreContext.Provider value={store}>
  196. <ZCodeIntlProvider initialLocale={params.get("locale") === "zh-CN" ? "zh-CN" : "en-US"}>
  197. <TooltipProvider>
  198. <Fixture />
  199. </TooltipProvider>
  200. </ZCodeIntlProvider>
  201. </StoreContext.Provider>
  202. </ServiceProvider>,
  203. );