-
Notifications
You must be signed in to change notification settings - Fork 3.9k
[HOLD] [Odometer] Fix state restoration after App reload when image preview is open #82407
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
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 |
|---|---|---|
|
|
@@ -24,6 +24,7 @@ import useThemeStyles from '@hooks/useThemeStyles'; | |
| import { | ||
| createDistanceRequest, | ||
| getMoneyRequestParticipantsFromReport, | ||
| removeMoneyRequestOdometerImage, | ||
| setCustomUnitRateID, | ||
| setMoneyRequestDistance, | ||
| setMoneyRequestMerchant, | ||
|
|
@@ -251,24 +252,60 @@ function IOURequestStepDistanceOdometer({ | |
|
|
||
| const startImageSource = useMemo(() => getImageSource(odometerStartImage), [getImageSource, odometerStartImage]); | ||
| const endImageSource = useMemo(() => getImageSource(odometerEndImage), [getImageSource, odometerEndImage]); | ||
| const hasStartOdometerImage = !!startImageSource; | ||
|
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. ❌ PERF-6 (docs)Redundant state derived from memo
Suggested fix: Since these are only used once each in the onPress={() => {
handleOdometerImagePress(CONST.IOU.ODOMETER_IMAGE_TYPE.START, !!startImageSource);
}}
// and
onPress={() => {
handleOdometerImagePress(CONST.IOU.ODOMETER_IMAGE_TYPE.END, !!endImageSource);
}}This eliminates the intermediate constants that don't add clarity. Please rate this suggestion with 👍 or 👎 to help us improve! Reactions are used to monitor reviewer efficiency. |
||
| const hasEndOdometerImage = !!endImageSource; | ||
|
|
||
| useEffect(() => { | ||
| return () => { | ||
| if (isEditingConfirmation) { | ||
| return; | ||
| } | ||
| if (!startImageSource?.startsWith('blob:')) { | ||
| return; | ||
| } | ||
| URL.revokeObjectURL(startImageSource); | ||
| }; | ||
| }, [startImageSource]); | ||
| }, [startImageSource, isEditingConfirmation]); | ||
|
|
||
| useEffect(() => { | ||
| return () => { | ||
| if (isEditingConfirmation) { | ||
| return; | ||
| } | ||
| if (!endImageSource?.startsWith('blob:')) { | ||
|
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. ❌ PERF-12 (docs)Missing cleanup for blob validation fetch The Suggested fix: const validateOdometerBlobSource = useCallback(
(imageSource: string | undefined, imageType: OdometerImageType) => {
if (\!imageSource?.startsWith('blob:')) {
return;
}
const controller = new AbortController();
fetch(imageSource, { signal: controller.signal })
.catch((error) => {
if (error.name === 'AbortError') {
return;
}
if (imageType === CONST.IOU.ODOMETER_IMAGE_TYPE.START) {
initialStartImageRef.current = undefined;
} else {
initialEndImageRef.current = undefined;
}
removeMoneyRequestOdometerImage(transactionID, imageType, isTransactionDraft);
});
return controller;
},
[isTransactionDraft, transactionID],
);
useEffect(() => {
const controller = validateOdometerBlobSource(startImageSource, CONST.IOU.ODOMETER_IMAGE_TYPE.START);
return () => controller?.abort();
}, [startImageSource, validateOdometerBlobSource]);
useEffect(() => {
const controller = validateOdometerBlobSource(endImageSource, CONST.IOU.ODOMETER_IMAGE_TYPE.END);
return () => controller?.abort();
}, [endImageSource, validateOdometerBlobSource]);Please rate this suggestion with 👍 or 👎 to help us improve! Reactions are used to monitor reviewer efficiency. |
||
| return; | ||
| } | ||
| URL.revokeObjectURL(endImageSource); | ||
| }; | ||
| }, [endImageSource]); | ||
| }, [endImageSource, isEditingConfirmation]); | ||
|
|
||
| const validateOdometerBlobSource = useCallback( | ||
| (imageSource: string | undefined, imageType: OdometerImageType) => { | ||
| if (!imageSource?.startsWith('blob:')) { | ||
| return; | ||
| } | ||
|
|
||
| // Blob URLs are process-local and can become stale after refresh. | ||
| // If the blob cannot be fetched anymore, clear it from transaction state. | ||
| fetch(imageSource).catch(() => { | ||
| if (imageType === CONST.IOU.ODOMETER_IMAGE_TYPE.START) { | ||
| initialStartImageRef.current = undefined; | ||
| } else { | ||
| initialEndImageRef.current = undefined; | ||
| } | ||
| removeMoneyRequestOdometerImage(transactionID, imageType, isTransactionDraft); | ||
| }); | ||
| }, | ||
| [isTransactionDraft, transactionID], | ||
| ); | ||
|
|
||
| useEffect(() => { | ||
| validateOdometerBlobSource(startImageSource, CONST.IOU.ODOMETER_IMAGE_TYPE.START); | ||
| }, [startImageSource, validateOdometerBlobSource]); | ||
|
|
||
| useEffect(() => { | ||
| validateOdometerBlobSource(endImageSource, CONST.IOU.ODOMETER_IMAGE_TYPE.END); | ||
| }, [endImageSource, validateOdometerBlobSource]); | ||
|
|
||
| const buttonText = (() => { | ||
| if (shouldSkipConfirmation) { | ||
|
|
@@ -331,6 +368,17 @@ function IOURequestStepDistanceOdometer({ | |
| [reportID, transactionID, action, iouType], | ||
| ); | ||
|
|
||
| const handleOdometerImagePress = useCallback( | ||
| (imageType: OdometerImageType, hasImage: boolean) => { | ||
| if (!hasImage) { | ||
| handleCaptureImage(imageType); | ||
| return; | ||
| } | ||
| handleViewOdometerImage(imageType); | ||
| }, | ||
| [handleCaptureImage, handleViewOdometerImage], | ||
| ); | ||
|
|
||
| // Navigate to confirmation page helper - following Manual tab pattern | ||
| const navigateToConfirmationPage = () => { | ||
| if (!transactionID || !reportID) { | ||
|
|
@@ -593,11 +641,7 @@ function IOURequestStepDistanceOdometer({ | |
| accessibilityRole="button" | ||
| sentryLabel={CONST.SENTRY_LABEL.ODOMETER_EXPENSE.CAPTURE_IMAGE_START} | ||
| onPress={() => { | ||
| if (odometerStartImage) { | ||
| handleViewOdometerImage(CONST.IOU.ODOMETER_IMAGE_TYPE.START); | ||
| } else { | ||
| handleCaptureImage(CONST.IOU.ODOMETER_IMAGE_TYPE.START); | ||
| } | ||
| handleOdometerImagePress(CONST.IOU.ODOMETER_IMAGE_TYPE.START, hasStartOdometerImage); | ||
| }} | ||
| style={[ | ||
| StyleUtils.getWidthAndHeightStyle(variables.inputHeight, variables.inputHeight), | ||
|
|
@@ -639,11 +683,7 @@ function IOURequestStepDistanceOdometer({ | |
| accessibilityRole="button" | ||
| sentryLabel={CONST.SENTRY_LABEL.ODOMETER_EXPENSE.CAPTURE_IMAGE_END} | ||
| onPress={() => { | ||
| if (odometerEndImage) { | ||
| handleViewOdometerImage(CONST.IOU.ODOMETER_IMAGE_TYPE.END); | ||
| } else { | ||
| handleCaptureImage(CONST.IOU.ODOMETER_IMAGE_TYPE.END); | ||
| } | ||
| handleOdometerImagePress(CONST.IOU.ODOMETER_IMAGE_TYPE.END, hasEndOdometerImage); | ||
| }} | ||
| style={[ | ||
| StyleUtils.getWidthAndHeightStyle(variables.inputHeight, variables.inputHeight), | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,15 +23,14 @@ import variables from '@styles/variables'; | |
| import {setMoneyRequestOdometerImage} from '@userActions/IOU'; | ||
| import CONST from '@src/CONST'; | ||
| import type {IOUAction, IOUType} from '@src/CONST'; | ||
| import ROUTES from '@src/ROUTES'; | ||
| import type SCREENS from '@src/SCREENS'; | ||
| import type {FileObject} from '@src/types/utils/Attachment'; | ||
|
|
||
| type IOURequestStepOdometerImageProps = WithFullTransactionOrNotFoundProps<typeof SCREENS.MONEY_REQUEST.ODOMETER_IMAGE>; | ||
|
|
||
| function IOURequestStepOdometerImage({ | ||
| route: { | ||
| params: {action, iouType, transactionID, reportID, imageType}, | ||
| params: {action, iouType, transactionID, imageType}, | ||
| }, | ||
| }: IOURequestStepOdometerImageProps) { | ||
| const {translate} = useLocalize(); | ||
|
|
@@ -53,11 +52,9 @@ function IOURequestStepOdometerImage({ | |
| const icon = imageType === CONST.IOU.ODOMETER_IMAGE_TYPE.START ? lazyIcons.OdometerStart : lazyIcons.OdometerEnd; | ||
| const messageHTML = `<centered-text><muted-text-label>${message}</muted-text-label></centered-text>`; | ||
|
|
||
| const odometerRoute = ROUTES.DISTANCE_REQUEST_CREATE_TAB_ODOMETER.getRoute(action, iouType, transactionID, reportID); | ||
|
|
||
| const navigateBack = useCallback(() => { | ||
| Navigation.goBack(odometerRoute); | ||
| }, [odometerRoute]); | ||
| Navigation.goBack(); | ||
|
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.
Using Useful? React with 👍 / 👎. |
||
| }, []); | ||
|
|
||
| const revokeDropBlobUrls = useCallback(() => { | ||
| for (const url of dropBlobUrlsRef.current) { | ||
|
|
||
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.
❌ PERF-2 (docs)
Expensive work performed before validation
The code calls
getStateFromPath(normalizedPath, options)before validating whetherfocusedRouteAsPartialmeets all required conditions. If the route name check or parameter validation fails, the expensive state parsing was unnecessary.Suggested fix:
Move the validation logic before calling
getStateFromPath. Since you need the state to get the focused route, consider restructuring to avoid the secondgetStateFromPathcall:Please rate this suggestion with 👍 or 👎 to help us improve! Reactions are used to monitor reviewer efficiency.