Skip to content

Commit 3593310

Browse files
committed
Review feedback
1 parent 7695f73 commit 3593310

4 files changed

Lines changed: 26 additions & 31 deletions

File tree

src/components/GPSTripStateChecker/index.native.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ function GPSTripStateChecker() {
2727

2828
const reportID = gpsDraftDetails?.reportID ?? generateReportID();
2929

30-
useUpdateGpsTripOnReconnect();
30+
useUpdateGpsTripOnReconnect({gpsPoints: getGpsPoints(gpsDraftDetails)});
3131
useUpdateGpsNotification();
3232

3333
useEffect(() => {

src/components/GPSTripStateChecker/useUpdateGpsTripOnReconnect.ts

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,12 @@
11
import OnyxUtils from 'react-native-onyx/dist/OnyxUtils';
22
import useNetwork from '@hooks/useNetwork';
3-
import useOnyx from '@hooks/useOnyx';
43
import {updateGpsPoints} from '@libs/actions/GPSDraftDetails';
54
import {addressFromGpsPoint, getGpsPoints} from '@libs/GPSDraftDetailsUtils';
65
import ONYXKEYS from '@src/ONYXKEYS';
76
import type {GPSPoint} from '@src/types/onyx/GpsDraftDetails';
87

9-
function useUpdateGpsTripOnReconnect() {
10-
const [gpsDraftDetails] = useOnyx(ONYXKEYS.GPS_DRAFT_DETAILS);
11-
8+
function useUpdateGpsTripOnReconnect({gpsPoints}: {gpsPoints: GPSPoint[][]}) {
129
const updateAddressesToHumanReadable = async () => {
13-
if (!gpsDraftDetails) {
14-
return;
15-
}
16-
17-
const gpsPoints = getGpsPoints(gpsDraftDetails);
18-
1910
const waypointUpdates: Array<Promise<{point: GPSPoint; segmentIndex: number; type: 'start' | 'end'}>> = [];
2011

2112
for (const [segmentIndex, tripSegment] of gpsPoints.entries()) {
@@ -49,9 +40,9 @@ function useUpdateGpsTripOnReconnect() {
4940

5041
// To avoid race conditions, we need to get the latest gpsDraftDetails, because reverse geocoding may even take a few seconds
5142
const gpsDraftDetailsPromiseResult = await OnyxUtils.get(ONYXKEYS.GPS_DRAFT_DETAILS).catch(() => undefined);
52-
const latestGpsDraftDetails = gpsDraftDetailsPromiseResult ?? gpsDraftDetails;
43+
const latestGpsDraftDetails = gpsDraftDetailsPromiseResult;
5344

54-
const latestGpsPoints = getGpsPoints(latestGpsDraftDetails);
45+
const latestGpsPoints = getGpsPoints(latestGpsDraftDetails) ?? gpsPoints;
5546
const newGpsPoints = [...latestGpsPoints];
5647

5748
for (const {point, segmentIndex, type} of waypointAddresses) {

src/components/MapView/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import type {LngLat} from 'react-map-gl';
2-
import getArrayDepth from '@libs/getArrayDepth';
2+
import is2dArray from '@libs/is2dArray';
33
import type {Coordinate} from './MapViewTypes';
44

55
function isSingleSegmentRoute(directionCoordinates: Coordinate[] | Coordinate[][]): directionCoordinates is Coordinate[] {
6-
return getArrayDepth(directionCoordinates) === 2;
6+
return is2dArray(directionCoordinates);
77
}
88

99
function getBounds(waypoints: Coordinate[], directionCoordinates: undefined | Coordinate[]): {southWest: Coordinate; northEast: Coordinate} {

src/pages/iou/request/step/IOURequestStepDistanceGPS/useGPSWaypointMarkers.tsx

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type {WayPoint} from '@components/MapView/MapViewTypes';
55
import {useMemoizedLazyExpensifyIcons} from '@hooks/useLazyAsset';
66
import useOnyx from '@hooks/useOnyx';
77
import useTheme from '@hooks/useTheme';
8-
import {getGPSWaypoints, getTotalGpsTripSegments, isTripStopped as isTripStoppedUtil} from '@libs/GPSDraftDetailsUtils';
8+
import {getGPSWaypoints, isTripStopped as isTripStoppedUtil} from '@libs/GPSDraftDetailsUtils';
99
import CONST from '@src/CONST';
1010
import ONYXKEYS from '@src/ONYXKEYS';
1111
import type IconAsset from '@src/types/utils/IconAsset';
@@ -28,28 +28,32 @@ function useGPSWaypointMarkers(): WayPoint[] {
2828
);
2929

3030
const gpsWaypoints = getGPSWaypoints(gpsDraftDetails);
31+
const waypointEntries = Object.entries(gpsWaypoints);
32+
const lastIndex = waypointEntries.length - 1;
33+
34+
return waypointEntries.flatMap(([key, waypoint], index): WayPoint[] => {
35+
const isStart = index === 0;
36+
const isEnd = index === lastIndex;
37+
38+
if (isEnd && !isTripStopped) {
39+
return [];
40+
}
3141

32-
const gpsWaypointMarkers = Object.entries(gpsWaypoints).map(([key, waypoint], index): WayPoint | null => {
33-
const tripSegmentsCount = getTotalGpsTripSegments(gpsDraftDetails);
3442
let icon = DotIndicator;
35-
if (index === 0) {
43+
if (isStart) {
3644
icon = DotIndicatorUnfilled;
37-
} else if (index === tripSegmentsCount * 2 - 1) {
45+
} else if (isEnd) {
3846
icon = Location;
39-
40-
if (!isTripStopped) {
41-
return null;
42-
}
4347
}
4448

45-
return {
46-
id: key,
47-
coordinate: [waypoint.lng, waypoint.lat],
48-
markerComponent: (): ReactNode => getMarkerComponent(icon),
49-
};
49+
return [
50+
{
51+
id: key,
52+
coordinate: [waypoint.lng, waypoint.lat],
53+
markerComponent: (): ReactNode => getMarkerComponent(icon),
54+
},
55+
];
5056
});
51-
52-
return gpsWaypointMarkers.filter((waypoint): waypoint is WayPoint => !!waypoint);
5357
}
5458

5559
export default useGPSWaypointMarkers;

0 commit comments

Comments
 (0)