All files / publisher/state modelsState.ts

69.56% Statements 16/23
7.14% Branches 1/14
62.5% Functions 5/8
68.18% Lines 15/22

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              3x 3x                                 4x         4x         4x               4x     3x 3x 3x 3x                     3x       4x       2x 2x 2x                      
import { atom, selector, selectorFamily } from "recoil";
 
import { policiesListState } from "./policiesState";
 
import { Model } from "../types/shared";
 
function getFilteredModels(models: Array<Model>, filterQuery?: string | null) {
  Eif (!filterQuery) {
    return models;
  }
 
  return models.filter((model: Model) => {
    if (
      (model.name && model.name.includes(filterQuery)) ||
      (model["api-key"] && model["api-key"].includes(filterQuery)) ||
      (model["created-at"] && model["created-at"].includes(filterQuery)) ||
      (model["modified-at"] && model["modified-at"].includes(filterQuery))
    ) {
      return true;
    }
 
    return false;
  });
}
 
const modelsListState = atom({
  key: "modelsList",
  default: [] as Array<Model>,
});
 
const modelsListFilterState = atom({
  key: "modelsListFilter",
  default: "" as string,
});
 
const newModelState = atom({
  key: "newModel",
  default: {
    name: "",
    apiKey: "",
  },
});
 
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);
    },
});
 
export {
  modelsListState,
  modelsListFilterState,
  newModelState,
  filteredModelsListState,
  currentModelState,
};