Repository navigation
Refactor Onfido native loader to ActivityIndicator #96819
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
Changes from all commits
5954717
1238fa0
45f9f21
949e8a5
1b981f7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| import FullscreenLoadingIndicator from '@components/FullscreenLoadingIndicator'; | ||
| import ActivityIndicator from '@components/ActivityIndicator'; | ||
|
|
||
| import useLocalize from '@hooks/useLocalize'; | ||
| import useThemeStyles from '@hooks/useThemeStyles'; | ||
|
|
||
| import getPlatform from '@libs/getPlatform'; | ||
| import goToSettings from '@libs/goToSettings'; | ||
|
|
@@ -20,8 +21,31 @@ const {AppStateTracker} = NativeModules; | |
|
|
||
| function Onfido({sdkToken, onUserExit, onSuccess, onError}: OnfidoProps) { | ||
| const {translate} = useLocalize(); | ||
| const styles = useThemeStyles(); | ||
|
|
||
| useEffect(() => { | ||
| // Back is exposed while OnfidoSDK.start() is still pending, so a late resolve/reject could navigate or mutate | ||
| // wallet/bank-account state after the user has already left. Ignore the callbacks once this component unmounts. | ||
| let isActive = true; | ||
| const handleSuccess: OnfidoProps['onSuccess'] = (data) => { | ||
| if (!isActive) { | ||
| return; | ||
| } | ||
| onSuccess(data); | ||
| }; | ||
| const handleUserExit: OnfidoProps['onUserExit'] = (isUserInitiated) => { | ||
| if (!isActive) { | ||
| return; | ||
| } | ||
| onUserExit(isUserInitiated); | ||
| }; | ||
| const handleError: OnfidoProps['onError'] = (error) => { | ||
| if (!isActive) { | ||
| return; | ||
| } | ||
| onError(error); | ||
| }; | ||
|
|
||
| OnfidoSDK.start({ | ||
| sdkToken, | ||
| theme: OnfidoTheme.AUTOMATIC, | ||
|
|
@@ -38,7 +62,7 @@ function Onfido({sdkToken, onUserExit, onSuccess, onError}: OnfidoProps) { | |
| }, | ||
| }, | ||
| }) | ||
| .then(onSuccess) | ||
| .then(handleSuccess) | ||
| .catch((error: OnfidoError) => { | ||
| const errorMessage: string = error.message ?? CONST.ERROR.UNKNOWN_ERROR; | ||
| const errorType = error.type; | ||
|
|
@@ -50,18 +74,23 @@ function Onfido({sdkToken, onUserExit, onSuccess, onError}: OnfidoProps) { | |
| if (([CONST.ONFIDO.ERROR.USER_CANCELLED, CONST.ONFIDO.ERROR.USER_TAPPED_BACK, CONST.ONFIDO.ERROR.USER_EXITED] as string[]).includes(errorMessage)) { | ||
| if (getPlatform() === CONST.PLATFORM.ANDROID) { | ||
| AppStateTracker.getWasAppRelaunchedFromIcon().then((wasAppRelaunchedFromIcon) => { | ||
| onUserExit(!wasAppRelaunchedFromIcon); | ||
| handleUserExit(!wasAppRelaunchedFromIcon); | ||
| }); | ||
| return; | ||
| } | ||
|
|
||
| onUserExit(true); | ||
| handleUserExit(true); | ||
| return; | ||
| } | ||
|
|
||
| if (!!errorMessage && getPlatform() === CONST.PLATFORM.IOS) { | ||
| checkMultiple([PERMISSIONS.IOS.MICROPHONE, PERMISSIONS.IOS.CAMERA]) | ||
| .then((statuses) => { | ||
| // The permission check resolves asynchronously, so the user may have already backed out (unmounting this | ||
| // component) by the time it settles. Skip the alert to avoid showing a stale permission prompt on the screen they returned to. | ||
| if (!isActive) { | ||
| return; | ||
| } | ||
| const isMicAllowed = statuses[PERMISSIONS.IOS.MICROPHONE] === RESULTS.GRANTED; | ||
| const isCameraAllowed = statuses[PERMISSIONS.IOS.CAMERA] === RESULTS.GRANTED; | ||
| let alertTitle: TranslationPaths | '' = ''; | ||
|
|
@@ -81,12 +110,12 @@ function Onfido({sdkToken, onUserExit, onSuccess, onError}: OnfidoProps) { | |
| [ | ||
| { | ||
| text: translate('common.cancel'), | ||
| onPress: () => onUserExit(true), | ||
| onPress: () => handleUserExit(true), | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
On iOS with camera or microphone permission denied, backing out while Useful? React with 👍 / 👎.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @MelvinBot is this valid?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, this is valid. The Since Back is now exposed while Fix as suggested: short-circuit when unmounted before presenting the alert, e.g. add
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @MelvinBot address this then
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤖 Addressed in Good catch — the checkMultiple([PERMISSIONS.IOS.MICROPHONE, PERMISSIONS.IOS.CAMERA])
.then((statuses) => {
// The permission check resolves asynchronously, so the user may have already backed out (unmounting this
// component) by the time it settles. Skip the alert to avoid showing a stale permission prompt on the screen they returned to.
if (!isActive) {
return;
}
...
|
||
| }, | ||
| { | ||
| text: translate('common.settings'), | ||
| onPress: () => { | ||
| onUserExit(); | ||
| handleUserExit(); | ||
| goToSettings(); | ||
| }, | ||
| }, | ||
|
|
@@ -95,20 +124,29 @@ function Onfido({sdkToken, onUserExit, onSuccess, onError}: OnfidoProps) { | |
| ); | ||
| return; | ||
| } | ||
| onError(errorMessage); | ||
| handleError(errorMessage); | ||
| }) | ||
| .catch(() => { | ||
| onError(errorMessage); | ||
| handleError(errorMessage); | ||
| }); | ||
| } else { | ||
| onError(errorMessage); | ||
| handleError(errorMessage); | ||
| } | ||
| }); | ||
|
|
||
| return () => { | ||
| isActive = false; | ||
| }; | ||
|
Comment on lines
+137
to
+139
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Fresh evidence after the follow-up fix is that unmount cleanup now only flips Useful? React with 👍 / 👎.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @MelvinBot is this valid case?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Partly valid — the underlying race is real, but the mental model in your rebuttal has one gap worth calling out. Why it's a real race: I checked the SDK surface — return () => {
isActive = false;
};So the bot is technically correct that a The gap in "hidden by onfido step pages": the spinner + Back button are shown before the native Onfido UI presents (during But it's low severity, and I'd lean toward deferring rather than blocking:
Since there's no SDK cancel to call, the only way to fully close it is to keep Back unavailable until the native flow presents — which partly works against this PR's goal of exposing Back. Given the guards already prevent the harmful effects, treating this as a follow-up/won't-fix seems reasonable rather than a blocker. |
||
| // Onfido should be initialized only once on mount | ||
| // eslint-disable-next-line react-hooks/exhaustive-deps | ||
| }, []); | ||
|
|
||
| return <FullscreenLoadingIndicator />; | ||
| return ( | ||
| <ActivityIndicator | ||
| size={CONST.ACTIVITY_INDICATOR_SIZE.LARGE} | ||
| style={styles.flex1} | ||
| /> | ||
|
Comment on lines
+145
to
+148
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Now that this inline Useful? React with 👍 / 👎.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤖 Addressed in Good catch — with Back now tappable during loading, a late
|
||
| ); | ||
| } | ||
|
|
||
| export default Onfido; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@MelvinBot address #96819 (review).
It's edge case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressing
Julesssss' concern about theisActiveapproach dropping success/error results in this critical flow.TL;DR: The guards don't drop a success/error in the normal flow — they only fire when the component unmounts before
OnfidoSDK.start()settles. In the happy path that never happens, so I'd lean toward keeping them (agreeing withsituchanthat the drop scenario is a narrow edge case). Details + an alternative below.Why normal success/error is never dropped: the native Onfido UI presents on top of this still-mounted component (we only render the spinner underneath). So when
start()resolves/rejects, this component is still mounted,isActive === true, and the callback runs — the parent only unmounts us as a result of that callback.isActiveisfalseonly when something tears this component down before the SDK settles. In practice that's the user tapping Back on the init spinner (the case these guards were added for), wherestart()comes back as exit/cancel, not a genuine success. A real success/error is dropped only in the edge casesituchanflagged: an external Onyx/navigation change unmounts the flow while the native modal is still up.Why I'd keep the guards rather than remove them: the callbacks aren't pure "record the result" — several navigate or show global UI, which is exactly what breaks if they run after unmount. So the guards are actively preventing the stale-navigation / stale-toast bugs raised earlier in this review.
Per-caller side effects of each callback (why the guards matter)
onUserExit→Navigation.goBack()(OnfidoStep),onBackButtonPress()+setOnfidoKey(...)React state (requestorVerifyIdentity). Late call → stale double-navigation / setState-after-unmount. Must stay guarded.onError→ globalGrowl.error(...)on all three callers, plusclearOnfidoToken()+onBackButtonPress()on the requestor page. Late call → a stale toast on whatever screen the user returned to, and a stray navigation.onSuccess→ mostly action dispatch (verifyIdentity*+ draft writes), which is safe post-unmount — but the requestor page also callsonSubmit?.(), which advances the step.Net: removing the guards trades a very-rare dropped verification (the user can retry) for visibly-stale navigation/toasts. Since the drop is only reachable via that external-unmount edge case, keeping the current approach seems right.
If you'd rather bias toward never losing a completed verification (
Julesssss' preference), the surgical option is to ungardhandleSuccessonly — the pure recording path. Caveat: on the requestor pageonSuccessalso callsonSubmit?.()(navigation), so it isn't 100% side-effect-free. Just say the word and I'll push that variant.Relevant code: guards, permission-alert guard, unmount cleanup.