|
| 1 | +import OnyxUtils from 'react-native-onyx/dist/OnyxUtils'; |
1 | 2 | import useNetwork from '@hooks/useNetwork'; |
2 | | -import useOnyx from '@hooks/useOnyx'; |
3 | | -import {setEndAddress, setStartAddress} from '@libs/actions/GPSDraftDetails'; |
4 | | -import {addressFromGpsPoint} from '@libs/GPSDraftDetailsUtils'; |
| 3 | +import {updateGpsPoints} from '@libs/actions/GPSDraftDetails'; |
| 4 | +import {addressFromGpsPoint, getGpsPoints} from '@libs/GPSDraftDetailsUtils'; |
5 | 5 | import ONYXKEYS from '@src/ONYXKEYS'; |
6 | | -import type {GpsDraftDetails} from '@src/types/onyx'; |
| 6 | +import type {GPSPoint} from '@src/types/onyx/GpsDraftDetails'; |
7 | 7 |
|
8 | | -function useUpdateGpsTripOnReconnect() { |
9 | | - const [gpsDraftDetails] = useOnyx(ONYXKEYS.GPS_DRAFT_DETAILS); |
| 8 | +function useUpdateGpsTripOnReconnect({gpsPoints}: {gpsPoints: GPSPoint[][]}) { |
| 9 | + const updateAddressesToHumanReadable = async () => { |
| 10 | + const waypointUpdates: Array<Promise<{point: GPSPoint; segmentIndex: number; type: 'start' | 'end'}>> = []; |
10 | 11 |
|
11 | | - const updateAddressToHumanReadable = async (gpsPoint: GpsDraftDetails['gpsPoints'][number] | undefined, setAddress: typeof setStartAddress) => { |
12 | | - if (!gpsPoint) { |
13 | | - return; |
| 12 | + for (const [segmentIndex, tripSegment] of gpsPoints.entries()) { |
| 13 | + for (const [pointIndex, point] of tripSegment.entries()) { |
| 14 | + // If the address is not a coordinates (already human readable), we don't need to update it |
| 15 | + if (point.address?.type === 'address') { |
| 16 | + continue; |
| 17 | + } |
| 18 | + |
| 19 | + if (pointIndex === 0) { |
| 20 | + waypointUpdates.push( |
| 21 | + addressFromGpsPoint(point).then((address) => ({ |
| 22 | + point: {...point, address: address ? {value: address, type: 'address'} : point.address}, |
| 23 | + segmentIndex, |
| 24 | + type: 'start', |
| 25 | + })), |
| 26 | + ); |
| 27 | + } else if (pointIndex === tripSegment.length - 1) { |
| 28 | + waypointUpdates.push( |
| 29 | + addressFromGpsPoint(point).then((address) => ({ |
| 30 | + point: {...point, address: address ? {value: address, type: 'address'} : point.address}, |
| 31 | + segmentIndex, |
| 32 | + type: 'end', |
| 33 | + })), |
| 34 | + ); |
| 35 | + } |
| 36 | + } |
14 | 37 | } |
15 | 38 |
|
16 | | - const address = await addressFromGpsPoint(gpsPoint); |
| 39 | + const waypointAddresses = (await Promise.all(waypointUpdates)).filter((waypoints) => !!waypoints.point.address); |
17 | 40 |
|
18 | | - if (address !== null) { |
19 | | - setAddress({value: address, type: 'address'}); |
20 | | - } |
21 | | - }; |
| 41 | + // To avoid race conditions, we need to get the latest gpsDraftDetails, because reverse geocoding may even take a few seconds |
| 42 | + const gpsDraftDetailsPromiseResult = await OnyxUtils.get(ONYXKEYS.GPS_DRAFT_DETAILS).catch(() => undefined); |
| 43 | + const latestGpsDraftDetails = gpsDraftDetailsPromiseResult; |
22 | 44 |
|
23 | | - const updateAddressesToHumanReadable = () => { |
24 | | - if (!gpsDraftDetails) { |
25 | | - return; |
26 | | - } |
| 45 | + const latestGpsPoints = getGpsPoints(latestGpsDraftDetails) ?? gpsPoints; |
| 46 | + const newGpsPoints = [...latestGpsPoints]; |
27 | 47 |
|
28 | | - const {gpsPoints, startAddress, endAddress} = gpsDraftDetails; |
| 48 | + for (const {point, segmentIndex, type} of waypointAddresses) { |
| 49 | + const segment = newGpsPoints.at(segmentIndex); |
| 50 | + if (!segment) { |
| 51 | + continue; |
| 52 | + } |
29 | 53 |
|
30 | | - if (startAddress.type === 'coordinates') { |
31 | | - updateAddressToHumanReadable(gpsPoints.at(0), setStartAddress); |
| 54 | + if (type === 'start') { |
| 55 | + const newSegment = [point, ...segment.slice(1)]; |
| 56 | + newGpsPoints.splice(segmentIndex, 1, newSegment); |
| 57 | + } else if (type === 'end') { |
| 58 | + const newSegment = [...segment.slice(0, -1), point]; |
| 59 | + newGpsPoints.splice(segmentIndex, 1, newSegment); |
| 60 | + } |
32 | 61 | } |
33 | 62 |
|
34 | | - if (endAddress.type === 'coordinates') { |
35 | | - updateAddressToHumanReadable(gpsPoints.at(-1), setEndAddress); |
36 | | - } |
| 63 | + updateGpsPoints(newGpsPoints); |
37 | 64 | }; |
38 | 65 |
|
39 | 66 | // This is intentional to use async/await pattern for better readability |
40 | | - |
| 67 | + // eslint-disable-next-line @typescript-eslint/no-misused-promises |
41 | 68 | useNetwork({onReconnect: updateAddressesToHumanReadable}); |
42 | 69 | } |
43 | 70 |
|
|
0 commit comments