All files / stores useDevicesStore.ts

100% Statements 16/16
100% Branches 4/4
100% Functions 3/3
100% Lines 16/16

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 381x 1x 1x                       1x 1x 1x 2x           2x           2x 2x 1x 1x 1x 1x 1x 1x  
import { create } from "zustand";
import { persist, createJSONStorage } from "zustand/middleware";
import { DEVICES_STORE_IDENTIFIER, DevicesStore } from "~/types/DevicesStore";
import { HubDevice } from "~/types/HubDevice";
 
/**
 * A Zustand store for managing the hub devices state of the application.
 * The store persists the devices state in localStorage.
 *
 * @example
 * ```typescript
 * const devices = useDevicesStore((state) => state.devices);
 * ```
 */
export const useDevicesStore = create<DevicesStore>()(
	persist(
		(set, get) => ({
			devices: null,
 
			/**
			 * Retrieves the current devices state.
			 * @returns {HubDevices | null} The current devices state or null if no devices are set.
			 */
			getDevices: (): HubDevice[] | null => get().devices,
 
			/**
			 * Updates the devices state with a new array of devices.
			 * @param {HubDevice[]} devices - The array of devices to set as the new state.
			 */
			updateDevices: (devices: HubDevice[]) => set({ devices }),
		}),
		{
			name: DEVICES_STORE_IDENTIFIER,
			storage: createJSONStorage(() => localStorage),
		},
	),
);