Back

91.66% Statements 11/12
100% Branches 0/0
83.33% Functions 5/6
88.88% Lines 8/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                      12x 12x 12x       12x 12x   12x   12x         12x                
import { useDispatch, useSelect } from '@wordpress/data';
import { useCallback } from 'react';
import { STORE_ID } from 'store';
 
/**
 * Provides AI Answers and Instant Search settings from the store,
 * along with a dispatcher for updating them.
 *
 * @return {{ isAiAnswersEnabled: boolean, isInstantSearchEnabled: boolean, isAiMasterEnabled: boolean, isAiAnswersSaved: boolean, setAiAnswersEnabled: Function }} Settings state and updater.
 */
export default function useSearchSettings() {
	const isAiAnswersEnabled = useSelect( select => select( STORE_ID ).isAiAnswersEnabled(), [] );
	const isInstantSearchEnabled = useSelect(
		select => select( STORE_ID ).isInstantSearchEnabled(),
		[]
	);
 
	const isAiMasterEnabled = useSelect( select => select( STORE_ID ).isAiMasterEnabled(), [] );
	const isAiAnswersSaved = useSelect( select => select( STORE_ID ).isAiAnswersSaved(), [] );
 
	const { updateJetpackSettings } = useDispatch( STORE_ID );
 
	const setAiAnswersEnabled = useCallback(
		value => updateJetpackSettings( { ai_answers_enabled: value } ),
		[ updateJetpackSettings ]
	);
 
	return {
		isAiAnswersEnabled,
		isInstantSearchEnabled,
		isAiMasterEnabled,
		isAiAnswersSaved,
		setAiAnswersEnabled,
	};
}