commandsStore.ts 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. import { create } from "zustand";
  2. import type {
  3. CommandCreateParams,
  4. CommandDeleteParams,
  5. CommandSetEnabledParams,
  6. CommandUpdateParams,
  7. UserCommand,
  8. ZCodeCommand,
  9. } from "@zcode/shared";
  10. import type { ICommandsService } from "@zcode/services";
  11. interface CommandsStoreState {
  12. workspacePath: string | null;
  13. workspaceIdentity: string | null;
  14. loadedWorkspacePath: string | null;
  15. loadedWorkspaceIdentity: string | null;
  16. commands: ZCodeCommand[];
  17. userCommands: UserCommand[];
  18. pluginCommands: ZCodeCommand[];
  19. capability: { userScopeAvailable: boolean };
  20. loading: boolean;
  21. error: string | null;
  22. operatingCommandId: string | null;
  23. initialize: (
  24. workspacePath: string | undefined,
  25. commandsService: ICommandsService,
  26. workspaceIdentity?: string,
  27. ) => Promise<void>;
  28. refresh: (commandsService: ICommandsService) => Promise<void>;
  29. createCommand: (
  30. params: CommandCreateParams,
  31. commandsService: ICommandsService,
  32. ) => Promise<UserCommand | null>;
  33. updateCommand: (
  34. params: CommandUpdateParams,
  35. commandsService: ICommandsService,
  36. ) => Promise<UserCommand | null>;
  37. deleteCommand: (
  38. params: CommandDeleteParams,
  39. commandsService: ICommandsService,
  40. ) => Promise<boolean>;
  41. toggleCommand: (
  42. params: CommandSetEnabledParams,
  43. commandsService: ICommandsService,
  44. ) => Promise<void>;
  45. }
  46. const inflightLists = new Map<string, Promise<void>>();
  47. type StoreGet = () => CommandsStoreState;
  48. function listKey(workspacePath: string | undefined, workspaceIdentity?: string): string {
  49. return workspaceIdentity?.trim() || workspacePath || "__no_workspace__";
  50. }
  51. // store 是单例,Scope 菜单可在同一页面切换 target。任何异步结果写回之前都要比对
  52. // 发起时的 target key,否则慢 target 的列表会覆盖当前投影,随后 delete/toggle 会拿着旧 host
  53. // 的 filePath 在当前 target 的 host 上执行。
  54. function currentListKey(get: StoreGet): string {
  55. const { workspacePath, workspaceIdentity } = get();
  56. return listKey(workspacePath ?? undefined, workspaceIdentity ?? undefined);
  57. }
  58. export const useCommandsStore = create<CommandsStoreState>((set, get) => ({
  59. workspacePath: null,
  60. workspaceIdentity: null,
  61. loadedWorkspacePath: null,
  62. loadedWorkspaceIdentity: null,
  63. commands: [],
  64. userCommands: [],
  65. pluginCommands: [],
  66. capability: { userScopeAvailable: true },
  67. loading: false,
  68. error: null,
  69. operatingCommandId: null,
  70. initialize: async (workspacePath, commandsService, workspaceIdentity) => {
  71. const normalizedWorkspaceIdentity = workspaceIdentity?.trim() || null;
  72. set({ workspacePath: workspacePath ?? null, workspaceIdentity: normalizedWorkspaceIdentity });
  73. const key = listKey(workspacePath, normalizedWorkspaceIdentity ?? undefined);
  74. const existing = inflightLists.get(key);
  75. if (existing) {
  76. await existing;
  77. return;
  78. }
  79. const promise = (async () => {
  80. set({ loading: true, error: null });
  81. try {
  82. const result = await commandsService.list({
  83. workspacePath,
  84. ...(normalizedWorkspaceIdentity
  85. ? { workspaceIdentity: normalizedWorkspaceIdentity }
  86. : {}),
  87. });
  88. if (currentListKey(get) !== key) return;
  89. set({
  90. commands: result.commands,
  91. userCommands: result.userCommands,
  92. pluginCommands: result.pluginCommands,
  93. capability: result.capability,
  94. loadedWorkspacePath: workspacePath ?? null,
  95. loadedWorkspaceIdentity: normalizedWorkspaceIdentity,
  96. loading: false,
  97. });
  98. } catch (err) {
  99. const message = err instanceof Error ? err.message : String(err);
  100. if (currentListKey(get) !== key) return;
  101. set({ loading: false, error: message });
  102. } finally {
  103. inflightLists.delete(key);
  104. }
  105. })();
  106. inflightLists.set(key, promise);
  107. await promise;
  108. },
  109. refresh: async (commandsService) => {
  110. const { workspacePath, workspaceIdentity } = get();
  111. const key = currentListKey(get);
  112. set({ loading: true, error: null });
  113. try {
  114. const result = await commandsService.list({
  115. workspacePath: workspacePath ?? undefined,
  116. ...(workspaceIdentity ? { workspaceIdentity } : {}),
  117. });
  118. if (currentListKey(get) !== key) return;
  119. set({
  120. commands: result.commands,
  121. userCommands: result.userCommands,
  122. pluginCommands: result.pluginCommands,
  123. capability: result.capability,
  124. loadedWorkspacePath: workspacePath,
  125. loadedWorkspaceIdentity: workspaceIdentity,
  126. loading: false,
  127. });
  128. } catch (err) {
  129. const message = err instanceof Error ? err.message : String(err);
  130. if (currentListKey(get) !== key) return;
  131. set({ loading: false, error: message });
  132. }
  133. },
  134. createCommand: async (params, commandsService) => {
  135. const key = currentListKey(get);
  136. set({ error: null });
  137. try {
  138. const { command } = await commandsService.writeCommandFile(params);
  139. // 写入在发起时的 target host 上已完成,但期间若切了 Scope,
  140. // 结果不能再合并进当前 target 的投影,否则列表混入别的 host 的命令。
  141. if (currentListKey(get) !== key) return command;
  142. const { commands, userCommands } = get();
  143. set({
  144. commands: [...commands, command],
  145. userCommands: [...userCommands, command],
  146. });
  147. return command;
  148. } catch (err) {
  149. const message = err instanceof Error ? err.message : String(err);
  150. if (currentListKey(get) !== key) throw err;
  151. set({ error: message });
  152. throw err;
  153. }
  154. },
  155. updateCommand: async (params, commandsService) => {
  156. const key = currentListKey(get);
  157. set({ operatingCommandId: params.commandId, error: null });
  158. try {
  159. const { command } = await commandsService.updateCommandFile(params);
  160. if (currentListKey(get) !== key) {
  161. set({ operatingCommandId: null });
  162. return command;
  163. }
  164. const { commands, userCommands } = get();
  165. set({
  166. commands: commands.map((c) => (c.id === params.commandId ? command : c)),
  167. userCommands: userCommands.map((c) => (c.id === params.commandId ? command : c)),
  168. operatingCommandId: null,
  169. });
  170. return command;
  171. } catch (err) {
  172. const message = err instanceof Error ? err.message : String(err);
  173. set(
  174. currentListKey(get) === key
  175. ? { operatingCommandId: null, error: message }
  176. : { operatingCommandId: null },
  177. );
  178. throw err;
  179. }
  180. },
  181. deleteCommand: async (params, commandsService) => {
  182. const key = currentListKey(get);
  183. set({ operatingCommandId: params.commandId, error: null });
  184. try {
  185. await commandsService.deleteCommandFile(params);
  186. if (currentListKey(get) !== key) {
  187. set({ operatingCommandId: null });
  188. return true;
  189. }
  190. const { commands, userCommands } = get();
  191. set({
  192. commands: commands.filter((c) => c.id !== params.commandId),
  193. userCommands: userCommands.filter((c) => c.id !== params.commandId),
  194. operatingCommandId: null,
  195. });
  196. return true;
  197. } catch (err) {
  198. const message = err instanceof Error ? err.message : String(err);
  199. set(
  200. currentListKey(get) === key
  201. ? { operatingCommandId: null, error: message }
  202. : { operatingCommandId: null },
  203. );
  204. throw err;
  205. }
  206. },
  207. toggleCommand: async (params, commandsService) => {
  208. const key = currentListKey(get);
  209. set({ operatingCommandId: params.commandId, error: null });
  210. try {
  211. await commandsService.setCommandEnabled(params);
  212. if (currentListKey(get) !== key) {
  213. set({ operatingCommandId: null });
  214. return;
  215. }
  216. const { commands, userCommands } = get();
  217. set({
  218. commands: commands.map((command) =>
  219. command.id === params.commandId ? { ...command, enabled: params.enabled } : command,
  220. ),
  221. userCommands: userCommands.map((command) =>
  222. command.id === params.commandId ? { ...command, enabled: params.enabled } : command,
  223. ),
  224. operatingCommandId: null,
  225. });
  226. } catch (err) {
  227. const message = err instanceof Error ? err.message : String(err);
  228. set(
  229. currentListKey(get) === key
  230. ? { operatingCommandId: null, error: message }
  231. : { operatingCommandId: null },
  232. );
  233. throw err;
  234. }
  235. },
  236. }));