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

50% Statements 5/10
50% Branches 1/2
16.66% Functions 1/6
44.44% Lines 4/9

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      34x 34x                                                                                               34x                                                                                     34x          
import { create } from 'zustand';
import { devtools } from 'zustand/middleware';
 
const PRESENTATION_TYPE_ID = 'viewportsByPositionId';
const DEBUG_STORE = false;
 
/**
 * Represents the state and actions for managing viewports by position.
 */
type ViewportsByPositionState = {
  /**
   * Type identifier for the store.
   */
  type: string;
 
  /**
   * Stores viewports indexed by their position.
   */
  viewportsByPosition: Record<string, unknown>;
 
  /**
   * Stores initial display viewports as an array of strings.
   */
  initialInDisplay: string[];
 
  /**
   * Sets the viewport for a given key.
   *
   * @param key - The key identifying the viewport position.
   * @param value - The viewport data to associate with the key.
   */
  setViewportsByPosition: (key: string, value: unknown) => void;
 
  /**
   * Clears all viewports by position.
   */
  clearViewportsByPosition: () => void;
 
  /**
   * Adds an initial display viewport.
   *
   * @param value - The viewport identifier to add.
   */
  addInitialInDisplay: (value: string) => void;
};
 
/**
 * Creates the Viewports By Position store.
 *
 * @param set - The zustand set function.
 * @returns The Viewports By Position store state and actions.
 */
const createViewportsByPositionStore = (set): ViewportsByPositionState => ({
  type: PRESENTATION_TYPE_ID,
  viewportsByPosition: {},
  initialInDisplay: [],
 
  /**
   * Sets the viewport for a given key.
   */
  setViewportsByPosition: (key, value) =>
    set(
      state => ({
        viewportsByPosition: {
          ...state.viewportsByPosition,
          [key]: value,
        },
      }),
      false,
      'setViewportsByPosition'
    ),
 
  /**
   * Clears all viewports by position.
   */
  clearViewportsByPosition: () =>
    set({ viewportsByPosition: {} }, false, 'clearViewportsByPosition'),
 
  /**
   * Adds an initial display viewport.
   */
  addInitialInDisplay: value =>
    set(
      state => ({
        initialInDisplay: [...state.initialInDisplay, value],
      }),
      false,
      'addInitialInDisplay'
    ),
});
 
/**
 * Zustand store for managing viewports by position.
 * Applies devtools middleware when DEBUG_STORE is enabled.
 */
export const useViewportsByPositionStore = create<ViewportsByPositionState>()(
  DEBUG_STORE
    ? devtools(createViewportsByPositionStore, { name: 'ViewportsByPositionStore' })
    : createViewportsByPositionStore
);