All files / brand-store/selectors index.ts

57.69% Statements 30/52
0% Branches 0/4
45.45% Functions 10/22
59.57% Lines 28/47

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                                                            10x 38x 10x 14x 10x 14x 10x     10x     3x 3x 3x 3x                     3x       10x       2x 2x 2x       10x     1x 1x 1x 1x                     1x             10x       5x 5x 5x       10x                                                                                    
import { selector, selectorFamily } from "recoil";
 
import {
  modelsListFilterState,
  modelsListState,
  policiesListState,
  policiesListFilterState,
  signingKeysListState,
  brandStoresState,
  signingKeysListFilterState,
} from "../atoms";
 
import {
  getFilteredModels,
  getFilteredPolicies,
  getFilteredSigningKeys,
} from "../utils";
 
import type {
  StoresSlice,
  CurrentStoreSlice,
  SnapsSlice,
  InvitesSlice,
  MembersSlice,
  Model,
  Policy,
  SigningKey,
} from "../types/shared";
 
// Redux selectors
const brandStoresListSelector = (state: StoresSlice) =>
  state.brandStores.brandStoresList;
const currentStoreSelector = (state: CurrentStoreSlice) =>
  state.currentStore.currentStore;
const snapsSelector = (state: SnapsSlice) => state.snaps.snaps;
const membersSelector = (state: MembersSlice) => state.members.members;
const invitesSelector = (state: InvitesSlice) => state.invites.invites;
 
// Recoil selectors
const filteredModelsListState = selector<Array<Model>>({
  key: "filteredModelsList",
  get: ({ get }) => {
    const filter = get(modelsListFilterState);
    const models = get(modelsListState);
    const policies = get(policiesListState);
    const modelsWithPolicies = models.map((model) => {
      const policy = policies.find(
        (policy) => policy["model-name"] === model.name,
      );
 
      return {
        ...model,
        "policy-revision": policy ? policy.revision : undefined,
      };
    });
 
    return getFilteredModels(modelsWithPolicies, filter);
  },
});
 
const currentModelState = selectorFamily({
  key: "currentModel",
  get:
    (modelId) =>
    ({ get }) => {
      const models = get(modelsListState);
      return models.find((model) => model.name === modelId);
    },
});
 
const filteredPoliciesListState = selector<Array<Policy>>({
  key: "filteredPoliciesList",
  get: ({ get }) => {
    const filter = get(policiesListFilterState);
    const policies = get(policiesListState);
    const signingKeys = get(signingKeysListState);
    const policiesWithKeys = policies.map((policy) => {
      const signingKey = signingKeys.find(
        (key) => key["sha3-384"] === policy["signing-key-sha3-384"],
      );
 
      return {
        ...policy,
        "signing-key-name": signingKey?.name,
      };
    });
 
    return getFilteredPolicies(policiesWithKeys, filter);
  },
  set: ({ set }, newValue) => {
    set(policiesListState, newValue);
  },
});
 
const brandStoreState = selectorFamily({
  key: "brandStore",
  get:
    (storeId) =>
    ({ get }) => {
      const brandStores = get(brandStoresState);
      return brandStores.find((store) => store.id === storeId);
    },
});
 
const filteredSigningKeysListState = selector<Array<SigningKey>>({
  key: "filteredSigningKeysList",
  get: ({ get }) => {
    const filter = get(signingKeysListFilterState);
    const policies = get(policiesListState);
    const signingKeys = get(signingKeysListState);
    const signingKeysWithPolicies = signingKeys.map((signingKey) => {
      const matchingPolicies = policies.filter((policy) => {
        return policy["signing-key-sha3-384"] === signingKey["sha3-384"];
      });
 
      const signingKeyModels: string[] = [];
 
      matchingPolicies.forEach((policy) => {
        if (!signingKeyModels.includes(policy["model-name"])) {
          signingKeyModels.push(policy["model-name"]);
        }
      });
 
      return {
        ...signingKey,
        models: signingKeyModels,
        policies: matchingPolicies,
      };
    });
 
    return getFilteredSigningKeys(signingKeysWithPolicies, filter);
  },
});
 
export {
  brandStoresListSelector,
  currentStoreSelector,
  snapsSelector,
  membersSelector,
  invitesSelector,
  filteredModelsListState,
  currentModelState,
  filteredPoliciesListState,
  brandStoreState,
  filteredSigningKeysListState,
};