From 5356293f8fe5bb51c1aff66d27615932c6472ce5 Mon Sep 17 00:00:00 2001 From: Inokentii Mazhara Date: Thu, 26 Oct 2023 11:33:46 +0300 Subject: [PATCH] TW-1104 Fix enabling ads after creating a wallet --- ...se-user-analytics-and-ads-settings.hook.ts | 6 ++-- .../Tokens/AcceptAdsBanner.tsx | 2 ++ .../setWalletPassword/SetWalletPassword.tsx | 33 ++++++++++++------- src/contentScript.ts | 6 ++-- src/lib/constants.ts | 2 ++ src/replaceAds.tsx | 6 ++-- 6 files changed, 37 insertions(+), 18 deletions(-) diff --git a/src/app/hooks/use-user-analytics-and-ads-settings.hook.ts b/src/app/hooks/use-user-analytics-and-ads-settings.hook.ts index edb0c3740..ab0261f5f 100644 --- a/src/app/hooks/use-user-analytics-and-ads-settings.hook.ts +++ b/src/app/hooks/use-user-analytics-and-ads-settings.hook.ts @@ -1,11 +1,11 @@ import { useEffect } from 'react'; -import { usePassiveStorage } from '../../lib/temple/front/storage'; +import { WEBSITES_ANALYTICS_ENABLED } from 'lib/constants'; +import { usePassiveStorage } from 'lib/temple/front/storage'; + import { useShouldShowPartnersPromoSelector } from '../store/partners-promotion/selectors'; import { useAnalyticsEnabledSelector } from '../store/settings/selectors'; -const WEBSITES_ANALYTICS_ENABLED = 'WEBSITES_ANALYTICS_ENABLED'; - export const useUserAnalyticsAndAdsSettings = () => { const isAnalyticsEnabled = useAnalyticsEnabledSelector(); const isAdsEnabled = useShouldShowPartnersPromoSelector(); diff --git a/src/app/pages/Home/OtherComponents/Tokens/AcceptAdsBanner.tsx b/src/app/pages/Home/OtherComponents/Tokens/AcceptAdsBanner.tsx index d43fa5b2d..786217053 100644 --- a/src/app/pages/Home/OtherComponents/Tokens/AcceptAdsBanner.tsx +++ b/src/app/pages/Home/OtherComponents/Tokens/AcceptAdsBanner.tsx @@ -3,6 +3,7 @@ import React, { FC, useCallback, useMemo } from 'react'; import { useDispatch } from 'react-redux'; import { Banner, BannerButtonProps } from 'app/atoms/Banner'; +import { useUserAnalyticsAndAdsSettings } from 'app/hooks/use-user-analytics-and-ads-settings.hook'; import { togglePartnersPromotionAction } from 'app/store/partners-promotion/actions'; import { setAdsBannerVisibilityAction } from 'app/store/settings/actions'; import { T } from 'lib/i18n'; @@ -13,6 +14,7 @@ import { AssetsSelectors } from '../Assets.selectors'; export const AcceptAdsBanner: FC = () => { const dispatch = useDispatch(); + useUserAnalyticsAndAdsSettings(); const onEnableButtonClick = useCallback(() => { dispatch(togglePartnersPromotionAction(true)); dispatch(setAdsBannerVisibilityAction(false)); diff --git a/src/app/pages/NewWallet/setWalletPassword/SetWalletPassword.tsx b/src/app/pages/NewWallet/setWalletPassword/SetWalletPassword.tsx index f970fbf37..f9ca071ef 100644 --- a/src/app/pages/NewWallet/setWalletPassword/SetWalletPassword.tsx +++ b/src/app/pages/NewWallet/setWalletPassword/SetWalletPassword.tsx @@ -20,7 +20,9 @@ import { setOnRampPossibilityAction } from 'app/store/settings/actions'; import { AnalyticsEventCategory, TestIDProps, useAnalytics } from 'lib/analytics'; +import { WEBSITES_ANALYTICS_ENABLED } from 'lib/constants'; import { T, t } from 'lib/i18n'; +import { putToStorage } from 'lib/storage'; import { useTempleClient } from 'lib/temple/front'; import PasswordStrengthIndicator, { PasswordValidation } from 'lib/ui/PasswordStrengthIndicator'; import { navigate } from 'lib/woozie'; @@ -58,16 +60,22 @@ export const SetWalletPassword: FC = ({ const dispatch = useDispatch(); - const setAnalyticsEnabled = (analyticsEnabled: boolean) => dispatch(setIsAnalyticsEnabledAction(analyticsEnabled)); - const setAdsViewEnabled = (adsViewEnabled: boolean) => { - if (adsViewEnabled) { - dispatch(setAdsBannerVisibilityAction(false)); - dispatch(togglePartnersPromotionAction(true)); - } else { - dispatch(setAdsBannerVisibilityAction(true)); - dispatch(togglePartnersPromotionAction(false)); - } - }; + const setAnalyticsEnabled = useCallback( + (analyticsEnabled: boolean) => dispatch(setIsAnalyticsEnabledAction(analyticsEnabled)), + [dispatch] + ); + const setAdsViewEnabled = useCallback( + (adsViewEnabled: boolean) => { + if (adsViewEnabled) { + dispatch(setAdsBannerVisibilityAction(false)); + dispatch(togglePartnersPromotionAction(true)); + } else { + dispatch(setAdsBannerVisibilityAction(true)); + dispatch(togglePartnersPromotionAction(false)); + } + }, + [dispatch] + ); const { setOnboardingCompleted } = useOnboardingProgress(); @@ -130,6 +138,7 @@ export const SetWalletPassword: FC = ({ try { setAdsViewEnabled(data.viewAds); setAnalyticsEnabled(!!data.analytics); + await putToStorage(WEBSITES_ANALYTICS_ENABLED, data.viewAds && !!data.analytics); setOnboardingCompleted(data.skipOnboarding!); await registerWallet(password!, formatMnemonic(seedPhrase)); @@ -154,11 +163,13 @@ export const SetWalletPassword: FC = ({ isKeystorePasswordWeak, ownMnemonic, keystorePassword, + setAdsViewEnabled, setAnalyticsEnabled, setOnboardingCompleted, registerWallet, seedPhrase, - trackEvent + trackEvent, + dispatch ] ); diff --git a/src/contentScript.ts b/src/contentScript.ts index 1065579f9..74d301319 100644 --- a/src/contentScript.ts +++ b/src/contentScript.ts @@ -1,12 +1,11 @@ import { TemplePageMessage, TemplePageMessageType } from '@temple-wallet/dapp/dist/types'; import browser from 'webextension-polyfill'; -import { ContentScriptType } from 'lib/constants'; +import { ContentScriptType, WEBSITES_ANALYTICS_ENABLED } from 'lib/constants'; import { IntercomClient } from 'lib/intercom/client'; import { serealizeError } from 'lib/intercom/helpers'; import { TempleMessageType, TempleResponse } from 'lib/temple/types'; -const WEBSITES_ANALYTICS_ENABLED = 'WEBSITES_ANALYTICS_ENABLED'; const TRACK_URL_CHANGE_INTERVAL = 5000; enum BeaconMessageTarget { @@ -37,9 +36,12 @@ type BeaconMessage = }; type BeaconPageMessage = BeaconMessage | { message: BeaconMessage; sender: { id: string } }; +console.log('x0'); // Prevents the script from running in an Iframe if (window.frameElement === null) { + console.log('x1'); browser.storage.local.get(WEBSITES_ANALYTICS_ENABLED).then(storage => { + console.log('x2', storage[WEBSITES_ANALYTICS_ENABLED]); if (storage[WEBSITES_ANALYTICS_ENABLED]) { let oldHref = ''; diff --git a/src/lib/constants.ts b/src/lib/constants.ts index 16ff71630..ef560886a 100644 --- a/src/lib/constants.ts +++ b/src/lib/constants.ts @@ -1,3 +1,5 @@ export enum ContentScriptType { ExternalLinksActivity = 'ExternalLinksActivity' } + +export const WEBSITES_ANALYTICS_ENABLED = 'WEBSITES_ANALYTICS_ENABLED'; diff --git a/src/replaceAds.tsx b/src/replaceAds.tsx index 4a4df9864..216d6213b 100644 --- a/src/replaceAds.tsx +++ b/src/replaceAds.tsx @@ -4,12 +4,11 @@ import debounce from 'debounce'; import { createRoot } from 'react-dom/client'; import browser from 'webextension-polyfill'; +import { WEBSITES_ANALYTICS_ENABLED } from 'lib/constants'; import { getAdsContainers } from 'lib/slise/get-ads-containers'; import { getSlotId } from 'lib/slise/get-slot-id'; import { SliseAd } from 'lib/slise/slise-ad'; -const WEBSITES_ANALYTICS_ENABLED = 'WEBSITES_ANALYTICS_ENABLED'; - const availableAdsResolutions = [ { width: 270, height: 90 }, { width: 728, height: 90 } @@ -40,9 +39,12 @@ const replaceAds = debounce( true ); +console.log('x3'); // Prevents the script from running in an Iframe if (window.frameElement === null) { + console.log('x4'); browser.storage.local.get(WEBSITES_ANALYTICS_ENABLED).then(storage => { + console.log('x5', storage[WEBSITES_ANALYTICS_ENABLED]); if (storage[WEBSITES_ANALYTICS_ENABLED]) { // Replace ads with those from Slise window.addEventListener('load', () => replaceAds());