-
Notifications
You must be signed in to change notification settings - Fork 4k
Remove OnyxUtils.get() from useUpdateGpsTripOnReconnect (use useOnyx) #99391
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 |
|---|---|---|
| @@ -1,14 +1,23 @@ | ||
| import useNetwork from '@hooks/useNetwork'; | ||
| import useOnyx from '@hooks/useOnyx'; | ||
|
|
||
| import {updateGpsPoints, updateTrimmedEndPoint} from '@libs/actions/GPSDraftDetails'; | ||
| import {addressFromGpsPoint, getGpsPoints} from '@libs/GPSDraftDetailsUtils'; | ||
|
|
||
| import ONYXKEYS from '@src/ONYXKEYS'; | ||
| import type {GPSPoint, TrimmedGPSPoint} from '@src/types/onyx/GpsDraftDetails'; | ||
|
|
||
| import OnyxUtils from 'react-native-onyx/dist/OnyxUtils'; | ||
| import {useEffect, useRef} from 'react'; | ||
|
|
||
| function useUpdateGpsTripOnReconnect({gpsPoints}: {gpsPoints: GPSPoint[][]}) { | ||
| const [gpsDraftDetails] = useOnyx(ONYXKEYS.GPS_DRAFT_DETAILS); | ||
| // Mirror the latest gpsDraftDetails into a ref so the async onReconnect handler can read the newest value | ||
| // after awaiting reverse geocoding, instead of the stale value captured when the callback started. | ||
| const latestGpsDraftDetailsRef = useRef(gpsDraftDetails); | ||
| useEffect(() => { | ||
| latestGpsDraftDetailsRef.current = gpsDraftDetails; | ||
| }, [gpsDraftDetails]); | ||
|
Comment on lines
+17
to
+19
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.
When 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. Edge case enough to ignore. Not reproducible |
||
|
|
||
| // The trimmed end point is chosen in the Edit Stop screen. When trimmed while offline, its address is stored as | ||
| // stringified coordinates, so on reconnect we fetch the human readable address to replace it. | ||
| const updateTrimmedEndPointAddress = async (trimmedEndPoint: TrimmedGPSPoint | undefined) => { | ||
|
|
@@ -57,9 +66,8 @@ function useUpdateGpsTripOnReconnect({gpsPoints}: {gpsPoints: GPSPoint[][]}) { | |
|
|
||
| const waypointAddresses = (await Promise.all(waypointUpdates)).filter((waypoints) => !!waypoints.point.address); | ||
|
|
||
| // To avoid race conditions, we need to get the latest gpsDraftDetails, because reverse geocoding may even take a few seconds | ||
| const gpsDraftDetailsPromiseResult = await OnyxUtils.get(ONYXKEYS.GPS_DRAFT_DETAILS).catch(() => undefined); | ||
| const latestGpsDraftDetails = gpsDraftDetailsPromiseResult; | ||
| // To avoid race conditions, we need the latest gpsDraftDetails, because reverse geocoding may even take a few seconds | ||
| const latestGpsDraftDetails = latestGpsDraftDetailsRef.current; | ||
|
|
||
| const latestGpsPoints = getGpsPoints(latestGpsDraftDetails) ?? gpsPoints; | ||
| const newGpsPoints = [...latestGpsPoints]; | ||
|
|
||
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.
If the app starts offline with a persisted GPS trip and reconnects before this subscription reaches
loaded, bothgpsDraftDetailsand the parent-provided fallback are initially empty. The emptyPromise.allthen completes immediately andupdateGpsPoints([[]])overwrites the persisted route; the parent checker already guards its startup logic withgpsDraftDetailsMetadata.status !== 'loaded'for this reason. Read the metadata here and defer the reconnect handler until the draft has hydrated.Useful? React with 👍 / 👎.
Uh oh!
There was an error while loading. Please reload this page.
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.
Edge case enough to ignore. Not reproducible.