All files / public/details/integrations state.ts

94.73% Statements 18/19
100% Branches 4/4
85.71% Functions 6/7
94.44% Lines 17/18

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        2x   2x   2x 6x   2x 2x   24x 10x 6x   4x   8x 4x   8x 7x   8x         10x               2x      
import { atom } from "jotai";
 
import type { IFilterChip, ISearchAndFilter } from "./types";
 
export const filterState = atom<IFilterChip[]>([]);
 
export const filterChipsAtom = atom<ISearchAndFilter[]>([]);
 
export const filterChipsSelector = atom(
  (get) => get(filterChipsAtom),
  (get, set, newValue: ISearchAndFilter[]) => {
    const current = get(filterChipsAtom);
    const newValues = newValue.reduce(
      (acc, value) => {
        const match = acc.find((accItem) => accItem.id === value.id);
        if (!match) {
          acc.push(value);
        } else {
          match.chips = [...match.chips, ...value.chips].reduce(
            (chipAcc, chipValue) => {
              const chipMatch = chipAcc.find(
                (chipAccItem) => chipAccItem.value === chipValue.value
              );
              if (!chipMatch) {
                chipAcc.push(chipValue);
              }
              return chipAcc;
            },
            [] as IFilterChip[]
          );
        }
        return acc;
      },
      current.map((item: ISearchAndFilter) => ({
        ...item,
        chips: [...item.chips],
      }))
    );
 
    set(filterChipsAtom, newValues);
  }
);