All files / hooks/intlHooks/useIntlKeyStore useIntlKeyStore.js

60% Statements 12/20
33.33% Branches 2/6
66.66% Functions 4/6
57.89% Lines 11/19

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          9x   394x 394x 394x 394x       394x                         946x 946x 946x 946x     946x          
// We can use hooks within zustand store functions without violating the laws of hooks
/* eslint-disable react-hooks/rules-of-hooks */
import { useNamespace } from '@folio/stripes/core';
import { create } from 'zustand';
 
const useIntlKeyStore = create((set, get) => ({
  intlKeys: {},
  addKey: (key, namespace) => set((state) => {
    let ns = namespace;
    const folioNS = useNamespace()?.[0];
    Iif (!ns) {
      ns = folioNS;
    }
 
    return { ...state, intlKeys: { [ns]: key, ...state.intlKeys } };
  }),
  removeKey: (namespace) => set((state) => {
    let ns = namespace;
    const folioNS = useNamespace()?.[0];
    if (!ns) {
      ns = folioNS;
    }
 
    const { [ns]: _valueToRemove, ...newKeys } = state.intlKeys;
    return { ...state, intlKeys: newKeys };
  }),
  getKey: (namespace) => {
    let ns = namespace;
    const folioNS = useNamespace()?.[0];
    Eif (!ns) {
      ns = folioNS;
    }
 
    return get().intlKeys?.[ns];
  }
}));
 
export default useIntlKeyStore;