diff --git a/src/entities/chain/model.ts b/src/entities/chain/model.ts index c0a0ecf..a6c6e1e 100644 --- a/src/entities/chain/model.ts +++ b/src/entities/chain/model.ts @@ -1,5 +1,8 @@ import { create } from "zustand"; import type { Chain } from "./schema"; +import type { Action } from "../action/schema"; + +const DEFAULT_WAIT_FOR = 60; // Seconds interface ChainState { chains?: Chain[]; @@ -8,6 +11,7 @@ interface ChainState { getChain: (id: string) => Chain | null; updateChain: (chain: Chain) => void; deleteChain: (id: string) => void; + addCommentAction: (chainId: string, action: Action) => void; } export const useChainState = create((set, get) => ({ @@ -59,4 +63,29 @@ export const useChainState = create((set, get) => ({ ], }; }), + + addCommentAction: (chainId, action) => + set((state) => { + const chains = state.chains; + if (!chains) return state; + + const chainIndex = chains.findIndex((value) => value._id === chainId); + if (chainIndex === -1) return state; + + const actions = chains[chainIndex].actions; + + if (typeof actions === "undefined" || actions.length === 0) { + chains[chainIndex].actions = [action]; + return state; + } + + const lastAction = actions.at(-1)!; + + if (lastAction.actionType === "comment") + actions.push({ actionType: "wait", waitFor: DEFAULT_WAIT_FOR }); + + actions.push(action); + chains[chainIndex].actions = actions; + return state; + }), }));