All files / stores snackbarStore.ts

100% Statements 7/7
100% Branches 0/0
100% Functions 5/5
100% Lines 4/4

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  3x                                                     3x                               5x                 1x                
'use client';
import { create } from 'zustand';
import { type SnackbarState } from '../types/index';
 
/**
 * @module SnackbarStore
 * @author Munir Mardinli
 *
 * Zustand store for managing snackbar notifications.
 *
 * @property {Object} snack - Current snackbar state
 * @property {string} snack.message - Notification message content
 * @property {string} snack.severity - Visual style/color ('error', 'warning', 'info', 'success')
 * @property {boolean} snack.open - Visibility state
 * @property {Function} setSnack - Displays a new snackbar notification
 * @property {Function} closeSnack - Hides the current notification
 *
 * @example
 * // Access the store in a React component
 * import { useSnackStore } from './snackbarStore';
 * const { setSnack } = useSnackStore();
 *
 * // Show success message:
 * setSnack('Operation completed!', 'success');
 *
 * // Show error:
 * setSnack('Failed to save data', 'error');
 */
export const useSnackStore = create<SnackbarState>()((set) => ({
	/**
	 * Current snackbar notification state
	 * @type {Object}
	 * @property {string} message - Display message content
	 * @property {string} severity - Visual severity type
	 * @property {boolean} open - Visibility flag
	 */
	snack: { message: '', severity: '', open: false },
	/**
	 * Displays a new snackbar notification
	 * @param {string} message - The text message to display
	 * @param {string} severity - Visual style ('error'|'warning'|'info'|'success')
	 * @returns {void}
	 */
	setSnack: (message, severity): void =>
		set(() => ({
			snack: { message: message, severity: severity, open: true },
		})),
	/**
	 * Hides the currently displayed snackbar
	 * @returns {void}
	 * @description Maintains the current message/severity while hiding
	 */
	closeSnack: (): void =>
		set((state) => ({
			snack: {
				message: state.snack.message,
				severity: state.snack.severity,
				open: false,
			},
		})),
}));