add comment action logic

This commit is contained in:
Robert 2024-03-09 03:15:03 +07:00
parent e4a2ed92b2
commit 23f7b69e2d
No known key found for this signature in database
GPG Key ID: F631C7FD957D5F22

View File

@ -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<ChainState>((set, get) => ({
@ -59,4 +63,29 @@ export const useChainState = create<ChainState>((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;
}),
}));