All files / src/store/features/entities/reducers variablesReducers.ts

94.11% Statements 48/51
87.27% Branches 48/55
80% Functions 8/10
94.11% Lines 48/51

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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230            83x         83x 83x 83x   83x                           83x 6x         83x 8x 8x 8x 8x           8x                                         8x           83x 2x 2x     2x             2x                         2x           83x 3x 3x 1x   2x                     83x 6x 6x 1x 5x 4x 4x                       1x 1x                       83x 4x 4x 4x 3x           1x 1x                         83x 1x                 83x                           83x               83x       19x                                   83x  
import {
  PayloadAction,
  CaseReducer,
  SliceCaseReducers,
} from "@reduxjs/toolkit";
import { EntitiesState } from "shared/lib/entities/entitiesTypes";
import {
  applyReparentEntityToCollection,
  applyReparentFolderToCollection,
  genEntitySymbol,
} from "shared/lib/entities/entitiesHelpers";
import { variablesAdapter } from "store/features/entities/adapters";
import { v4 as uuid } from "uuid";
import { localVariableSelectTotal } from "store/features/entities/helpers";
import { Variable, VariableType } from "shared/lib/resources/types";
import { toValidSymbol } from "shared/lib/helpers/symbols";
 
type AddVariablePayload = {
  variableId?: string;
  name?: string;
  type?: VariableType;
  length?: number;
  flags?: Record<string, string>;
};
 
type PreparedAddVariablePayload = AddVariablePayload & {
  variableId: string;
};
 
const clampLength = (length: number | undefined) =>
  Math.max(1, Math.floor(length || 1));
 
const addVariable: CaseReducer<
  EntitiesState,
  PayloadAction<PreparedAddVariablePayload>
> = (state, action) => {
  const numVariables = localVariableSelectTotal(state);
  const name = action.payload.name ?? "";
  const type = action.payload.type ?? "number";
  const symbol = genEntitySymbol(
    state,
    name ? `var_${name}` : `var_${numVariables + 1}`,
  );
 
  const newVariable: Variable =
    type === "array"
      ? {
          id: action.payload.variableId,
          name,
          symbol,
          type,
          length: clampLength(action.payload.length),
          ...(action.payload.flags !== undefined
            ? { flags: action.payload.flags }
            : {}),
        }
      : {
          id: action.payload.variableId,
          name,
          symbol,
          type,
          ...(action.payload.flags !== undefined
            ? { flags: action.payload.flags }
            : {}),
        };
 
  variablesAdapter.addOne(state.variables, newVariable);
};
 
const setVariableType: CaseReducer<
  EntitiesState,
  PayloadAction<{ variableId: string; type: VariableType }>
> = (state, action) => {
  const existingVariable = state.variables.entities[action.payload.variableId];
  Iif (!existingVariable) {
    return;
  }
  const baseVariable = {
    id: existingVariable.id,
    name: existingVariable.name,
    symbol: existingVariable.symbol,
    flags: existingVariable.flags,
  };
  const updatedVariable: Variable =
    action.payload.type === "array"
      ? {
          ...baseVariable,
          type: "array",
          length:
            existingVariable.type === "array"
              ? clampLength(existingVariable.length)
              : 1,
        }
      : {
          ...baseVariable,
          type: "number",
        };
  variablesAdapter.setOne(state.variables, updatedVariable);
};
 
const setVariableLength: CaseReducer<
  EntitiesState,
  PayloadAction<{ variableId: string; length: number }>
> = (state, action) => {
  const existingVariable = state.variables.entities[action.payload.variableId];
  if (!existingVariable || existingVariable.type !== "array") {
    return;
  }
  variablesAdapter.updateOne(state.variables, {
    id: action.payload.variableId,
    changes: {
      length: clampLength(action.payload.length),
    },
  });
};
 
const renameVariable: CaseReducer<
  EntitiesState,
  PayloadAction<{ variableId: string; name: string }>
> = (state, action) => {
  const existingVariable = state.variables.entities[action.payload.variableId];
  if (existingVariable?.name === action.payload.name) {
    return;
  } else if (existingVariable) {
    const preferredSymbol = `var_${action.payload.name}`;
    variablesAdapter.updateOne(state.variables, {
      id: action.payload.variableId,
      changes: {
        name: action.payload.name,
        symbol:
          action.payload.name.length > 0
            ? toValidSymbol(preferredSymbol) === existingVariable.symbol
              ? existingVariable.symbol
              : genEntitySymbol(state, preferredSymbol)
            : existingVariable.symbol,
      },
    });
  } else Eif (action.payload.name.length > 0) {
    variablesAdapter.addOne(state.variables, {
      id: action.payload.variableId,
      name: action.payload.name,
      symbol: genEntitySymbol(state, `var_${action.payload.name}`),
      type: "number",
    });
  }
};
 
const renameVariableFlags: CaseReducer<
  EntitiesState,
  PayloadAction<{ variableId: string; flags: Record<string, string> }>
> = (state, action) => {
  const existingVariable = state.variables.entities[action.payload.variableId];
  const numFlags = Object.values(action.payload.flags).length;
  if (existingVariable) {
    variablesAdapter.updateOne(state.variables, {
      id: action.payload.variableId,
      changes: {
        flags: action.payload.flags,
      },
    });
  } else Eif (numFlags > 0) {
    variablesAdapter.addOne(state.variables, {
      id: action.payload.variableId,
      name: "",
      symbol: "",
      type: "number",
      flags: action.payload.flags,
    });
  }
};
 
const removeVariable: CaseReducer<
  EntitiesState,
  PayloadAction<{ variableId: string }>
> = (state, action) => {
  variablesAdapter.removeOne(state.variables, action.payload.variableId);
};
 
const reparentVariablesFolder: CaseReducer<
  EntitiesState,
  PayloadAction<{
    fromPath: string;
    toPath: string;
  }>
> = (state, action) => {
  applyReparentFolderToCollection(
    state.variables.entities,
    action.payload.fromPath,
    action.payload.toPath,
  );
};
 
const reparentVariable: CaseReducer<
  EntitiesState,
  PayloadAction<{
    variableId: string;
    toPath: string;
  }>
> = (state, action) => {
  applyReparentEntityToCollection(
    state.variables.entities,
    action.payload.variableId,
    action.payload.toPath,
  );
};
 
const variablesReducers = {
  addVariable: {
    reducer: addVariable,
    prepare: (payload: AddVariablePayload = {}) => {
      return {
        payload: {
          ...payload,
          variableId: payload?.variableId ?? uuid(),
        },
      };
    },
  },
 
  renameVariable,
  renameVariableFlags,
  removeVariable,
  setVariableType,
  setVariableLength,
  reparentVariablesFolder,
  reparentVariable,
} satisfies SliceCaseReducers<EntitiesState>;
 
export default variablesReducers;