All files / libs/ext/react/hooks/src/lib/use-bool-states use-bool-states.ts

100% Statements 138/138
100% Branches 16/16
100% Functions 1/1
100% Lines 138/138

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 1391x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 10x 10x 10x 10x 10x 10x 10x 10x 10x 1x 1x 1x 1x 10x 10x 10x 2x 2x 2x 2x 10x 10x 10x 1x 1x 1x 1x 10x 10x 10x 2x 2x 2x 2x 10x 10x 10x 10x 10x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 1x 11x 10x 10x 10x 10x  
import { upperFirst } from 'lodash-es';
import { useCallback, useMemo, useState } from 'react';
 
type BooleanPropertyTrue<TBooleanIdentifier extends string> = Record<
  `is${Capitalize<TBooleanIdentifier>}True`,
  boolean
>;
type BooleanPropertyFalse<TBooleanIdentifier extends string> = Record<
  `is${Capitalize<TBooleanIdentifier>}False`,
  boolean
>;
type BooleanPropertyToggle<TBooleanIdentifier extends string> = Record<
  `toggle${Capitalize<TBooleanIdentifier>}`,
  () => void
>;
type BooleanPropertySetTrue<TBooleanIdentifier extends string> = Record<
  `set${Capitalize<TBooleanIdentifier>}True`,
  () => void
>;
type BooleanPropertySetFalse<TBooleanIdentifier extends string> = Record<
  `set${Capitalize<TBooleanIdentifier>}False`,
  () => void
>;
type BooleanPropertySet<TBooleanIdentifier extends string> = Record<
  `set${Capitalize<TBooleanIdentifier>}`,
  (booleanValue: boolean) => void
>;
 
type BooleanProperties<TBooleanIdentifier extends string> = BooleanPropertyFalse<TBooleanIdentifier> &
  BooleanPropertySet<TBooleanIdentifier> &
  BooleanPropertySetFalse<TBooleanIdentifier> &
  BooleanPropertySetTrue<TBooleanIdentifier> &
  BooleanPropertyToggle<TBooleanIdentifier> &
  BooleanPropertyTrue<TBooleanIdentifier>;
 
/**
 * The goal of this type is to create a type that take each string and create a Record of <`is${Capitalize<TBooleanIdentifiers[number]>}True`, boolean>
 *
 * Example:
 *
 * ```
 * CreateBooleanPropertiesFromArray<['deleteDashboardModal', 'duplicateDashboardModal']>
 *```
 * will return
 * ```
 * {
 *   isDeleteDashboardModalTrue: boolean;
 *   isDeleteDashboardModalFalse: boolean;
 *   toggleDeleteDashboardModal: () => void;
 *   setDeleteDashboardModalTrue: () => void;
 *   setDeleteDashboardModalFalse: () => void;
 *   setDeleteDashboardModal: (booleanValue: boolean) => void;
 *
 *   isDuplicateDashboardModalTrue: boolean;
 *   isDuplicateDashboardModalFalse: boolean;
 *   toggleDuplicateDashboardModal: () => void;
 *   setDuplicateDashboardModalTrue: () => void;
 *   setDuplicateDashboardModalFalse: () => void;
 *   setDuplicateDashboardModal: (booleanValue: boolean) => void;
 * }
 * ```
 */
type CreateBooleanPropertiesFromArray<
  TBooleanIdentifiers extends readonly string[],
  TAcc extends object = Record<string, never>,
> = TBooleanIdentifiers['length'] extends 0
  ? TAcc
  : TBooleanIdentifiers extends readonly [infer TBooleanIdentifier, ...infer TRest]
    ? TBooleanIdentifier extends string
      ? TRest extends readonly string[]
        ? CreateBooleanPropertiesFromArray<TRest, BooleanProperties<TBooleanIdentifier> & TAcc>
        : never
      : never
    : never;
 
type UseBoolStatesReturn<TBooleanIdentifiers extends readonly string[]> =
  CreateBooleanPropertiesFromArray<TBooleanIdentifiers>;
 
export const useBoolStates = <
  const TBooleanIdentifiers extends readonly string[],
  const TBooleanIdentifier extends TBooleanIdentifiers[number] = TBooleanIdentifiers[number],
>(
  booleanIdentifiers: TBooleanIdentifiers,
): UseBoolStatesReturn<TBooleanIdentifiers> => {
  const [booleanValues, setBooleanValues] = useState<Record<TBooleanIdentifier, boolean>>(
    booleanIdentifiers.reduce(
      (acc, booleanIdentifier) => ({ ...acc, [booleanIdentifier]: false }),
      {} as Record<TBooleanIdentifier, boolean>,
    ),
  );
 
  const setBooleanValue = useCallback((booleanIdentifier: TBooleanIdentifier, booleanValue: boolean) => {
    setBooleanValues((currentState) => ({
      ...currentState,
      [booleanIdentifier]: booleanValue,
    }));
  }, []);
 
  const setBooleanValueTrue = useCallback((booleanIdentifier: TBooleanIdentifier) => {
    setBooleanValues((currentState) => ({
      ...currentState,
      [booleanIdentifier]: true,
    }));
  }, []);
 
  const setBooleanValueFalse = useCallback((booleanIdentifier: TBooleanIdentifier) => {
    setBooleanValues((currentState) => ({
      ...currentState,
      [booleanIdentifier]: false,
    }));
  }, []);
 
  const toggleBooleanValue = useCallback((booleanIdentifier: TBooleanIdentifier) => {
    setBooleanValues((currentState) => ({
      ...currentState,
      [booleanIdentifier]: !currentState[booleanIdentifier],
    }));
  }, []);
 
  return useMemo(
    () =>
      booleanIdentifiers.reduce((acc, booleanIdentifier) => {
        const booleanIdentifierUpper = upperFirst(booleanIdentifier as TBooleanIdentifier);
 
        return {
          ...acc,
          [`is${booleanIdentifierUpper}True`]: booleanValues[booleanIdentifier as TBooleanIdentifier] === true,
          [`is${booleanIdentifierUpper}False`]: booleanValues[booleanIdentifier as TBooleanIdentifier] === false,
          [`set${booleanIdentifierUpper}True`]: () => setBooleanValueTrue(booleanIdentifier as TBooleanIdentifier),
          [`set${booleanIdentifierUpper}False`]: () => setBooleanValueFalse(booleanIdentifier as TBooleanIdentifier),
          [`toggle${booleanIdentifierUpper}`]: () => toggleBooleanValue(booleanIdentifier as TBooleanIdentifier),
          [`set${booleanIdentifierUpper}`]: (booleanValue: boolean) =>
            setBooleanValue(booleanIdentifier as TBooleanIdentifier, booleanValue),
        };
      }, {} as UseBoolStatesReturn<TBooleanIdentifiers>),
    [booleanIdentifiers, booleanValues, setBooleanValueTrue, setBooleanValueFalse, toggleBooleanValue, setBooleanValue],
  );
};