| 1 |
import apiFetch from '@wordpress/api-fetch'; |
| 2 |
import { __, sprintf } from '@wordpress/i18n'; |
| 3 |
import { create } from 'zustand'; |
| 4 |
import { devtools, persist } from 'zustand/middleware'; |
| 5 |
|
| 6 |
const storage = { |
| 7 |
setItem: (_name, store) => |
| 8 |
apiFetch({ |
| 9 |
path: '/extendify/v1/shared/update-user-meta', |
| 10 |
method: 'POST', |
| 11 |
data: { option: 'ai_consent', value: store.state.userGaveConsent }, |
| 12 |
}), |
| 13 |
}; |
| 14 |
|
| 15 |
const defaultConsentTerms = sprintf( |
| 16 |
// translators: %1$s and %2$s are opening and closing anchor tags. |
| 17 |
__( |
| 18 |
'By using AI features, you agree with the %1$sTerms of Use and Privacy Policy%2$s.', |
| 19 |
'extendify-local', |
| 20 |
), |
| 21 |
'<a href="https://hosting-ai-terms.com/" target="_blank">', |
| 22 |
'</a>', |
| 23 |
); |
| 24 |
|
| 25 |
const state = (set, get) => ({ |
| 26 |
showAIConsent: window.extSharedData?.showAIConsent ?? false, |
| 27 |
consentTerms: window.extSharedData?.consentTermsCustom || defaultConsentTerms, |
| 28 |
userGaveConsent: window.extSharedData?.userGaveConsent ?? false, |
| 29 |
setUserGaveConsent: (userGaveConsent) => set({ userGaveConsent }), |
| 30 |
// Context refers to the feature where the function is being used. |
| 31 |
shouldShowAIConsent: (context) => { |
| 32 |
const { showAIConsent, consentTerms, userGaveConsent } = get(); |
| 33 |
const enabled = showAIConsent && consentTerms; |
| 34 |
const display = { |
| 35 |
launch: enabled, |
| 36 |
draft: enabled && !userGaveConsent, |
| 37 |
'help-center': enabled && !userGaveConsent, |
| 38 |
}; |
| 39 |
return display?.[context] ?? false; |
| 40 |
}, |
| 41 |
}); |
| 42 |
|
| 43 |
export const useAIConsentStore = create( |
| 44 |
persist(devtools(state, { name: 'Extendify AI Consent' }), { |
| 45 |
name: 'extendify-ai-consent', |
| 46 |
storage, |
| 47 |
skipHydration: true, |
| 48 |
}), |
| 49 |
); |
| 50 |
|