2021-12-06 09:45:12 +00:00
|
|
|
/*
|
|
|
|
|
Copyright 2021 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
|
limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
2021-12-17 12:26:02 +00:00
|
|
|
import React, { SyntheticEvent } from 'react';
|
2022-03-09 18:14:07 +01:00
|
|
|
import maplibregl, { MapMouseEvent } from 'maplibre-gl';
|
2021-12-09 09:10:23 +00:00
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
2022-01-12 13:32:08 +00:00
|
|
|
import { RoomMember } from 'matrix-js-sdk/src/models/room-member';
|
2022-02-22 12:18:08 +00:00
|
|
|
import { ClientEvent, IClientWellKnown } from 'matrix-js-sdk/src/client';
|
2021-12-06 09:45:12 +00:00
|
|
|
|
|
|
|
|
import { _t } from '../../../languageHandler';
|
2022-01-12 13:32:08 +00:00
|
|
|
import MatrixClientContext from '../../../contexts/MatrixClientContext';
|
2022-01-13 14:38:04 +00:00
|
|
|
import Modal from '../../../Modal';
|
2022-03-28 12:48:38 +02:00
|
|
|
import SdkConfig from '../../../SdkConfig';
|
2022-01-27 09:51:06 +00:00
|
|
|
import { tileServerFromWellKnown } from '../../../utils/WellKnownUtils';
|
2022-03-28 12:48:38 +02:00
|
|
|
import { GenericPosition, genericPositionFromGeolocation, getGeoUri } from '../../../utils/beacon';
|
|
|
|
|
import { LocationShareError, findMapStyleUrl } from '../../../utils/location';
|
|
|
|
|
import ErrorDialog from '../dialogs/ErrorDialog';
|
2022-03-09 18:14:07 +01:00
|
|
|
import AccessibleButton from '../elements/AccessibleButton';
|
2022-03-11 09:52:57 +01:00
|
|
|
import { MapError } from './MapError';
|
2022-03-21 12:42:58 +01:00
|
|
|
import LiveDurationDropdown, { DEFAULT_DURATION_MS } from './LiveDurationDropdown';
|
2022-03-28 12:48:38 +02:00
|
|
|
import { LocationShareType, ShareLocationFn } from './shareLocation';
|
2022-04-06 13:40:39 +02:00
|
|
|
import Marker from './Marker';
|
2022-03-25 15:36:22 +01:00
|
|
|
|
2022-03-01 18:00:07 +01:00
|
|
|
export interface ILocationPickerProps {
|
2022-01-12 13:32:08 +00:00
|
|
|
sender: RoomMember;
|
2022-03-09 18:14:07 +01:00
|
|
|
shareType: LocationShareType;
|
2022-03-18 10:52:24 +01:00
|
|
|
onChoose: ShareLocationFn;
|
2021-12-17 12:26:02 +00:00
|
|
|
onFinished(ev?: SyntheticEvent): void;
|
2021-12-06 09:45:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface IState {
|
2022-03-21 12:42:58 +01:00
|
|
|
timeout: number;
|
2022-03-23 18:08:56 +01:00
|
|
|
position?: GenericPosition;
|
2022-03-11 09:52:57 +01:00
|
|
|
error?: LocationShareError;
|
2021-12-06 09:45:12 +00:00
|
|
|
}
|
|
|
|
|
|
2022-03-17 09:26:47 +01:00
|
|
|
const isSharingOwnLocation = (shareType: LocationShareType): boolean =>
|
|
|
|
|
shareType === LocationShareType.Own || shareType === LocationShareType.Live;
|
2022-01-10 10:07:51 +00:00
|
|
|
|
2022-03-01 18:00:07 +01:00
|
|
|
class LocationPicker extends React.Component<ILocationPickerProps, IState> {
|
2022-01-12 13:32:08 +00:00
|
|
|
public static contextType = MatrixClientContext;
|
|
|
|
|
public context!: React.ContextType<typeof MatrixClientContext>;
|
2022-01-27 09:51:06 +00:00
|
|
|
private map?: maplibregl.Map = null;
|
|
|
|
|
private geolocate?: maplibregl.GeolocateControl = null;
|
|
|
|
|
private marker?: maplibregl.Marker = null;
|
2021-12-06 09:45:12 +00:00
|
|
|
|
2022-03-01 18:00:07 +01:00
|
|
|
constructor(props: ILocationPickerProps) {
|
2021-12-06 09:45:12 +00:00
|
|
|
super(props);
|
|
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
|
position: undefined,
|
2022-03-21 12:42:58 +01:00
|
|
|
timeout: DEFAULT_DURATION_MS,
|
2021-12-06 09:45:12 +00:00
|
|
|
error: undefined,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-12 13:32:08 +00:00
|
|
|
private getMarkerId = () => {
|
|
|
|
|
return "mx_MLocationPicker_marker";
|
|
|
|
|
};
|
|
|
|
|
|
2021-12-06 09:45:12 +00:00
|
|
|
componentDidMount() {
|
2022-02-22 12:18:08 +00:00
|
|
|
this.context.on(ClientEvent.ClientWellKnown, this.updateStyleUrl);
|
2021-12-06 09:45:12 +00:00
|
|
|
|
2021-12-07 09:31:13 +00:00
|
|
|
try {
|
2022-01-27 09:51:06 +00:00
|
|
|
this.map = new maplibregl.Map({
|
|
|
|
|
container: 'mx_LocationPicker_map',
|
|
|
|
|
style: findMapStyleUrl(),
|
|
|
|
|
center: [0, 0],
|
|
|
|
|
zoom: 1,
|
|
|
|
|
});
|
|
|
|
|
|
2021-12-07 09:31:13 +00:00
|
|
|
// Add geolocate control to the map.
|
|
|
|
|
this.geolocate = new maplibregl.GeolocateControl({
|
|
|
|
|
positionOptions: {
|
|
|
|
|
enableHighAccuracy: true,
|
|
|
|
|
},
|
2022-05-03 14:03:56 +02:00
|
|
|
trackUserLocation: false,
|
2021-12-07 09:31:13 +00:00
|
|
|
});
|
2021-12-06 09:45:12 +00:00
|
|
|
|
2022-03-09 18:14:07 +01:00
|
|
|
this.map.addControl(this.geolocate);
|
2022-01-12 13:32:08 +00:00
|
|
|
|
2021-12-07 09:31:13 +00:00
|
|
|
this.map.on('error', (e) => {
|
2021-12-17 12:26:02 +00:00
|
|
|
logger.error(
|
|
|
|
|
"Failed to load map: check map_style_url in config.json "
|
2022-03-11 09:52:57 +01:00
|
|
|
+ "has a valid URL and API key",
|
2021-12-17 12:26:02 +00:00
|
|
|
e.error,
|
|
|
|
|
);
|
2022-03-11 09:52:57 +01:00
|
|
|
this.setState({ error: LocationShareError.MapStyleUrlNotReachable });
|
2021-12-07 09:31:13 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.map.on('load', () => {
|
|
|
|
|
this.geolocate.trigger();
|
|
|
|
|
});
|
|
|
|
|
|
2022-02-25 13:21:48 -05:00
|
|
|
this.geolocate.on('error', this.onGeolocateError);
|
2022-03-09 18:14:07 +01:00
|
|
|
|
2022-03-17 09:26:47 +01:00
|
|
|
if (isSharingOwnLocation(this.props.shareType)) {
|
2022-03-09 18:14:07 +01:00
|
|
|
this.geolocate.on('geolocate', this.onGeolocate);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.props.shareType === LocationShareType.Pin) {
|
|
|
|
|
const navigationControl = new maplibregl.NavigationControl({
|
|
|
|
|
showCompass: false, showZoom: true,
|
|
|
|
|
});
|
|
|
|
|
this.map.addControl(navigationControl, 'bottom-right');
|
|
|
|
|
this.map.on('click', this.onClick);
|
|
|
|
|
}
|
2021-12-07 09:31:13 +00:00
|
|
|
} catch (e) {
|
2022-01-27 09:51:06 +00:00
|
|
|
logger.error("Failed to render map", e);
|
2022-03-11 09:52:57 +01:00
|
|
|
const errorType = e?.message === LocationShareError.MapStyleUrlNotConfigured ?
|
|
|
|
|
LocationShareError.MapStyleUrlNotConfigured :
|
|
|
|
|
LocationShareError.Default;
|
|
|
|
|
this.setState({ error: errorType });
|
2021-12-07 09:31:13 +00:00
|
|
|
}
|
2021-12-06 09:45:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
2022-02-25 13:21:48 -05:00
|
|
|
this.geolocate?.off('error', this.onGeolocateError);
|
2021-12-07 09:31:13 +00:00
|
|
|
this.geolocate?.off('geolocate', this.onGeolocate);
|
2022-03-09 18:14:07 +01:00
|
|
|
this.map?.off('click', this.onClick);
|
2022-02-22 12:18:08 +00:00
|
|
|
this.context.off(ClientEvent.ClientWellKnown, this.updateStyleUrl);
|
2021-12-06 09:45:12 +00:00
|
|
|
}
|
|
|
|
|
|
2022-03-09 18:14:07 +01:00
|
|
|
private addMarkerToMap = () => {
|
|
|
|
|
this.marker = new maplibregl.Marker({
|
|
|
|
|
element: document.getElementById(this.getMarkerId()),
|
|
|
|
|
anchor: 'bottom',
|
|
|
|
|
offset: [0, -1],
|
|
|
|
|
}).setLngLat(new maplibregl.LngLat(0, 0))
|
|
|
|
|
.addTo(this.map);
|
|
|
|
|
};
|
|
|
|
|
|
2022-01-27 09:51:06 +00:00
|
|
|
private updateStyleUrl = (clientWellKnown: IClientWellKnown) => {
|
|
|
|
|
const style = tileServerFromWellKnown(clientWellKnown)?.["map_style_url"];
|
|
|
|
|
if (style) {
|
|
|
|
|
this.map?.setStyle(style);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2021-12-06 10:13:06 +00:00
|
|
|
private onGeolocate = (position: GeolocationPosition) => {
|
2022-03-09 18:14:07 +01:00
|
|
|
if (!this.marker) {
|
|
|
|
|
this.addMarkerToMap();
|
|
|
|
|
}
|
|
|
|
|
this.setState({ position: genericPositionFromGeolocation(position) });
|
2022-01-27 09:51:06 +00:00
|
|
|
this.marker?.setLngLat(
|
2022-01-12 13:32:08 +00:00
|
|
|
new maplibregl.LngLat(
|
|
|
|
|
position.coords.longitude,
|
|
|
|
|
position.coords.latitude,
|
|
|
|
|
),
|
|
|
|
|
);
|
2022-02-25 13:21:48 -05:00
|
|
|
};
|
|
|
|
|
|
2022-03-09 18:14:07 +01:00
|
|
|
private onClick = (event: MapMouseEvent) => {
|
|
|
|
|
if (!this.marker) {
|
|
|
|
|
this.addMarkerToMap();
|
|
|
|
|
}
|
|
|
|
|
this.marker?.setLngLat(event.lngLat);
|
|
|
|
|
this.setState({
|
|
|
|
|
position: {
|
|
|
|
|
timestamp: Date.now(),
|
|
|
|
|
latitude: event.lngLat.lat,
|
|
|
|
|
longitude: event.lngLat.lng,
|
2022-02-25 13:21:48 -05:00
|
|
|
},
|
2022-03-09 18:14:07 +01:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private onGeolocateError = (e: GeolocationPositionError) => {
|
|
|
|
|
logger.error("Could not fetch location", e);
|
|
|
|
|
// close the dialog and show an error when trying to share own location
|
|
|
|
|
// pin drop location without permissions is ok
|
2022-03-17 09:26:47 +01:00
|
|
|
if (isSharingOwnLocation(this.props.shareType)) {
|
2022-03-09 18:14:07 +01:00
|
|
|
this.props.onFinished();
|
2022-06-14 17:51:51 +01:00
|
|
|
Modal.createDialog(ErrorDialog, {
|
|
|
|
|
title: _t("Could not fetch location"),
|
|
|
|
|
description: positionFailureMessage(e.code),
|
|
|
|
|
});
|
2022-03-09 18:14:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.geolocate) {
|
|
|
|
|
this.map?.removeControl(this.geolocate);
|
|
|
|
|
}
|
2021-12-06 09:45:12 +00:00
|
|
|
};
|
|
|
|
|
|
2022-03-21 12:42:58 +01:00
|
|
|
private onTimeoutChange = (timeout: number): void => {
|
|
|
|
|
this.setState({ timeout });
|
|
|
|
|
};
|
2021-12-06 10:13:06 +00:00
|
|
|
|
2022-03-21 12:42:58 +01:00
|
|
|
private onOk = () => {
|
|
|
|
|
const { timeout, position } = this.state;
|
|
|
|
|
|
|
|
|
|
this.props.onChoose(
|
|
|
|
|
position ? { uri: getGeoUri(position), timestamp: position.timestamp, timeout } : {
|
|
|
|
|
timeout,
|
|
|
|
|
});
|
2021-12-06 09:45:12 +00:00
|
|
|
this.props.onFinished();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
render() {
|
2022-03-11 09:52:57 +01:00
|
|
|
if (this.state.error) {
|
|
|
|
|
return <div className="mx_LocationPicker mx_LocationPicker_hasError">
|
|
|
|
|
<MapError
|
|
|
|
|
error={this.state.error}
|
|
|
|
|
onFinished={this.props.onFinished} />
|
|
|
|
|
</div>;
|
|
|
|
|
}
|
2021-12-06 09:45:12 +00:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="mx_LocationPicker">
|
|
|
|
|
<div id="mx_LocationPicker_map" />
|
2022-04-11 18:40:06 +02:00
|
|
|
|
2022-03-09 18:14:07 +01:00
|
|
|
{ this.props.shareType === LocationShareType.Pin && <div className="mx_LocationPicker_pinText">
|
|
|
|
|
<span>
|
|
|
|
|
{ this.state.position ? _t("Click to move the pin") : _t("Click to drop a pin") }
|
|
|
|
|
</span>
|
|
|
|
|
</div>
|
|
|
|
|
}
|
2021-12-06 09:45:12 +00:00
|
|
|
<div className="mx_LocationPicker_footer">
|
|
|
|
|
<form onSubmit={this.onOk}>
|
2022-03-21 12:42:58 +01:00
|
|
|
{ this.props.shareType === LocationShareType.Live &&
|
|
|
|
|
<LiveDurationDropdown
|
|
|
|
|
onChange={this.onTimeoutChange}
|
|
|
|
|
timeout={this.state.timeout}
|
|
|
|
|
/>
|
|
|
|
|
}
|
2022-03-09 18:14:07 +01:00
|
|
|
<AccessibleButton
|
|
|
|
|
data-test-id="location-picker-submit-button"
|
|
|
|
|
type="submit"
|
|
|
|
|
element='button'
|
|
|
|
|
kind='primary'
|
|
|
|
|
className='mx_LocationPicker_submitButton'
|
|
|
|
|
disabled={!this.state.position}
|
|
|
|
|
onClick={this.onOk}>
|
|
|
|
|
{ _t('Share location') }
|
|
|
|
|
</AccessibleButton>
|
2021-12-06 09:45:12 +00:00
|
|
|
</form>
|
|
|
|
|
</div>
|
2022-04-06 13:40:39 +02:00
|
|
|
<div id={this.getMarkerId()}>
|
2022-03-21 12:42:58 +01:00
|
|
|
{ /*
|
|
|
|
|
maplibregl hijacks the div above to style the marker
|
|
|
|
|
it must be in the dom when the map is initialised
|
|
|
|
|
and keep a consistent class
|
|
|
|
|
we want to hide the marker until it is set in the case of pin drop
|
|
|
|
|
so hide the internal visible elements
|
|
|
|
|
*/ }
|
|
|
|
|
|
2022-04-06 13:40:39 +02:00
|
|
|
{ !!this.marker && <Marker
|
|
|
|
|
roomMember={isSharingOwnLocation(this.props.shareType) ? this.props.sender : undefined}
|
|
|
|
|
useMemberColor={this.props.shareType === LocationShareType.Live}
|
|
|
|
|
/>
|
|
|
|
|
}
|
2022-01-12 13:32:08 +00:00
|
|
|
</div>
|
2021-12-06 09:45:12 +00:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default LocationPicker;
|
2022-01-13 14:38:04 +00:00
|
|
|
|
|
|
|
|
function positionFailureMessage(code: number): string {
|
2022-03-23 16:31:25 +00:00
|
|
|
const brand = SdkConfig.get().brand;
|
2022-01-13 14:38:04 +00:00
|
|
|
switch (code) {
|
|
|
|
|
case 1: return _t(
|
2022-03-23 16:31:25 +00:00
|
|
|
"%(brand)s was denied permission to fetch your location. " +
|
|
|
|
|
"Please allow location access in your browser settings.", { brand },
|
2022-01-13 14:38:04 +00:00
|
|
|
);
|
|
|
|
|
case 2: return _t(
|
|
|
|
|
"Failed to fetch your location. Please try again later.",
|
|
|
|
|
);
|
|
|
|
|
case 3: return _t(
|
|
|
|
|
"Timed out trying to fetch your location. Please try again later.",
|
|
|
|
|
);
|
|
|
|
|
case 4: return _t(
|
|
|
|
|
"Unknown error fetching location. Please try again later.",
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|