Skip to content

Commit

Permalink
some cleanup and fix set self hosted login navigator issue
Browse files Browse the repository at this point in the history
  • Loading branch information
latter-bolden committed Nov 20, 2024
1 parent 7dee8aa commit 9f8e29b
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 48 deletions.
9 changes: 7 additions & 2 deletions apps/tlon-mobile/src/App.main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,20 @@ const App = () => {
};
}, []);

const showAuthenticatedApp = useMemo(() => {
return (
isAuthenticated && !(currentlyOnboarding || finishingSelfHostedLogin)
);
}, [isAuthenticated, currentlyOnboarding, finishingSelfHostedLogin]);

return (
<View height={'100%'} width={'100%'} backgroundColor="$background">
{connected ? (
isLoading ? (
<View flex={1} alignItems="center" justifyContent="center">
<LoadingSpinner />
</View>
) : isAuthenticated &&
!(currentlyOnboarding || finishingSelfHostedLogin) ? (
) : showAuthenticatedApp ? (
<AuthenticatedApp />
) : (
<OnboardingStack />
Expand Down
6 changes: 3 additions & 3 deletions apps/tlon-mobile/src/components/AuthenticatedApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { useShip } from '@tloncorp/app/contexts/ship';
import { useAppStatusChange } from '@tloncorp/app/hooks/useAppStatusChange';
import { useConfigureUrbitClient } from '@tloncorp/app/hooks/useConfigureUrbitClient';
import { useCurrentUserId } from '@tloncorp/app/hooks/useCurrentUser';
import { useNavigationLogging } from '@tloncorp/app/hooks/useNavigationLogger';
import { useNetworkLogger } from '@tloncorp/app/hooks/useNetworkLogger';
import {
useInitializeUserTelemetry,
useTrackAppActive,
} from '@tloncorp/app/hooks/useInitializeTelemetry';
import { useNavigationLogging } from '@tloncorp/app/hooks/useNavigationLogger';
import { useNetworkLogger } from '@tloncorp/app/hooks/useNetworkLogger';
} from '@tloncorp/app/hooks/useTelemetryHelpers';
import { RootStack } from '@tloncorp/app/navigation/RootStack';
import { AppDataProvider } from '@tloncorp/app/provider/AppDataProvider';
import { sync } from '@tloncorp/shared';
Expand Down
10 changes: 8 additions & 2 deletions apps/tlon-mobile/src/screens/Onboarding/ShipLoginScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export const ShipLoginScreen = ({ navigation }: Props) => {
const [codevisible, setCodeVisible] = useState(false);

const isValidUrl = useCallback((url: string) => {
return true;
const urlPattern =
/^(https?:\/\/)?(localhost|(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})|[\w.-]+\.([a-z]{2,}))(:\d+)?$/i;
const hostedPattern = /tlon\.network/i;
Expand Down Expand Up @@ -96,6 +97,13 @@ export const ShipLoginScreen = ({ navigation }: Props) => {
if (authCookie) {
await setFinishingSelfHostedLogin(true);
const shipId = getShipFromCookie(authCookie);

navigation.navigate('SetTelemetry');

// Delay to allow the transition to telemetry screen via Onboarding navigator to complete
// before setting auth and potentially triggering a re-render of app.main (which might change nav prop to Root navigator)
await new Promise((resolve) => setTimeout(resolve, 100));

setShip({
ship: shipId,
shipUrl,
Expand All @@ -107,8 +115,6 @@ export const ShipLoginScreen = ({ navigation }: Props) => {
if (!hasSignedUp) {
logger.trackEvent(AnalyticsEvent.LoggedInBeforeSignup);
}

navigation.navigate('SetTelemetry');
} else {
setRemoteError(
"Sorry, we couldn't log in to your ship. It may be busy or offline."
Expand Down
38 changes: 18 additions & 20 deletions packages/app/features/settings/AppInfoScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { getEmailClients, openComposer } from 'react-native-email-link';
import { ScrollView } from 'react-native-gesture-handler';

import { NOTIFY_PROVIDER, NOTIFY_SERVICE } from '../../constants';
import { useTelemetryDisabler } from '../../hooks/useInitializeTelemetry';
import { useTelemetryDisabler } from '../../hooks/useTelemetryHelpers';
import { setDebug } from '../../lib/debug';
import { getEasUpdateDisplay } from '../../lib/platformHelpers';
import { RootStackParamList } from '../../navigation/types';
Expand Down Expand Up @@ -185,25 +185,23 @@ export function AppInfoScreen(props: Props) {
</YStack>
)}

{telemetry.canDisable && (
<YStack>
<XStack
justifyContent="space-between"
alignItems="center"
padding="$l"
>
<SizableText flexShrink={1}>Share Usage Statistics</SizableText>
<Switch
style={{ flexShrink: 0 }}
value={!telemetryDisabled}
onValueChange={toggleSetTelemetry}
></Switch>
</XStack>
<SizableText size="$s" marginLeft="$l">
By sharing, you help us improve the app for everyone.
</SizableText>
</YStack>
)}
<YStack>
<XStack
justifyContent="space-between"
alignItems="center"
padding="$l"
>
<SizableText flexShrink={1}>Share Usage Statistics</SizableText>
<Switch
style={{ flexShrink: 0 }}
value={!telemetryDisabled}
onValueChange={toggleSetTelemetry}
></Switch>
</XStack>
<SizableText size="$s" marginLeft="$l">
By sharing, you help us improve the app for everyone.
</SizableText>
</YStack>
</YStack>
</ScrollView>
</View>
Expand Down
2 changes: 1 addition & 1 deletion packages/app/hooks/useBootSequence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export function useBootSequence({
authCookie: auth.authCookie,
authType: 'hosted',
});
telemetry?.identify(preSig(auth.nodeId));
telemetry?.identify(preSig(auth.nodeId), { isHostedUser: true });

await wait(2000);

Expand Down
12 changes: 6 additions & 6 deletions packages/app/hooks/useHandleLogout.native.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { createDevLogger } from '@tloncorp/shared';
import * as api from '@tloncorp/shared/api';
import { didInitializeTelemetry } from '@tloncorp/shared/db';
import { finishingSelfHostedLogin } from '@tloncorp/shared/db';
import * as store from '@tloncorp/shared/store';
import { useCallback } from 'react';

import { useBranch } from '../contexts/branch';
import { clearShipInfo, useShip } from '../contexts/ship';
import { removeHostingToken, removeHostingUserId } from '../utils/hosting';
import { clearSplashDismissed } from '../utils/splash';
import { useTelemetry } from './useTelemetry';
import { useClearTelemetryConfig } from './useTelemetryHelpers';

const logger = createDevLogger('logout', true);

export function useHandleLogout({ resetDb }: { resetDb: () => void }) {
const { clearShip } = useShip();
const { clearLure, clearDeepLink } = useBranch();
const telemetry = useTelemetry();
const resetTelemetry = useClearTelemetryConfig();

const handleLogout = useCallback(async () => {
api.queryClient.clear();
Expand All @@ -27,15 +27,15 @@ export function useHandleLogout({ resetDb }: { resetDb: () => void }) {
clearLure();
clearDeepLink();
clearSplashDismissed();
telemetry?.reset();
didInitializeTelemetry.resetValue();
resetTelemetry();
finishingSelfHostedLogin.resetValue();
if (!resetDb) {
logger.trackError('could not reset db on logout');
return;
}
// delay DB reset to next tick to avoid race conditions
setTimeout(() => resetDb());
}, [clearDeepLink, clearLure, clearShip, resetDb, telemetry]);
}, [clearDeepLink, clearLure, clearShip, resetDb, resetTelemetry]);

return handleLogout;
}
5 changes: 2 additions & 3 deletions packages/app/hooks/useTelemetry.native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export function useTelemetry(): TelemetryClient {
const posthog = useNativePosthog();

const optedOut = useMemo(() => {
console.log(`posthog opted out recalc`, posthog?.optedOut);
return posthog?.optedOut ?? false;
}, [posthog]);

Expand All @@ -20,8 +19,8 @@ export function useTelemetry(): TelemetryClient {
}, [posthog]);

const identify = useCallback(
(userId: string) => {
return posthog?.identify(userId);
(userId: string, properties: Record<string, any>) => {
return posthog?.identify(userId, properties);
},
[posthog]
);
Expand Down
4 changes: 2 additions & 2 deletions packages/app/hooks/useTelemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ export function useTelemetry(): TelemetryClient {
}, [posthog]);

const identify = useCallback(
(userId: string) => {
return posthog?.identify(userId);
(userId: string, properties?: Record<string, any>) => {
return posthog?.identify(userId, properties);
},
[posthog]
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,19 @@ import { useShip } from '../contexts/ship';
import { useCurrentUserId } from './useCurrentUser.native';
import { useTelemetry } from './useTelemetry';

export function useClearTelemetryConfig() {
const telemetry = useTelemetry();

const clearTelemetryConfig = useCallback(async () => {
await telemetry.flush();
telemetry?.reset();
didInitializeTelemetry.resetValue();
lastAnonymousAppOpenAt.resetValue();
}, [telemetry]);

return clearTelemetryConfig;
}

export function useIsHosted() {
const ship = useShip();
// We use different heuristics for determining whether a user is hosted across the app.
Expand Down Expand Up @@ -42,7 +55,7 @@ export function useSetTelemetryDisabled(methodId?: string) {
async (shouldDisable: boolean) => {
if (shouldDisable) {
telemetry?.optIn();
telemetry?.capture('Telemetry opt out', {
telemetry?.capture('Telemetry disabled', {
isHostedUser,
detectionMethod: methodId ?? 'useSetTelemetryDisabled',
});
Expand All @@ -52,8 +65,9 @@ export function useSetTelemetryDisabled(methodId?: string) {
} else {
telemetry?.optIn();
if (isHosted) {
telemetry?.identify(currentUserId);
telemetry?.identify(currentUserId, { ishostedUser: true });
}
telemetry?.capture('Telemetry enabled', { isHostedUser });
}
},
[currentUserId, isHosted, isHostedUser, methodId, telemetry]
Expand All @@ -72,7 +86,7 @@ export function useInitializeUserTelemetry() {
useEffect(() => {
async function initializeTelemetry() {
if (isHosted) {
telemetry?.identify(currentUserId);
telemetry?.identify(currentUserId, { isHostedUser: true });
}

if (telemetry?.optedOut) {
Expand Down
2 changes: 1 addition & 1 deletion packages/app/types/telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export interface TelemetryClient {
optedOut: boolean;
optIn: () => void;
optOut: () => void;
identify: (userId: string) => void;
identify: (userId: string, properties?: Record<string, any>) => void;
capture: (eventName: string, properties?: Record<string, any>) => void;
flush: () => Promise<void>;
reset: () => void;
Expand Down
10 changes: 5 additions & 5 deletions packages/shared/src/db/keyValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,12 +301,12 @@ export const didInitializeTelemetry = createStorageItem<boolean>({
defaultValue: false,
});

export const finishingSelfHostedLogin = createStorageItem<boolean>({
key: 'finishingSelfHostedLogin',
defaultValue: false,
});

export const lastAnonymousAppOpenAt = createStorageItem<number | null>({
key: 'lastAnonymousAppOpenAt',
defaultValue: null,
});

export const finishingSelfHostedLogin = createStorageItem<boolean>({
key: 'finishingSelfHostedLogin',
defaultValue: false,
});

0 comments on commit 9f8e29b

Please sign in to comment.