All files / extensions/default/src/stores useUIStateStore.ts

62.5% Statements 5/8
50% Branches 1/2
25% Functions 1/4
57.14% Lines 4/7

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            34x           34x                                                                                     34x                                                         34x      
import { create } from 'zustand';
import { devtools } from 'zustand/middleware';
 
/**
 * Identifier for the UI State store type.
 */
const PRESENTATION_TYPE_ID = 'uiStateId';
 
/**
 * Flag to enable or disable debug mode for the store.
 * Set to `true` to enable zustand devtools.
 */
const DEBUG_STORE = false;
 
/**
 * Represents the UI state.
 */
type UIState = {
  [key: string]: unknown;
};
 
/**
 * State shape for the UI State store.
 */
type UIStateStore = {
  /**
   * Type identifier for the store.
   */
  type: string;
 
  /**
   * Stores the UI state as a key-value mapping.
   */
  uiState: UIState;
 
  /**
   * Sets the UI state for a given key.
   *
   * @param key - The key to set in the UI state.
   * @param value - The value to associate with the key.
   */
  setUIState: (key: string, value: unknown) => void;
 
  /**
   * Clears all UI state.
   */
  clearUIState: () => void;
};
 
/**
 * Creates the UI State store.
 *
 * @param set - The zustand set function.
 * @returns The UI State store state and actions.
 */
const createUIStateStore = (set): UIStateStore => ({
  type: PRESENTATION_TYPE_ID,
  uiState: {},
 
  /**
   * Sets the UI state for a given key.
   */
  setUIState: (key, value) =>
    set(
      state => ({
        uiState: {
          ...state.uiState,
          [key]: value,
        },
      }),
      false,
      'setUIState'
    ),
 
  /**
   * Clears all UI state.
   */
  clearUIState: () => set({ uiState: {} }, false, 'clearUIState'),
});
 
/**
 * Zustand store for managing UI state.
 * Applies devtools middleware when DEBUG_STORE is enabled.
 */
export const useUIStateStore = create<UIStateStore>()(
  DEBUG_STORE ? devtools(createUIStateStore, { name: 'UIStateStore' }) : createUIStateStore
);