use-ai-consent.ts 943 B

123456789101112131415161718192021222324252627282930313233
  1. import { useCallback, useMemo, useState } from 'react'
  2. import useTutorial from '@/shared/hooks/promotions/use-tutorial'
  3. const AI_CONSENT_TUTORIAL_KEY = 'workbench-consent-release'
  4. const eventData = { name: AI_CONSENT_TUTORIAL_KEY }
  5. export { AI_CONSENT_TUTORIAL_KEY }
  6. export default function useAiConsent() {
  7. const { completeTutorial, checkCompletion } = useTutorial(
  8. AI_CONSENT_TUTORIAL_KEY,
  9. eventData
  10. )
  11. const hasGivenAiConsent = useMemo(() => checkCompletion(), [checkCompletion])
  12. const [consentError, setConsentError] = useState(false)
  13. const giveAiConsent = useCallback(async () => {
  14. setConsentError(false)
  15. try {
  16. await completeTutorial(
  17. { event: 'promo-click', action: 'complete' },
  18. { failSilently: false }
  19. )
  20. return true
  21. } catch {
  22. setConsentError(true)
  23. return false
  24. }
  25. }, [completeTutorial])
  26. return { hasGivenAiConsent, giveAiConsent, consentError }
  27. }