All files / src/hooks/playbooks useEdgeConditions.ts

0% Statements 0/67
0% Branches 0/29
0% Functions 0/13
0% Lines 0/60

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133                                                                                                                                                                                                                                                                         
import { addRuleToRelationByIndex } from "../../utils/conditionals/addRuleToRelationByIndex.ts";
import { ruleOptions } from "../../utils/conditionals/ruleOptions.ts";
import { useDispatch, useSelector } from "react-redux";
import {
  addRule,
  addStepRule,
  currentPlaybookSelector,
  setCurrentPlaybookKey,
} from "../../store/features/playbook/playbookSlice.ts";
import {
  LogicalOperator,
  StepRelation,
  StepRelationContract,
} from "../../types";
import { RuleType } from "../../components/common/Conditions/types/RuleTypes.ts";
import { handleRelationRuleChange } from "../../utils/conditionals/handleRelationRuleChange.ts";
 
const playbookKey = "step_relations";
 
function useEdgeConditions(id: string, ruleSetIndex: number = 0) {
  const currentPlaybook = useSelector(currentPlaybookSelector);
  const relations = currentPlaybook?.step_relations ?? [];
  const relation = relations.find((r) => r.id === id);
  const edgeIndex = relations?.findIndex((e) => e.id === id);
  const edge = relations?.length > 0 ? relations[edgeIndex] : undefined;
  const rule_sets = edge?.condition?.rule_sets ?? [];
  const ruleSet = rule_sets?.[ruleSetIndex];
  const conditions = ruleSet?.rules ?? [];
  const condition = relation?.condition;
  const rules = ruleSet?.rules ?? [];
  const step_rules = ruleSet?.step_rules ?? [];
  const globalRule = edge?.condition?.logical_operator ?? ruleOptions[0].id;
  const dispatch = useDispatch();
 
  const setPlaybookRelations = (
    value: (StepRelation | StepRelationContract)[],
  ) => {
    dispatch(
      setCurrentPlaybookKey({
        key: playbookKey,
        value,
      }),
    );
  };
 
  const addNewRule = () => {
    Iif (!relation?.id) return;
    dispatch(addRule({ id: relation?.id, ruleSetIndex }));
  };
 
  const addNewStepRule = () => {
    Iif (!relation?.id) return;
    dispatch(addStepRule({ id: relation?.id, ruleSetIndex }));
  };
 
  const deleteRule = (conditionIndex: number) => {
    const temp = structuredClone(relations ?? []);
    const tempEdge = temp[edgeIndex];
    Iif (!tempEdge.condition) return;
    const tempRuleSet = tempEdge.condition.rule_sets?.[ruleSetIndex];
    tempRuleSet.rules = tempRuleSet?.rules.filter(
      (c, i) => i !== conditionIndex,
    );
    setPlaybookRelations(temp);
  };
 
  const deleteStepRule = (conditionIndex: number) => {
    const temp = structuredClone(relations ?? []);
    const tempEdge = temp[edgeIndex];
    Iif (!tempEdge.condition) return;
    const tempRuleSet = tempEdge.condition.rule_sets?.[ruleSetIndex];
    tempRuleSet.step_rules = tempRuleSet?.step_rules?.filter(
      (_, i) => i !== conditionIndex,
    );
    setPlaybookRelations(temp);
  };
 
  const handleRule = (
    key: string,
    value: any,
    ruleIndex: number,
    ruleType: RuleType,
  ) => {
    handleRelationRuleChange(
      key,
      value,
      edgeIndex,
      ruleIndex,
      ruleType,
      ruleSetIndex,
    );
  };
 
  const handleGlobalRule = (value: string) => {
    const temp = structuredClone(relations ?? []);
    const tempEdge = temp[edgeIndex];
    Iif (!tempEdge.condition) return;
    tempEdge.condition.logical_operator = value as LogicalOperator;
    setPlaybookRelations(temp);
  };
 
  const handleDeleteRule = (ruleType: RuleType, index: number) => {
    switch (ruleType) {
      case RuleType.RULE:
        deleteRule(index);
        break;
      case RuleType.STEP_RULE:
        deleteStepRule(index);
        break;
      default:
        return;
    }
  };
 
  return {
    playbookEdges: relations,
    edge,
    edgeIndex,
    conditions,
    globalRule,
    condition,
    rules,
    step_rules,
    handleRule,
    addNewStepRule,
    addNewRule,
    handleDeleteRule,
    handleGlobalRule,
  };
}
 
export default useEdgeConditions;