| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- import { create } from "zustand";
- import type {
- CommandCreateParams,
- CommandDeleteParams,
- CommandSetEnabledParams,
- CommandUpdateParams,
- UserCommand,
- ZCodeCommand,
- } from "@zcode/shared";
- import type { ICommandsService } from "@zcode/services";
- interface CommandsStoreState {
- workspacePath: string | null;
- workspaceIdentity: string | null;
- loadedWorkspacePath: string | null;
- loadedWorkspaceIdentity: string | null;
- commands: ZCodeCommand[];
- userCommands: UserCommand[];
- pluginCommands: ZCodeCommand[];
- capability: { userScopeAvailable: boolean };
- loading: boolean;
- error: string | null;
- operatingCommandId: string | null;
- initialize: (
- workspacePath: string | undefined,
- commandsService: ICommandsService,
- workspaceIdentity?: string,
- ) => Promise<void>;
- refresh: (commandsService: ICommandsService) => Promise<void>;
- createCommand: (
- params: CommandCreateParams,
- commandsService: ICommandsService,
- ) => Promise<UserCommand | null>;
- updateCommand: (
- params: CommandUpdateParams,
- commandsService: ICommandsService,
- ) => Promise<UserCommand | null>;
- deleteCommand: (
- params: CommandDeleteParams,
- commandsService: ICommandsService,
- ) => Promise<boolean>;
- toggleCommand: (
- params: CommandSetEnabledParams,
- commandsService: ICommandsService,
- ) => Promise<void>;
- }
- const inflightLists = new Map<string, Promise<void>>();
- type StoreGet = () => CommandsStoreState;
- function listKey(workspacePath: string | undefined, workspaceIdentity?: string): string {
- return workspaceIdentity?.trim() || workspacePath || "__no_workspace__";
- }
- // store 是单例,Scope 菜单可在同一页面切换 target。任何异步结果写回之前都要比对
- // 发起时的 target key,否则慢 target 的列表会覆盖当前投影,随后 delete/toggle 会拿着旧 host
- // 的 filePath 在当前 target 的 host 上执行。
- function currentListKey(get: StoreGet): string {
- const { workspacePath, workspaceIdentity } = get();
- return listKey(workspacePath ?? undefined, workspaceIdentity ?? undefined);
- }
- export const useCommandsStore = create<CommandsStoreState>((set, get) => ({
- workspacePath: null,
- workspaceIdentity: null,
- loadedWorkspacePath: null,
- loadedWorkspaceIdentity: null,
- commands: [],
- userCommands: [],
- pluginCommands: [],
- capability: { userScopeAvailable: true },
- loading: false,
- error: null,
- operatingCommandId: null,
- initialize: async (workspacePath, commandsService, workspaceIdentity) => {
- const normalizedWorkspaceIdentity = workspaceIdentity?.trim() || null;
- set({ workspacePath: workspacePath ?? null, workspaceIdentity: normalizedWorkspaceIdentity });
- const key = listKey(workspacePath, normalizedWorkspaceIdentity ?? undefined);
- const existing = inflightLists.get(key);
- if (existing) {
- await existing;
- return;
- }
- const promise = (async () => {
- set({ loading: true, error: null });
- try {
- const result = await commandsService.list({
- workspacePath,
- ...(normalizedWorkspaceIdentity
- ? { workspaceIdentity: normalizedWorkspaceIdentity }
- : {}),
- });
- if (currentListKey(get) !== key) return;
- set({
- commands: result.commands,
- userCommands: result.userCommands,
- pluginCommands: result.pluginCommands,
- capability: result.capability,
- loadedWorkspacePath: workspacePath ?? null,
- loadedWorkspaceIdentity: normalizedWorkspaceIdentity,
- loading: false,
- });
- } catch (err) {
- const message = err instanceof Error ? err.message : String(err);
- if (currentListKey(get) !== key) return;
- set({ loading: false, error: message });
- } finally {
- inflightLists.delete(key);
- }
- })();
- inflightLists.set(key, promise);
- await promise;
- },
- refresh: async (commandsService) => {
- const { workspacePath, workspaceIdentity } = get();
- const key = currentListKey(get);
- set({ loading: true, error: null });
- try {
- const result = await commandsService.list({
- workspacePath: workspacePath ?? undefined,
- ...(workspaceIdentity ? { workspaceIdentity } : {}),
- });
- if (currentListKey(get) !== key) return;
- set({
- commands: result.commands,
- userCommands: result.userCommands,
- pluginCommands: result.pluginCommands,
- capability: result.capability,
- loadedWorkspacePath: workspacePath,
- loadedWorkspaceIdentity: workspaceIdentity,
- loading: false,
- });
- } catch (err) {
- const message = err instanceof Error ? err.message : String(err);
- if (currentListKey(get) !== key) return;
- set({ loading: false, error: message });
- }
- },
- createCommand: async (params, commandsService) => {
- const key = currentListKey(get);
- set({ error: null });
- try {
- const { command } = await commandsService.writeCommandFile(params);
- // 写入在发起时的 target host 上已完成,但期间若切了 Scope,
- // 结果不能再合并进当前 target 的投影,否则列表混入别的 host 的命令。
- if (currentListKey(get) !== key) return command;
- const { commands, userCommands } = get();
- set({
- commands: [...commands, command],
- userCommands: [...userCommands, command],
- });
- return command;
- } catch (err) {
- const message = err instanceof Error ? err.message : String(err);
- if (currentListKey(get) !== key) throw err;
- set({ error: message });
- throw err;
- }
- },
- updateCommand: async (params, commandsService) => {
- const key = currentListKey(get);
- set({ operatingCommandId: params.commandId, error: null });
- try {
- const { command } = await commandsService.updateCommandFile(params);
- if (currentListKey(get) !== key) {
- set({ operatingCommandId: null });
- return command;
- }
- const { commands, userCommands } = get();
- set({
- commands: commands.map((c) => (c.id === params.commandId ? command : c)),
- userCommands: userCommands.map((c) => (c.id === params.commandId ? command : c)),
- operatingCommandId: null,
- });
- return command;
- } catch (err) {
- const message = err instanceof Error ? err.message : String(err);
- set(
- currentListKey(get) === key
- ? { operatingCommandId: null, error: message }
- : { operatingCommandId: null },
- );
- throw err;
- }
- },
- deleteCommand: async (params, commandsService) => {
- const key = currentListKey(get);
- set({ operatingCommandId: params.commandId, error: null });
- try {
- await commandsService.deleteCommandFile(params);
- if (currentListKey(get) !== key) {
- set({ operatingCommandId: null });
- return true;
- }
- const { commands, userCommands } = get();
- set({
- commands: commands.filter((c) => c.id !== params.commandId),
- userCommands: userCommands.filter((c) => c.id !== params.commandId),
- operatingCommandId: null,
- });
- return true;
- } catch (err) {
- const message = err instanceof Error ? err.message : String(err);
- set(
- currentListKey(get) === key
- ? { operatingCommandId: null, error: message }
- : { operatingCommandId: null },
- );
- throw err;
- }
- },
- toggleCommand: async (params, commandsService) => {
- const key = currentListKey(get);
- set({ operatingCommandId: params.commandId, error: null });
- try {
- await commandsService.setCommandEnabled(params);
- if (currentListKey(get) !== key) {
- set({ operatingCommandId: null });
- return;
- }
- const { commands, userCommands } = get();
- set({
- commands: commands.map((command) =>
- command.id === params.commandId ? { ...command, enabled: params.enabled } : command,
- ),
- userCommands: userCommands.map((command) =>
- command.id === params.commandId ? { ...command, enabled: params.enabled } : command,
- ),
- operatingCommandId: null,
- });
- } catch (err) {
- const message = err instanceof Error ? err.message : String(err);
- set(
- currentListKey(get) === key
- ? { operatingCommandId: null, error: message }
- : { operatingCommandId: null },
- );
- throw err;
- }
- },
- }));
|