Move blocker flag to redux

Was trying to save 1 state by assuming the homepage will be in a busy state and the ad-detection code will finish first. But this is not true for those with a small Following count.
This commit is contained in:
infinite-persistence 2022-03-15 01:49:15 +08:00
parent 98d653a8f8
commit 745e40a83e
No known key found for this signature in database
GPG key ID: B9C3252EDC3D0AA0
8 changed files with 44 additions and 10 deletions

View file

@ -33,6 +33,7 @@ export const TOGGLE_SPLASH_ANIMATION = 'TOGGLE_SPLASH_ANIMATION';
export const SET_ACTIVE_CHANNEL = 'SET_ACTIVE_CHANNEL'; export const SET_ACTIVE_CHANNEL = 'SET_ACTIVE_CHANNEL';
export const SET_INCOGNITO = 'SET_INCOGNITO'; export const SET_INCOGNITO = 'SET_INCOGNITO';
export const SET_MOBILE_PLAYER_DIMENSIONS = 'SET_MOBILE_PLAYER_DIMENSIONS'; export const SET_MOBILE_PLAYER_DIMENSIONS = 'SET_MOBILE_PLAYER_DIMENSIONS';
export const SET_AD_BLOCKER_FOUND = 'SET_AD_BLOCKER_FOUND';
export const RELOAD_REQUIRED = 'RELOAD_REQUIRED'; export const RELOAD_REQUIRED = 'RELOAD_REQUIRED';
// Navigation // Navigation

View file

