Skip to content
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

Map panning fix #25977

Closed
Closed
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config/webpack/webpack.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ const webpackConfig = ({envFile = '.env', platform = 'web'}) => ({
// This is also why we have to use .website.js for our own web-specific files...
// Because desktop also relies on "web-specific" module implementations
// This also skips packing web only dependencies to desktop and vice versa
extensions: ['.web.js', platform === 'web' ? '.website.js' : '.desktop.js', '.js', '.jsx', '.web.ts', platform === 'web' ? '.website.ts' : '.desktop.ts', '.ts', '.tsx'],
extensions: ['.web.js', platform === 'web' ? '.website.js' : '.desktop.js', '.js', '.jsx', '.web.ts', platform === 'web' ? '.website.ts' : '.desktop.ts', '.ts', '.web.tsx', '.tsx'],
fallback: {
'process/browser': require.resolve('process/browser'),
},
Expand Down
19 changes: 0 additions & 19 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@
"react-native-web-linear-gradient": "^1.1.2",
"react-native-web-lottie": "^1.4.4",
"react-native-webview": "^11.17.2",
"react-native-x-maps": "1.0.10",
"react-pdf": "^6.2.2",
"react-plaid-link": "3.3.2",
"react-web-config": "^1.0.0",
Expand Down
8 changes: 7 additions & 1 deletion src/CONST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2594,7 +2594,6 @@ const CONST = {

SF_COORDINATES: [-122.4194, 37.7749],

MAPBOX_STYLE_URL: 'mapbox://styles/expensify/cllcoiqds00cs01r80kp34tmq',
NAVIGATION: {
TYPE: {
FORCED_UP: 'FORCED_UP',
Expand All @@ -2611,6 +2610,13 @@ const CONST = {
SAASTR: 'SaaStrDemoSetup',
SBE: 'SbeDemoSetup',
},

MAPBOX: {
PADDING: 50,
DEFAULT_ZOOM: 10,
DEFAULT_COORDINATE: [-122.4021, 37.7911],
STYLE_URL: 'mapbox://styles/expensify/cllcoiqds00cs01r80kp34tmq',
},
} as const;

export default CONST;
118 changes: 62 additions & 56 deletions src/components/DistanceRequest.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,41 @@
import React, {useEffect, useState} from 'react';
import React, {useEffect, useMemo, useState} from 'react';
import {ScrollView, View} from 'react-native';
import {withOnyx} from 'react-native-onyx';
import lodashGet from 'lodash/get';
import _ from 'underscore';
import PropTypes from 'prop-types';
import {withOnyx} from 'react-native-onyx';
import MapView from 'react-native-x-maps';
import _ from 'underscore';

import CONST from '../CONST';
import ROUTES from '../ROUTES';
import ONYXKEYS from '../ONYXKEYS';
import * as Transaction from '../libs/actions/Transaction';
import * as TransactionUtils from '../libs/TransactionUtils';
import MenuItemWithTopDescription from './MenuItemWithTopDescription';
import * as Expensicons from './Icon/Expensicons';
import theme from '../styles/themes/default';
import Button from './Button';

import styles from '../styles/styles';
import variables from '../styles/variables';
import LinearGradient from './LinearGradient';
import * as MapboxToken from '../libs/actions/MapboxToken';
import CONST from '../CONST';
import BlockingView from './BlockingViews/BlockingView';
import theme from '../styles/themes/default';

import transactionPropTypes from './transactionPropTypes';

import useNetwork from '../hooks/useNetwork';
import usePrevious from '../hooks/usePrevious';
import useLocalize from '../hooks/useLocalize';

import * as ErrorUtils from '../libs/ErrorUtils';
import Navigation from '../libs/Navigation/Navigation';
import ROUTES from '../ROUTES';
import transactionPropTypes from './transactionPropTypes';
import * as MapboxToken from '../libs/actions/MapboxToken';
import * as Transaction from '../libs/actions/Transaction';
import * as TransactionUtils from '../libs/TransactionUtils';

import Button from './Button';
import MapView from './MapView';
import LinearGradient from './LinearGradient';
import * as Expensicons from './Icon/Expensicons';
import BlockingView from './BlockingViews/BlockingView';
import DotIndicatorMessage from './DotIndicatorMessage';
import * as ErrorUtils from '../libs/ErrorUtils';
import usePrevious from '../hooks/usePrevious';
import MenuItemWithTopDescription from './MenuItemWithTopDescription';

const MAX_WAYPOINTS = 25;
const MAX_WAYPOINTS_TO_DISPLAY = 4;

const MAP_PADDING = 50;
const DEFAULT_ZOOM_LEVEL = 10;

const propTypes = {
/** The transactionID of this request */
transactionID: PropTypes.string,
Expand Down Expand Up @@ -62,8 +65,7 @@ function DistanceRequest({transactionID, transaction, mapboxAccessToken}) {
const [scrollContentHeight, setScrollContentHeight] = useState(0);
const {isOffline} = useNetwork();
const {translate} = useLocalize();

const waypoints = lodashGet(transaction, 'comment.waypoints', {});
const waypoints = useMemo(() => lodashGet(transaction, 'comment.waypoints', {}), [transaction]);
const numberOfWaypoints = _.size(waypoints);

const lastWaypointIndex = numberOfWaypoints - 1;
Expand All @@ -73,34 +75,39 @@ function DistanceRequest({transactionID, transaction, mapboxAccessToken}) {
const haveWaypointsChanged = !_.isEqual(previousWaypoints, waypoints);
const shouldFetchRoute = haveWaypointsChanged && !isOffline && !isLoadingRoute && TransactionUtils.validateWaypoints(waypoints);

const waypointMarkers = _.filter(
_.map(waypoints, (waypoint, key) => {
if (!waypoint || waypoint.lng === undefined || waypoint.lat === undefined) {
return;
}

const index = Number(key.replace('waypoint', ''));
let MarkerComponent;
if (index === 0) {
MarkerComponent = Expensicons.DotIndicatorUnfilled;
} else if (index === lastWaypointIndex) {
MarkerComponent = Expensicons.Location;
} else {
MarkerComponent = Expensicons.DotIndicator;
}

return {
coordinate: [waypoint.lng, waypoint.lat],
markerComponent: () => (
<MarkerComponent
width={20}
height={20}
fill={theme.icon}
/>
),
};
}),
(waypoint) => waypoint,
const waypointMarkers = useMemo(
() =>
_.filter(
_.map(waypoints, (waypoint, key) => {
if (!waypoint || waypoint.lng === undefined || waypoint.lat === undefined) {
return;
}

const index = Number(key.replace('waypoint', ''));
let MarkerComponent;
if (index === 0) {
MarkerComponent = Expensicons.DotIndicatorUnfilled;
} else if (index === lastWaypointIndex) {
MarkerComponent = Expensicons.Location;
} else {
MarkerComponent = Expensicons.DotIndicator;
}

return {
id: `${waypoint.lng},${waypoint.lat},${index}`,
coordinate: [waypoint.lng, waypoint.lat],
markerComponent: () => (
<MarkerComponent
width={20}
height={20}
fill={theme.icon}
/>
),
};
}),
(waypoint) => waypoint,
),
[waypoints, lastWaypointIndex],
);

// Show up to the max number of waypoints plus 1/2 of one to hint at scrolling
Expand Down Expand Up @@ -207,17 +214,16 @@ function DistanceRequest({transactionID, transaction, mapboxAccessToken}) {
{!isOffline && Boolean(mapboxAccessToken.token) ? (
<MapView
accessToken={mapboxAccessToken.token}
mapPadding={MAP_PADDING}
mapPadding={CONST.MAPBOX.PADDING}
pitchEnabled={false}
initialState={{
location: CONST.SF_COORDINATES,
zoom: DEFAULT_ZOOM_LEVEL,
zoom: CONST.MAPBOX.DEFAULT_ZOOM,
location: CONST.MAPBOX.DEFAULT_COORDINATE,
}}
directionCoordinates={lodashGet(transaction, 'routes.route0.geometry.coordinates', [])}
directionStyle={styles.mapDirection}
style={styles.mapView}
waypoints={waypointMarkers}
styleURL={CONST.MAPBOX_STYLE_URL}
styleURL={CONST.MAPBOX.STYLE_URL}
/>
) : (
<View style={[styles.mapPendingView]}>
Expand Down
30 changes: 30 additions & 0 deletions src/components/MapView/Direction.tsx
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NAB I prefer to call this Route

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import Mapbox from '@rnmapbox/maps';
import {DirectionProps} from './MapViewTypes';
import styles from '../../styles/styles';

function Direction({coordinates}: DirectionProps) {
if (coordinates.length < 1) {
return null;
}

return (
<Mapbox.ShapeSource
id="routeSource"
shape={{
type: 'Feature',
properties: {},
geometry: {
type: 'LineString',
coordinates,
},
}}
>
<Mapbox.LineLayer
id="routeFill"
style={styles.mapDirection}
/>
</Mapbox.ShapeSource>
);
}

export default Direction;
48 changes: 48 additions & 0 deletions src/components/MapView/Direction.web.tsx
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please create a child folder Direction in Mapview and then create this file as index.tsx. for native file, name would be index.native.tsx

Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Explanation: Different Mapbox libraries are required for web and native mobile platforms.
// This is why we have separate components for web and native to handle the specific implementations.
// For the web version, we use the Mapbox Web library called react-map-gl, while for the native mobile version,
// we utilize a different Mapbox library @rnmapbox/maps tailored for mobile development.

import React from 'react';
hayata-suenaga marked this conversation as resolved.
Show resolved Hide resolved
import {View} from 'react-native';
import {Layer, Source} from 'react-map-gl';
import {DirectionProps} from './MapViewTypes';

import styles from '../../styles/styles';

function Direction({coordinates}: DirectionProps) {
const layerLayoutStyle: Record<string, string> = styles.mapDirectionLayer.layout;
const layerPointStyle: Record<string, string | number> = styles.mapDirectionLayer.paint;

if (coordinates.length < 1) {
return null;
}
return (
<View>
{coordinates && (
<Source
id="route"
type="geojson"
data={{
type: 'Feature',
properties: {},
geometry: {
type: 'LineString',
coordinates,
},
}}
Comment on lines +26 to +33
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to move this data in mapView utils.ts file as a function which takes coordinates as parameter.

>
<Layer
id="route"
Comment on lines +24 to +36
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use different ids for layer and source and keep these the same as native varient.

Technically, we don't need to pass these ids. given that we are not using this our logic and lib will assign default ones.

type="line"
source="route"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
source="route"

Beacuse it is nested in a Source so let's remove it for less confusion.

paint={layerPointStyle}
layout={layerLayoutStyle}
/>
</Source>
)}
</View>
);
}

export default Direction;
Loading
Loading