Back

93.33% Statements 28/30
100% Branches 16/16
88.88% Functions 16/18
93.33% Lines 28/30

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            7x 53x 1x   52x 40x   12x     7x 5x 6x 6x   8x 7x 3x             6x   6x 59x 6x 6x   2x     59x     2x 2x 1x   1x       3x 3x 1x   2x          
// Defence-in-depth fallback for the case where the back end hasn't been
// updated to send `experience` yet (or a unit test stubs only the legacy
// fields). `'embedded'` isn't derivable from booleans alone — the legacy
// schema can't distinguish Embedded from Theme search — so derived inline
// is the right default in that case; once the back end ships, the seeded
// `experience` value takes precedence.
const deriveExperienceFromBooleans = state => {
	if ( ! state.jetpackSettings.module_active ) {
		return 'off';
	}
	if ( state.jetpackSettings.instant_search_enabled ) {
		return 'overlay';
	}
	return 'inline';
};
 
const jetpackSettingSelectors = {
	getSearchModuleStatus: state => state.jetpackSettings,
	isModuleEnabled: state => state.jetpackSettings.module_active,
	isInstantSearchEnabled: state => state.jetpackSettings.instant_search_enabled,
	isReaderChatAvailable: state =>
		Object.prototype.hasOwnProperty.call( state.jetpackSettings, 'reader_chat' ),
	isReaderChatEnabled: state => state.jetpackSettings.reader_chat,
	isAiAnswersEnabled: state => !! state.jetpackSettings.ai_answers_enabled,
	// The stored choice, ungated — shown while the master switch is off so a
	// saved setting isn't misreported back to the user as off.
	isAiAnswersSaved: state => !! state.jetpackSettings.ai_answers_saved,
	// Treat a missing value as on, so a back end that predates the field doesn't
	// gate the toggle.
	isAiMasterEnabled: state => state.jetpackSettings.ai_master_enabled !== false,
	isSearchSuggestionsEnabled: state => !! state.jetpackSettings.search_suggestions_enabled,
	isWooCommerceSearchTemplateOverrideEnabled: state =>
		!! state.jetpackSettings.override_woocommerce_search_template,
	isUpdatingJetpackSettings: state => state.jetpackSettings.is_updating,
	isTogglingModule: state => state.jetpackSettings.is_toggling_module,
	isTogglingInstantSearch: state => state.jetpackSettings.is_toggling_instant_search,
 
	getPendingExperience: state => state.jetpackSettings.pending_experience ?? null,
 
	getActiveExperience: state =>
		state.jetpackSettings.experience ?? deriveExperienceFromBooleans( state ),
 
	getSelectedExperience: state => {
		const pending = state.jetpackSettings.pending_experience;
		if ( pending !== null && pending !== undefined ) {
			return pending;
		}
		return jetpackSettingSelectors.getActiveExperience( state );
	},
 
	isDirty: state => {
		const pending = state.jetpackSettings.pending_experience;
		if ( pending === null || pending === undefined ) {
			return false;
		}
		return pending !== jetpackSettingSelectors.getActiveExperience( state );
	},
};
 
export default jetpackSettingSelectors;