Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add isAndroidApp to boot cache #4031

Merged
merged 6 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,19 @@ import { ButtonColor } from '../buttons/Button';
import { usePlusSubscription } from '../../hooks/usePlusSubscription';
import { LogEvent, TargetId } from '../../lib/log';
import { GooglePlayIcon } from '../icons/Google/Play';
import { checkIsBrowser, isAndroidApp, UserAgent } from '../../lib/func';
import { checkIsBrowser, UserAgent } from '../../lib/func';
import { useConditionalFeature } from '../../hooks';
import { featureOnboardingAndroid } from '../../lib/featureManagement';

const useMenuItems = (): NavItemProps[] => {
const { logout } = useAuthContext();
const { logout, isAndroidApp } = useAuthContext();
const { openModal } = useLazyModal();
const { showPrompt } = usePrompt();
const { showPlusSubscription, isPlus, logSubscriptionEvent } =
usePlusSubscription();
const { value: appExperiment } = useConditionalFeature({
feature: featureOnboardingAndroid,
shouldEvaluate: checkIsBrowser(UserAgent.Android) && !isAndroidApp(),
shouldEvaluate: checkIsBrowser(UserAgent.Android) && !isAndroidApp,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But this would still catch app even right due to webapp redirect?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rebelchris We store the isAndroidApp in the boot:local storage key now, so its persisted across visits. I tested on the chrome browser on my android phone in debug mode, and it did persist the value after redirect. I will test it on the app after merging to make sure it exhibits the same behavior

});

const onLogout = useCallback(async () => {
Expand Down
4 changes: 4 additions & 0 deletions packages/shared/src/contexts/AuthContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export interface AuthContextData {
squads?: Squad[];
isAuthReady?: boolean;
geo?: Boot['geo'];
isAndroidApp?: boolean;
}
const isExtension = checkIsExtension();
const AuthContext = React.createContext<AuthContextData>(null);
Expand Down Expand Up @@ -112,6 +113,7 @@ export type AuthContextProviderProps = {
| 'squads'
| 'refetchBoot'
| 'geo'
| 'isAndroidApp'
>;

export const AuthContextProvider = ({
Expand All @@ -130,6 +132,7 @@ export const AuthContextProvider = ({
squads,
firstLoad,
geo,
isAndroidApp,
}: AuthContextProviderProps): ReactElement => {
const [loginState, setLoginState] = useState<LoginState | null>(null);
const endUser = user && 'providers' in user ? user : null;
Expand Down Expand Up @@ -180,6 +183,7 @@ export const AuthContextProvider = ({
accessToken,
squads,
geo,
isAndroidApp,
}}
>
{children}
Expand Down
12 changes: 10 additions & 2 deletions packages/shared/src/contexts/BootProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ const updateLocalBootData = (
'exp',
'feeds',
'geo',
'isAndroidApp',
]);

storage.setItem(BOOT_LOCAL_KEY, JSON.stringify(result));
Expand Down Expand Up @@ -181,7 +182,7 @@ export const BootDataProvider = ({

const isBootReady = isFetched && !isError;
const loadedFromCache = !!cachedBootData;
const { user, settings, alerts, notifications, squads, geo } =
const { user, settings, alerts, notifications, squads, geo, isAndroidApp } =
cachedBootData || {};

useRefreshToken(remoteData?.accessToken, refetch);
Expand All @@ -190,7 +191,13 @@ export const BootDataProvider = ({
(updatedBootData: Partial<BootCacheData>, update = true) => {
const cachedData = getCachedOrNull() || {};
const lastAppliedChange = lastAppliedChangeRef.current;
let updatedData = { ...updatedBootData };
const params = new URLSearchParams(globalThis?.location?.search);
let updatedData = {
...updatedBootData,
isAndroidApp:
cachedData?.isAndroidApp || Boolean(params.get('android')),
};

if (update) {
if (lastAppliedChange) {
updatedData = { ...lastAppliedChange, ...updatedData };
Expand Down Expand Up @@ -280,6 +287,7 @@ export const BootDataProvider = ({
squads={squads}
firstLoad={initialLoad}
geo={geo}
isAndroidApp={isAndroidApp}
>
<SettingsContextProvider
settings={settings}
Expand Down
3 changes: 2 additions & 1 deletion packages/shared/src/lib/boot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export type Boot = {
ip?: string;
region?: string;
};
isAndroidApp?: boolean;
};

export type BootCacheData = Pick<
Expand All @@ -82,7 +83,7 @@ export type BootCacheData = Pick<
| 'exp'
| 'feeds'
| 'geo'
> & { lastModifier?: string };
> & { lastModifier?: string; isAndroidApp?: boolean };

export async function getBootData(app: string, url?: string): Promise<Boot> {
const appRoute = app === 'companion' ? '/companion' : '';
Expand Down
9 changes: 0 additions & 9 deletions packages/shared/src/lib/func.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ export const postWindowMessage = (
export const checkIsExtension = (): boolean => !!process.env.TARGET_BROWSER;
export const isExtension = !!process.env.TARGET_BROWSER;

export const isAndroidApp = (): boolean => globalThis?.isAndroidApp;

export const isPWA = (): boolean =>
// @ts-expect-error - Safari only, not web standard.
globalThis?.navigator?.standalone ||
Expand Down Expand Up @@ -158,13 +156,6 @@ export const initReactModal = ({
globalThis.reactModalInit = true;
};

export const initApp = (): void => {
const params = new URLSearchParams(globalThis?.location?.search);
if (params.get('android') === 'true') {
globalThis.isAndroidApp = true;
}
};

export const isMobile = (): boolean =>
globalThis?.localStorage.mobile || globalThis?.navigator.maxTouchPoints > 1;

Expand Down
5 changes: 0 additions & 5 deletions packages/webapp/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import { useThemedAsset } from '@dailydotdev/shared/src/hooks/utils';
import { DndContextProvider } from '@dailydotdev/shared/src/contexts/DndContext';
import { structuredCloneJsonPolyfill } from '@dailydotdev/shared/src/lib/structuredClone';
import { fromCDN } from '@dailydotdev/shared/src/lib';
import { initApp } from '@dailydotdev/shared/src/lib/func';
import Seo, { defaultSeo, defaultSeoTitle } from '../next-seo';
import useWebappVersion from '../hooks/useWebappVersion';

Expand Down Expand Up @@ -74,10 +73,6 @@ function InternalApp({ Component, pageProps, router }: AppProps): ReactElement {
const { modal, closeModal } = useLazyModal();
useConsoleLogo();

useEffect(() => {
initApp();
}, []);

useEffect(() => {
updateCookieBanner(user);

Expand Down
6 changes: 4 additions & 2 deletions packages/webapp/pages/onboarding.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export function OnboardPage(): ReactElement {
const router = useRouter();
const { setSettings } = useSettingsContext();
const isLogged = useRef(false);
const { user, isAuthReady, anonymous } = useAuthContext();
const { user, isAuthReady, anonymous, isAndroidApp } = useAuthContext();
const { logSubscriptionEvent, showPlusSubscription: isOnboardingPlusActive } =
usePlusSubscription();
const shouldVerify = anonymous?.shouldVerify;
Expand Down Expand Up @@ -174,7 +174,9 @@ export function OnboardPage(): ReactElement {
const { value: appExperiment } = useConditionalFeature({
feature: featureOnboardingAndroid,
shouldEvaluate:
shouldEnrollOnboardingStep && checkIsBrowser(UserAgent.Android),
shouldEnrollOnboardingStep &&
checkIsBrowser(UserAgent.Android) &&
!isAndroidApp,
});
const { shouldShowExtensionOnboarding } = useOnboardingExtension();
const { value: extensionExperiment } = useConditionalFeature({
Expand Down
Loading