HEX
Server: Apache
System: Linux efa57bbe-abb1-400d-2985-3b056fbc2701.secureserver.net 6.1.147-1.el9.elrepo.x86_64 #1 SMP PREEMPT_DYNAMIC Thu Jul 24 12:33:32 EDT 2025 x86_64
User: root (0)
PHP: 8.0.30.4
Disabled: NONE
Upload Files
File: //var/www/wp-content/plugins/image-optimization/modules/reviews/assets/src/hooks/use-settings.js
import { useState, createContext, useContext } from '@wordpress/element';

/**
 * Context Component.
 */
const SettingsContext = createContext( null );

export function useSettings() {
	return useContext( SettingsContext );
}

const SettingsProvider = ( { children } ) => {
	const [ rating, setRating ] = useState( 0 );
	const [ feedback, setFeedback ] = useState( '' );
	const [ currentPage, setCurrentPage ] = useState( 'ratings' );
	const [ nextButtonDisabled, setNextButtonDisabled ] = useState( true );
	const [ isOpened, setIsOpened ] = useState( true );

	// Notification
	const [ showNotification, setShowNotification ] = useState( false );
	const [ notificationMessage, setNotificationMessage ] = useState( '' );
	const [ notificationType, setNotificationType ] = useState( '' );

	return (
		<SettingsContext.Provider
			value={ {
				rating,
				setRating,
				feedback,
				setFeedback,
				currentPage,
				setCurrentPage,
				nextButtonDisabled,
				setNextButtonDisabled,
				showNotification,
				setShowNotification,
				notificationMessage,
				setNotificationMessage,
				notificationType,
				setNotificationType,
				isOpened,
				setIsOpened,
			} }
		>
			{ children }
		</SettingsContext.Provider>
	);
};

export const useNotifications = () => {
	const { setNotificationMessage, setNotificationType, setShowNotification } = useContext( SettingsContext );

	const error = ( message ) => {
		setNotificationMessage( message );
		setNotificationType( 'error' );
		setShowNotification( true );
	};

	const success = ( message ) => {
		setNotificationMessage( message );
		setNotificationType( 'success' );
		setShowNotification( true );
	};

	return {
		success,
		error,
	};
};

export default SettingsProvider;