Skip to content

Commit

Permalink
TW-1104 Fix enabling ads after creating a wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
keshan3262 committed Oct 26, 2023
1 parent 20daf32 commit 5356293
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 18 deletions.
6 changes: 3 additions & 3 deletions src/app/hooks/use-user-analytics-and-ads-settings.hook.ts
Original file line number Diff line number Diff line change
@@ -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();
Expand Down
2 changes: 2 additions & 0 deletions src/app/pages/Home/OtherComponents/Tokens/AcceptAdsBanner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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));
Expand Down
33 changes: 22 additions & 11 deletions src/app/pages/NewWallet/setWalletPassword/SetWalletPassword.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -58,16 +60,22 @@ export const SetWalletPassword: FC<SetWalletPasswordProps> = ({

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();

Expand Down Expand Up @@ -130,6 +138,7 @@ export const SetWalletPassword: FC<SetWalletPasswordProps> = ({
try {
setAdsViewEnabled(data.viewAds);
setAnalyticsEnabled(!!data.analytics);
await putToStorage(WEBSITES_ANALYTICS_ENABLED, data.viewAds && !!data.analytics);
setOnboardingCompleted(data.skipOnboarding!);

await registerWallet(password!, formatMnemonic(seedPhrase));
Expand All @@ -154,11 +163,13 @@ export const SetWalletPassword: FC<SetWalletPasswordProps> = ({
isKeystorePasswordWeak,
ownMnemonic,
keystorePassword,
setAdsViewEnabled,
setAnalyticsEnabled,
setOnboardingCompleted,
registerWallet,
seedPhrase,
trackEvent
trackEvent,
dispatch
]
);

Expand Down
6 changes: 4 additions & 2 deletions src/contentScript.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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 = '';

Expand Down
2 changes: 2 additions & 0 deletions src/lib/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export enum ContentScriptType {
ExternalLinksActivity = 'ExternalLinksActivity'
}

export const WEBSITES_ANALYTICS_ENABLED = 'WEBSITES_ANALYTICS_ENABLED';
6 changes: 4 additions & 2 deletions src/replaceAds.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit 5356293

Please sign in to comment.