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                3x 3x                                 4x   4x   4x         4x 3x 3x 3x 3x                     3x     4x 1x 2x 2x                      
import { atom } from "jotai";
import { atomFamily } from "jotai/utils";
 
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([] as Model[]);
 
const modelsListFilterState = atom("" as string);
 
const newModelState = atom({ name: "", apiKey: "" } as {
  name: string;
  apiKey: string;
});
 
const filteredModelsListState = atom<Array<Model>>((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 = atomFamily((modelId) =>
  atom((get) => {
    const models = get(modelsListState);
    return models.find((model) => model.name === modelId);
  }),
);
 
export {
  modelsListState,
  modelsListFilterState,
  newModelState,
  filteredModelsListState,
  currentModelState,
};