Skip to main content

useAppPersistentStorage

This document provides an overview of the useAppPersistentStorage.ts file, detailing its purpose, functionality, and usage in managing audio scenes and their association with bees in a persistent storage solution using Zustand and IndexedDB.

Overview

Purpose

The useAppPersistentStorage hook is designed to manage audio scenes and their association with bees in a persistent manner. It leverages Zustand for state management and integrates idb-keyval for IndexedDB storage.

Key Components

  • Zustand: A small, fast, and scalable state-management library.
  • IndexedDB: A way to store data persistently in users' browsers with idb-keyval providing a simple API.
  • Audio Scenes: Represents audio content tied to bees and ordered sequences.

Interfaces

BeeAudioScene

export interface BeeAudioScene {
bee: IBee;
audioScene: AudioScene | undefined;
isLooping: boolean;
}
  • bee: The bee associated with the audio scene.
  • audioScene: The audio scene linked to the bee.
  • isLooping: A flag indicating if the audio should loop.

OrderedAudioScene

export interface OrderedAudioScene {
audioScene: AudioScene;
order: number;
}
  • audioScene: The audio scene in an ordered collection.
  • order: Position of the audio scene in the sequence.

State Management

useAppPersistentStorageState

This interface defines the structure and methods used within the Zustand store:

MethodDescription
addOrderedAudioSceneAdds an audio scene to the ordered list if not present.
moveUpOrderedAudioSceneMoves an audio scene up in the order list.
moveDownOrderedAudioSceneMoves an audio scene down in the order list.
removeOrderedAudioSceneRemoves an audio scene from the order list.
removeBeeAudioSceneUnlinks an audio scene from a specific bee.
setAllBeesToAudioSceneAssigns a specific audio scene to all bees.
setCachedAudioScenesSets the cached audio scenes.
setSocketUrlUpdates the socket URL.
swapAllBeeAudioScenesReplaces all existing bee audio scenes.
updateBeeAudioSceneUpdates the audio scene for a specific bee.

State Variables:

  • beeAudioScenes: Array of BeeAudioScene.
  • cachedAudioScenes: Array of cached AudioScene.
  • orderedAudioScenes: Array of OrderedAudioScene.
  • socketUrl: URL string for socket communication.

Persistent Storage Configuration

A custom storage solution is implemented using idb-keyval:

const storage: StateStorage = {
getItem: async (name: string): Promise<string | null> => {
return (await get(name)) || null;
},
setItem: async (name: string, value: string): Promise<void> => {
await set(name, value);
},
removeItem: async (name: string): Promise<void> => {
await del(name);
},
};

The useAppPersistentStorage hook utilizes Zustand's persist middleware to save its state:

export const useAppPersistentStorage = create<useAppPersistentStorageState>()(
persist(
(set, get) => ({
// Implementation details...
}),
{
name: "kweenbTriggeringStates",
storage: createJSONStorage(() => storage),
partialize: (state) => ({
beeAudioScenes: state.beeAudioScenes,
orderedAudioScenes: state.orderedAudioScenes,
socketUrl: state.socketUrl,
}),
onRehydrateStorage: (state) => {
useAppStore.setState({ rehydrated: false });
return () => {
useAppStore.setState({ rehydrated: true });
};
},
}
)
);

Usage

By using useAppPersistentStorage, developers can easily manage and persist audio scenes and their association with bees in a frontend application. This setup ensures that state changes are consistent and long-lived across user sessions.

Conclusion

The useAppPersistentStorage.ts file provides a robust solution for managing persistent states associated with audio scenes in a bee-related application. With the combination of Zustand and IndexedDB, it efficiently handles complex state management scenarios, enhancing the application's reliability and user experience.