-
Notifications
You must be signed in to change notification settings - Fork 327
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4beb5bd
commit e422f97
Showing
17 changed files
with
456 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
apps/ledger-live-mobile/src/hooks/useAutoRedirectToPostOnboarding/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { useEffect, useRef } from "react"; | ||
import { useSelector } from "react-redux"; | ||
import { lastConnectedDeviceSelector } from "~/reducers/settings"; | ||
import { useOpenPostOnboardingCallback } from "./useOpenPostOnboardingCallback"; | ||
import { useShouldRedirect } from "./useShouldRedirect"; | ||
import { useOpenProtectUpsellCallback } from "./useOpenProtectUpsellCallback"; | ||
import { useIsFocused } from "@react-navigation/core"; | ||
|
||
/** | ||
* Redirects the user to the post onboarding or the protect (Ledger Recover) upsell if needed | ||
* */ | ||
export function useAutoRedirectToPostOnboarding() { | ||
const focused = useIsFocused(); | ||
const lastConnectedDevice = useSelector(lastConnectedDeviceSelector); | ||
|
||
const { shouldRedirectToProtectUpsell, shouldRedirectToPostOnboarding } = useShouldRedirect(); | ||
|
||
const openProtectUpsell = useOpenProtectUpsellCallback(); | ||
const openPostOnboarding = useOpenPostOnboardingCallback(); | ||
|
||
const isFocused = useIsFocused(); | ||
|
||
useEffect(() => { | ||
if (!isFocused) return; | ||
if (shouldRedirectToProtectUpsell) { | ||
openProtectUpsell(); | ||
} else if (shouldRedirectToPostOnboarding && lastConnectedDevice) { | ||
openPostOnboarding(lastConnectedDevice.modelId); | ||
} | ||
}, [ | ||
lastConnectedDevice, | ||
openPostOnboarding, | ||
openProtectUpsell, | ||
shouldRedirectToPostOnboarding, | ||
shouldRedirectToProtectUpsell, | ||
focused, | ||
isFocused, | ||
]); | ||
} |
20 changes: 20 additions & 0 deletions
20
...er-live-mobile/src/hooks/useAutoRedirectToPostOnboarding/useOpenPostOnboardingCallback.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { useStartPostOnboardingCallback } from "@ledgerhq/live-common/postOnboarding/hooks/useStartPostOnboardingCallback"; | ||
import { DeviceModelId } from "@ledgerhq/types-devices"; | ||
import { useCallback } from "react"; | ||
|
||
/** | ||
* Returns a callback to open the post onboarding screen | ||
* */ | ||
export function useOpenPostOnboardingCallback() { | ||
const startPostOnboarding = useStartPostOnboardingCallback(); | ||
return useCallback( | ||
(deviceModelId: DeviceModelId) => { | ||
startPostOnboarding({ | ||
deviceModelId: deviceModelId, | ||
resetNavigationStack: false, | ||
fallbackIfNoAction: () => {}, | ||
}); | ||
}, | ||
[startPostOnboarding], | ||
); | ||
} |
73 changes: 73 additions & 0 deletions
73
...ger-live-mobile/src/hooks/useAutoRedirectToPostOnboarding/useOpenProtectUpsellCallback.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { useFeature } from "@ledgerhq/live-common/featureFlags/index"; | ||
import { | ||
Source, | ||
useAlreadyOnboardedURI, | ||
useHomeURI, | ||
usePostOnboardingURI, | ||
useTouchScreenOnboardingUpsellURI, | ||
} from "@ledgerhq/live-common/hooks/recoverFeatureFlag"; | ||
import { DeviceModelId } from "@ledgerhq/types-devices"; | ||
import { useIsFocused } from "@react-navigation/core"; | ||
import { useCallback, useEffect, useState } from "react"; | ||
import { Linking } from "react-native"; | ||
import { useDispatch, useSelector } from "react-redux"; | ||
import { setHasBeenUpsoldProtect } from "~/actions/settings"; | ||
import { internetReachable } from "~/logic/internetReachable"; | ||
import { lastConnectedDeviceSelector, onboardingTypeSelector } from "~/reducers/settings"; | ||
import { OnboardingType } from "~/reducers/types"; | ||
|
||
/** | ||
* Returns a callback to open the Protect (Ledger Recover) upsell | ||
* */ | ||
export function useOpenProtectUpsellCallback() { | ||
const lastConnectedDevice = useSelector(lastConnectedDeviceSelector); | ||
const onboardingType = useSelector(onboardingTypeSelector); | ||
const protectFeature = useFeature("protectServicesMobile"); | ||
const recoverAlreadyOnboardedURI = useAlreadyOnboardedURI(protectFeature); | ||
const recoverPostOnboardingURI = usePostOnboardingURI(protectFeature); | ||
const touchScreenURI = useTouchScreenOnboardingUpsellURI( | ||
protectFeature, | ||
Source.LLM_ONBOARDING_24, | ||
); | ||
const recoverHomeURI = useHomeURI(protectFeature); | ||
const dispatch = useDispatch(); | ||
const [redirectionStarted, setRedirectionStarted] = useState(false); | ||
const isFocused = useIsFocused(); | ||
|
||
useEffect(() => { | ||
if (redirectionStarted && !isFocused) { | ||
dispatch(setHasBeenUpsoldProtect(true)); | ||
} | ||
}, [redirectionStarted, isFocused, dispatch]); | ||
|
||
return useCallback(async () => { | ||
const internetConnected = await internetReachable(); | ||
if (internetConnected && protectFeature?.enabled) { | ||
if ( | ||
lastConnectedDevice && | ||
touchScreenURI && | ||
[DeviceModelId.stax, DeviceModelId.europa].includes(lastConnectedDevice.modelId) | ||
) { | ||
Linking.openURL(touchScreenURI); | ||
setRedirectionStarted(true); | ||
} else if (recoverPostOnboardingURI && onboardingType === OnboardingType.restore) { | ||
Linking.openURL(recoverPostOnboardingURI); | ||
setRedirectionStarted(true); | ||
} else if (recoverHomeURI && onboardingType === OnboardingType.setupNew) { | ||
Linking.openURL(recoverHomeURI); | ||
setRedirectionStarted(true); | ||
} else if (recoverAlreadyOnboardedURI) { | ||
Linking.openURL(recoverAlreadyOnboardedURI); | ||
setRedirectionStarted(true); | ||
} | ||
} | ||
}, [ | ||
lastConnectedDevice, | ||
onboardingType, | ||
protectFeature?.enabled, | ||
recoverAlreadyOnboardedURI, | ||
recoverHomeURI, | ||
recoverPostOnboardingURI, | ||
touchScreenURI, | ||
]); | ||
} |
215 changes: 215 additions & 0 deletions
215
apps/ledger-live-mobile/src/hooks/useAutoRedirectToPostOnboarding/useShouldRedirect.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,215 @@ | ||
import { Device } from "@ledgerhq/live-common/hw/actions/types"; | ||
import { useShouldRedirect } from "./useShouldRedirect"; | ||
import { DeviceModelId } from "@ledgerhq/types-devices"; | ||
|
||
jest.mock("react-redux", () => ({ | ||
useSelector: (fn: () => void) => fn(), | ||
})); | ||
|
||
jest.mock("@ledgerhq/live-common/featureFlags/index", () => ({ | ||
useFeature: jest.fn(), | ||
})); | ||
|
||
jest.mock("~/reducers/settings", () => ({ | ||
hasBeenUpsoldProtectSelector: jest.fn(), | ||
hasBeenRedirectedToPostOnboardingSelector: jest.fn(), | ||
lastConnectedDeviceSelector: jest.fn(), | ||
})); | ||
|
||
const { useFeature } = jest.requireMock("@ledgerhq/live-common/featureFlags/index"); | ||
const { | ||
hasBeenUpsoldProtectSelector, | ||
hasBeenRedirectedToPostOnboardingSelector, | ||
lastConnectedDeviceSelector, | ||
} = jest.requireMock("~/reducers/settings"); | ||
|
||
function mockUseFeature(value: { enabled: boolean }) { | ||
useFeature.mockReturnValue(value); | ||
} | ||
function mockHasBeenUpsoldProtect(value: boolean) { | ||
hasBeenUpsoldProtectSelector.mockReturnValue(value); | ||
} | ||
|
||
function mockHasRedirectedToPostOnboarding(value: boolean) { | ||
hasBeenRedirectedToPostOnboardingSelector.mockReturnValue(value); | ||
} | ||
|
||
function mockLastConnectedDevice(value: Device) { | ||
lastConnectedDeviceSelector.mockReturnValue(value); | ||
} | ||
|
||
type Scenario = { | ||
device: { modelId: DeviceModelId }; | ||
featureFlagEnabled: boolean; | ||
expected: { shouldRedirectToProtectUpsell: boolean; shouldRedirectToPostOnboarding: boolean }; | ||
}; | ||
|
||
function testScenarios(scenarios: Scenario[]) { | ||
scenarios.forEach(scenario => { | ||
it(`should return ${JSON.stringify(scenario.expected)} for ${JSON.stringify(scenario.device)} and feature flag enabled: ${scenario.featureFlagEnabled}`, () => { | ||
mockLastConnectedDevice(scenario.device as Device); | ||
mockUseFeature({ enabled: scenario.featureFlagEnabled }); | ||
|
||
const result = useShouldRedirect(); | ||
|
||
expect( | ||
[result.shouldRedirectToPostOnboarding, result.shouldRedirectToProtectUpsell].filter( | ||
Boolean, | ||
).length, | ||
).toBeLessThanOrEqual(1); | ||
|
||
expect(result).toEqual(scenario.expected); | ||
}); | ||
}); | ||
} | ||
|
||
describe("useShouldRedirect", () => { | ||
afterEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
describe("user HAS NOT BEEN UPSOLD protect & HAS NOT BEEN REDIRECTED to post onboarding", () => { | ||
beforeEach(() => { | ||
mockHasBeenUpsoldProtect(false); | ||
mockHasRedirectedToPostOnboarding(false); | ||
}); | ||
|
||
testScenarios([ | ||
{ | ||
device: { modelId: DeviceModelId.nanoSP }, | ||
featureFlagEnabled: false, | ||
expected: { shouldRedirectToProtectUpsell: false, shouldRedirectToPostOnboarding: true }, | ||
}, | ||
{ | ||
device: { modelId: DeviceModelId.nanoSP }, | ||
featureFlagEnabled: true, | ||
expected: { shouldRedirectToProtectUpsell: false, shouldRedirectToPostOnboarding: true }, | ||
}, | ||
{ | ||
device: { modelId: DeviceModelId.nanoX }, | ||
featureFlagEnabled: false, | ||
expected: { shouldRedirectToProtectUpsell: true, shouldRedirectToPostOnboarding: false }, | ||
}, | ||
{ | ||
device: { modelId: DeviceModelId.stax }, | ||
featureFlagEnabled: false, | ||
expected: { shouldRedirectToProtectUpsell: false, shouldRedirectToPostOnboarding: true }, | ||
}, | ||
{ | ||
device: { modelId: DeviceModelId.stax }, | ||
featureFlagEnabled: true, | ||
expected: { shouldRedirectToProtectUpsell: true, shouldRedirectToPostOnboarding: false }, | ||
}, | ||
{ | ||
device: { modelId: DeviceModelId.europa }, | ||
featureFlagEnabled: false, | ||
expected: { shouldRedirectToProtectUpsell: false, shouldRedirectToPostOnboarding: true }, | ||
}, | ||
{ | ||
device: { modelId: DeviceModelId.europa }, | ||
featureFlagEnabled: true, | ||
expected: { shouldRedirectToProtectUpsell: true, shouldRedirectToPostOnboarding: false }, | ||
}, | ||
]); | ||
}); | ||
|
||
describe("user HAS BEEN UPSOLD protect & HAS NOT BEEN REDIRECTED to post onboarding", () => { | ||
beforeEach(() => { | ||
mockHasBeenUpsoldProtect(true); | ||
mockHasRedirectedToPostOnboarding(false); | ||
}); | ||
|
||
[ | ||
DeviceModelId.nanoS, | ||
DeviceModelId.nanoSP, | ||
DeviceModelId.nanoX, | ||
DeviceModelId.stax, | ||
DeviceModelId.europa, | ||
].forEach(modelId => { | ||
[true, false].forEach(featureFlagEnabled => | ||
testScenarios([ | ||
{ | ||
device: { modelId }, | ||
featureFlagEnabled, | ||
expected: { | ||
shouldRedirectToProtectUpsell: false, | ||
shouldRedirectToPostOnboarding: true, | ||
}, | ||
}, | ||
]), | ||
); | ||
}); | ||
}); | ||
|
||
describe("user HAS BEEN UPSOLD PROTECT & HAS BEEN REDIRECTED to post onboarding", () => { | ||
beforeEach(() => { | ||
mockHasBeenUpsoldProtect(true); | ||
mockHasRedirectedToPostOnboarding(true); | ||
}); | ||
[ | ||
DeviceModelId.nanoS, | ||
DeviceModelId.nanoSP, | ||
DeviceModelId.nanoX, | ||
DeviceModelId.stax, | ||
DeviceModelId.europa, | ||
].forEach(modelId => { | ||
[true, false].forEach(featureFlagEnabled => | ||
testScenarios([ | ||
{ | ||
device: { modelId }, | ||
featureFlagEnabled, | ||
expected: { | ||
shouldRedirectToProtectUpsell: false, | ||
shouldRedirectToPostOnboarding: false, | ||
}, | ||
}, | ||
]), | ||
); | ||
}); | ||
}); | ||
|
||
describe("user HAS NOT BEEN UPSOLD protect & HAS BEEN REDIRECTED to post onboarding", () => { | ||
beforeEach(() => { | ||
mockHasBeenUpsoldProtect(false); | ||
mockHasRedirectedToPostOnboarding(true); | ||
}); | ||
|
||
testScenarios([ | ||
{ | ||
device: { modelId: DeviceModelId.nanoSP }, | ||
featureFlagEnabled: false, | ||
expected: { shouldRedirectToProtectUpsell: false, shouldRedirectToPostOnboarding: false }, | ||
}, | ||
{ | ||
device: { modelId: DeviceModelId.nanoSP }, | ||
featureFlagEnabled: true, | ||
expected: { shouldRedirectToProtectUpsell: false, shouldRedirectToPostOnboarding: false }, | ||
}, | ||
{ | ||
device: { modelId: DeviceModelId.nanoX }, | ||
featureFlagEnabled: false, | ||
expected: { shouldRedirectToProtectUpsell: true, shouldRedirectToPostOnboarding: false }, | ||
}, | ||
{ | ||
device: { modelId: DeviceModelId.stax }, | ||
featureFlagEnabled: false, | ||
expected: { shouldRedirectToProtectUpsell: false, shouldRedirectToPostOnboarding: false }, | ||
}, | ||
{ | ||
device: { modelId: DeviceModelId.stax }, | ||
featureFlagEnabled: true, | ||
expected: { shouldRedirectToProtectUpsell: true, shouldRedirectToPostOnboarding: false }, | ||
}, | ||
{ | ||
device: { modelId: DeviceModelId.europa }, | ||
featureFlagEnabled: false, | ||
expected: { shouldRedirectToProtectUpsell: false, shouldRedirectToPostOnboarding: false }, | ||
}, | ||
{ | ||
device: { modelId: DeviceModelId.europa }, | ||
featureFlagEnabled: true, | ||
expected: { shouldRedirectToProtectUpsell: true, shouldRedirectToPostOnboarding: false }, | ||
}, | ||
]); | ||
}); | ||
}); |
Oops, something went wrong.