All files / extensions/default/src/stores useViewportGridStore.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 89 90            34x           34x                                                                                     34x                                                         34x          
import { create } from 'zustand';
import { devtools } from 'zustand/middleware';
 
/**
 * Identifier for the viewport grid store type.
 */
const PRESENTATION_TYPE_ID = 'viewportGridId';
 
/**
 * Flag to enable or disable debug mode for the store.
 * Set to `true` to enable zustand devtools.
 */
const DEBUG_STORE = false;
 
/**
 * Represents the state of the viewport grid.
 */
type ViewportGridState = {
  [key: string]: unknown;
};
 
/**
 * State shape for the Viewport Grid store.
 */
type ViewportGridStore = {
  /**
   * Type identifier for the store.
   */
  type: string;
 
  /**
   * Stores the viewport grid state as a key-value mapping.
   */
  viewportGridState: ViewportGridState;
 
  /**
   * Sets the viewport grid state for a given key.
   *
   * @param key - The key to set in the viewport grid state.
   * @param value - The value to associate with the key.
   */
  setViewportGridState: (key: string, value: unknown) => void;
 
  /**
   * Clears the entire viewport grid state.
   */
  clearViewportGridState: () => void;
};
 
/**
 * Creates the Viewport Grid store.
 *
 * @param set - The zustand set function.
 * @returns The Viewport Grid store state and actions.
 */
const createViewportGridStore = (set): ViewportGridStore => ({
  type: PRESENTATION_TYPE_ID,
  viewportGridState: {},
 
  /**
   * Sets the viewport grid state for a given key.
   */
  setViewportGridState: (key, value) =>
    set(
      state => ({
        viewportGridState: {
          ...state.viewportGridState,
          [key]: value,
        },
      }),
      false,
      'setViewportGridState'
    ),
 
  /**
   * Clears the entire viewport grid state.
   */
  clearViewportGridState: () => set({ viewportGridState: {} }, false, 'clearViewportGridState'),
});
 
/**
 * Zustand store for managing viewport grid state.
 * Applies devtools middleware when DEBUG_STORE is enabled.
 */
export const useViewportGridStore = create<ViewportGridStore>()(
  DEBUG_STORE
    ? devtools(createViewportGridStore, { name: 'ViewportGridStore' })
    : createViewportGridStore
);