@ -1,6 +1,7 @@
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import * as SETTINGS from 'constants/settings'; import * as SETTINGS from 'constants/settings';
import { doFetchActiveLivestreams } from 'redux/actions/livestream'; import { doFetchActiveLivestreams } from 'redux/actions/livestream';
import { selectAdBlockerFound } from 'redux/selectors/app';
import { selectActiveLivestreams, selectFetchingActiveLivestreams } from 'redux/selectors/livestream'; import { selectActiveLivestreams, selectFetchingActiveLivestreams } from 'redux/selectors/livestream';
import { selectFollowedTags } from 'redux/selectors/tags'; import { selectFollowedTags } from 'redux/selectors/tags';
import { selectUserVerifiedEmail } from 'redux/selectors/user'; import { selectUserVerifiedEmail } from 'redux/selectors/user';
@ -18,6 +19,7 @@ const select = (state) => ({
activeLivestreams: selectActiveLivestreams(state), activeLivestreams: selectActiveLivestreams(state),
fetchingActiveLivestreams: selectFetchingActiveLivestreams(state), fetchingActiveLivestreams: selectFetchingActiveLivestreams(state),
hideScheduledLivestreams: selectClientSetting(state, SETTINGS.HIDE_SCHEDULED_LIVESTREAMS), hideScheduledLivestreams: selectClientSetting(state, SETTINGS.HIDE_SCHEDULED_LIVESTREAMS),
adBlockerFound: selectAdBlockerFound(state),
}); });
const perform = (dispatch) => ({ const perform = (dispatch) => ({

View file

@ -32,6 +32,7 @@ type Props = {
doFetchActiveLivestreams: () => void, doFetchActiveLivestreams: () => void,
fetchingActiveLivestreams: boolean, fetchingActiveLivestreams: boolean,
hideScheduledLivestreams: boolean, hideScheduledLivestreams: boolean,
adBlockerFound: ?boolean,
}; };
function HomePage(props: Props) { function HomePage(props: Props) {
@ -45,6 +46,7 @@ function HomePage(props: Props) {
doFetchActiveLivestreams, doFetchActiveLivestreams,
fetchingActiveLivestreams, fetchingActiveLivestreams,
hideScheduledLivestreams, hideScheduledLivestreams,
adBlockerFound,
} = props; } = props;
const showPersonalizedChannels = (authenticated || !IS_WEB) && subscribedChannels && subscribedChannels.length > 0; const showPersonalizedChannels = (authenticated || !IS_WEB) && subscribedChannels && subscribedChannels.length > 0;
@ -104,7 +106,7 @@ function HomePage(props: Props) {
injectedItem={ injectedItem={
index === 0 && { index === 0 && {
node: <Ads small type="video" tileLayout />, node: <Ads small type="video" tileLayout />,
replace: window.odysee_ad_blocker_detected === false, replace: adBlockerFound === false,
} }
} }
/> />

View file

@ -741,3 +741,10 @@ export const doSetMobilePlayerDimensions = ({ height, width }) => ({
type: ACTIONS.SET_MOBILE_PLAYER_DIMENSIONS, type: ACTIONS.SET_MOBILE_PLAYER_DIMENSIONS,
data: { heightWidth: { height, width } }, data: { heightWidth: { height, width } },
}); });
export function doSetAdBlockerFound(found) {
return {
type: ACTIONS.SET_AD_BLOCKER_FOUND,
data: found,
};
}

View file

@ -47,6 +47,7 @@ export type AppState = {
activeChannel: ?string, activeChannel: ?string,
incognito: boolean, incognito: boolean,
mobilePlayerDimensions?: { height: number, width: number }, mobilePlayerDimensions?: { height: number, width: number },
adBlockerFound: ?boolean, // undefined = unknown; true/false = yes/no;
}; };
const defaultState: AppState = { const defaultState: AppState = {
@ -87,6 +88,7 @@ const defaultState: AppState = {
activeChannel: undefined, activeChannel: undefined,
incognito: false, incognito: false,
mobilePlayerDimensions: undefined, mobilePlayerDimensions: undefined,
adBlockerFound: undefined,
}; };
// @@router comes from react-router // @@router comes from react-router
@ -333,6 +335,13 @@ reducers[ACTIONS.SET_MOBILE_PLAYER_DIMENSIONS] = (state, action) => {
}; };
}; };
reducers[ACTIONS.SET_AD_BLOCKER_FOUND] = (state, action) => {
return {
...state,
adBlockerFound: action.data,
};
};
reducers[ACTIONS.USER_STATE_POPULATE] = (state, action) => { reducers[ACTIONS.USER_STATE_POPULATE] = (state, action) => {
const { welcomeVersion, allowAnalytics } = action.data; const { welcomeVersion, allowAnalytics } = action.data;
return { return {

View file

@ -108,3 +108,4 @@ export const selectActiveChannelStakedLevel = (state) => {
export const selectIncognito = (state) => selectState(state).incognito; export const selectIncognito = (state) => selectState(state).incognito;
export const selectMobilePlayerDimensions = (state) => selectState(state).mobilePlayerDimensions; export const selectMobilePlayerDimensions = (state) => selectState(state).mobilePlayerDimensions;
export const selectAdBlockerFound = (state) => selectState(state).adBlockerFound;

View file

@ -1,4 +1,5 @@
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import { doSetAdBlockerFound } from 'redux/actions/app';
import { selectTheme } from 'redux/selectors/settings'; import { selectTheme } from 'redux/selectors/settings';
import { makeSelectClaimForUri, selectClaimIsNsfwForUri } from 'redux/selectors/claims'; import { makeSelectClaimForUri, selectClaimIsNsfwForUri } from 'redux/selectors/claims';
import { selectOdyseeMembershipIsPremiumPlus } from 'redux/selectors/user'; import { selectOdyseeMembershipIsPremiumPlus } from 'redux/selectors/user';
@ -11,4 +12,8 @@ const select = (state, props) => ({
userHasPremiumPlus: selectOdyseeMembershipIsPremiumPlus(state), userHasPremiumPlus: selectOdyseeMembershipIsPremiumPlus(state),
}); });
export default connect(select)(Ads); const perform = {
doSetAdBlockerFound,
};
export default connect(select, perform)(Ads);

View file

@ -32,6 +32,9 @@ const IS_ANDROID = /Android/i.test(navigator.userAgent);
// const isFirefoxAndroid = IS_ANDROID && IS_FIREFOX; // const isFirefoxAndroid = IS_ANDROID && IS_FIREFOX;
// Internal use only. One-time update flag.
let ad_blocker_detected;
type Props = { type Props = {
type: string, type: string,
tileLayout?: boolean, tileLayout?: boolean,
@ -40,6 +43,7 @@ type Props = {
isMature: boolean, isMature: boolean,
userHasPremiumPlus: boolean, userHasPremiumPlus: boolean,
className?: string, className?: string,
doSetAdBlockerFound: (boolean) => void,
}; };
function removeIfExists(querySelector) { function removeIfExists(querySelector) {
@ -48,7 +52,7 @@ function removeIfExists(querySelector) {
} }
function Ads(props: Props) { function Ads(props: Props) {
const { type = 'video', tileLayout, small, userHasPremiumPlus, className } = props; const { type = 'video', tileLayout, small, userHasPremiumPlus, className, doSetAdBlockerFound } = props;
const [shouldShowAds, setShouldShowAds] = React.useState(resolveAdVisibility()); const [shouldShowAds, setShouldShowAds] = React.useState(resolveAdVisibility());
const mobileAds = IS_ANDROID || IS_IOS; const mobileAds = IS_ANDROID || IS_IOS;
@ -58,23 +62,26 @@ function Ads(props: Props) {
const adConfig = isInEu ? AD_CONFIGS.EU : mobileAds ? AD_CONFIGS.MOBILE : AD_CONFIGS.DEFAULT; const adConfig = isInEu ? AD_CONFIGS.EU : mobileAds ? AD_CONFIGS.MOBILE : AD_CONFIGS.DEFAULT;
function resolveAdVisibility() { function resolveAdVisibility() {
// 'window.odysee_ad_blocker_detected' will be undefined at startup. // 'ad_blocker_detected' will be undefined at startup. Wait until we are
// We'll wait until we are sure it is not blocked (i.e. === false) before // sure it is not blocked (i.e. === false) before showing the component.
// showing the component. return ad_blocker_detected === false && SHOW_ADS && !userHasPremiumPlus;
return window.odysee_ad_blocker_detected === false && SHOW_ADS && !userHasPremiumPlus;
} }
useEffect(() => { useEffect(() => {
if (window.odysee_ad_blocker_detected === undefined) { if (ad_blocker_detected === undefined) {
let mounted = true; let mounted = true;
const GOOGLE_AD_URL = 'https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js'; const GOOGLE_AD_URL = 'https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js';
fetch(GOOGLE_AD_URL) fetch(GOOGLE_AD_URL)
.then((response) => { .then((response) => {
window.odysee_ad_blocker_detected = response.redirected === true; const detected = response.redirected === true;
window.odysee_ad_blocker_detected = detected;
ad_blocker_detected = detected;
doSetAdBlockerFound(detected);
}) })
.catch(() => { .catch(() => {
window.odysee_ad_blocker_detected = true; ad_blocker_detected = true;
doSetAdBlockerFound(true);
}) })
.finally(() => { .finally(() => {
if (mounted) { if (mounted) {