2022-04-14 15:14:05 +02:00
|
|
|
/*
|
|
|
|
|
Copyright 2022 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.
|
|
|
|
|
*/
|
|
|
|
|
|
2022-07-18 10:34:39 +02:00
|
|
|
import React, { useState, useEffect } from 'react';
|
2022-04-14 15:14:05 +02:00
|
|
|
import { MatrixClient } from 'matrix-js-sdk/src/client';
|
|
|
|
|
import {
|
|
|
|
|
Beacon,
|
|
|
|
|
Room,
|
|
|
|
|
} from 'matrix-js-sdk/src/matrix';
|
|
|
|
|
import maplibregl from 'maplibre-gl';
|
|
|
|
|
|
2022-04-20 10:14:24 +02:00
|
|
|
import { Icon as LiveLocationIcon } from '../../../../res/img/location/live-location.svg';
|
2022-04-14 15:14:05 +02:00
|
|
|
import { useLiveBeacons } from '../../../utils/beacon/useLiveBeacons';
|
|
|
|
|
import MatrixClientContext from '../../../contexts/MatrixClientContext';
|
|
|
|
|
import BaseDialog from "../dialogs/BaseDialog";
|
|
|
|
|
import { IDialogProps } from "../dialogs/IDialogProps";
|
|
|
|
|
import Map from '../location/Map';
|
|
|
|
|
import ZoomButtons from '../location/ZoomButtons';
|
|
|
|
|
import BeaconMarker from './BeaconMarker';
|
2022-04-19 13:35:39 +02:00
|
|
|
import { Bounds, getBeaconBounds } from '../../../utils/beacon/bounds';
|
|
|
|
|
import { getGeoUri } from '../../../utils/beacon';
|
|
|
|
|
import { _t } from '../../../languageHandler';
|
|
|
|
|
import AccessibleButton from '../elements/AccessibleButton';
|
2022-04-20 10:14:24 +02:00
|
|
|
import DialogSidebar from './DialogSidebar';
|
2022-04-21 09:06:57 +02:00
|
|
|
import DialogOwnBeaconStatus from './DialogOwnBeaconStatus';
|
2022-05-13 10:30:53 +02:00
|
|
|
import BeaconStatusTooltip from './BeaconStatusTooltip';
|
2022-05-20 11:49:01 +02:00
|
|
|
import MapFallback from '../location/MapFallback';
|
2022-07-06 16:34:33 +02:00
|
|
|
import { MapError } from '../location/MapError';
|
|
|
|
|
import { LocationShareError } from '../../../utils/location';
|
2022-04-14 15:14:05 +02:00
|
|
|
|
|
|
|
|
interface IProps extends IDialogProps {
|
|
|
|
|
roomId: Room['roomId'];
|
|
|
|
|
matrixClient: MatrixClient;
|
2022-04-19 13:35:39 +02:00
|
|
|
// open the map centered on this beacon's location
|
2022-07-18 10:34:39 +02:00
|
|
|
initialFocusedBeacon?: Beacon;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// track the 'focused time' as ts
|
|
|
|
|
// to make it possible to refocus the same beacon
|
|
|
|
|
// as the beacon location may change
|
|
|
|
|
// or the map may move around
|
|
|
|
|
interface FocusedBeaconState {
|
|
|
|
|
ts: number;
|
|
|
|
|
beacon?: Beacon;
|
2022-04-14 15:14:05 +02:00
|
|
|
}
|
|
|
|
|
|
2022-04-19 13:35:39 +02:00
|
|
|
const getBoundsCenter = (bounds: Bounds): string | undefined => {
|
|
|
|
|
if (!bounds) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
return getGeoUri({
|
|
|
|
|
latitude: (bounds.north + bounds.south) / 2,
|
|
|
|
|
longitude: (bounds.east + bounds.west) / 2,
|
|
|
|
|
timestamp: Date.now(),
|
|
|
|
|
});
|
2022-04-14 15:14:05 +02:00
|
|
|
};
|
|
|
|
|
|
2022-07-18 10:34:39 +02:00
|
|
|
const useInitialMapPosition = (liveBeacons: Beacon[], { beacon, ts }: FocusedBeaconState): {
|
2022-04-21 18:56:11 +02:00
|
|
|
bounds?: Bounds; centerGeoUri: string;
|
|
|
|
|
} => {
|
2022-07-18 10:34:39 +02:00
|
|
|
const [bounds, setBounds] = useState<Bounds | undefined>(getBeaconBounds(liveBeacons));
|
|
|
|
|
const [centerGeoUri, setCenterGeoUri] = useState<string>(
|
|
|
|
|
beacon?.latestLocationState?.uri ||
|
|
|
|
|
getBoundsCenter(bounds),
|
2022-04-21 18:56:11 +02:00
|
|
|
);
|
2022-07-18 10:34:39 +02:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (
|
|
|
|
|
// this check ignores the first initial focused beacon state
|
|
|
|
|
// as centering logic on map zooms to show everything
|
|
|
|
|
// instead of focusing down
|
|
|
|
|
ts !== 0 &&
|
|
|
|
|
// only set focus to a known location
|
|
|
|
|
beacon?.latestLocationState?.uri
|
|
|
|
|
) {
|
|
|
|
|
// append custom `mxTs` parameter to geoUri
|
|
|
|
|
// so map is triggered to refocus on this uri
|
|
|
|
|
// event if it was previously the center geouri
|
|
|
|
|
// but the map have moved/zoomed
|
|
|
|
|
setCenterGeoUri(`${beacon?.latestLocationState?.uri};mxTs=${Date.now()}`);
|
|
|
|
|
setBounds(getBeaconBounds([beacon]));
|
|
|
|
|
}
|
|
|
|
|
}, [beacon, ts]);
|
|
|
|
|
|
|
|
|
|
return { bounds, centerGeoUri };
|
2022-04-21 18:56:11 +02:00
|
|
|
};
|
|
|
|
|
|
2022-04-14 15:14:05 +02:00
|
|
|
/**
|
|
|
|
|
* Dialog to view live beacons maximised
|
|
|
|
|
*/
|
2022-04-19 13:35:39 +02:00
|
|
|
const BeaconViewDialog: React.FC<IProps> = ({
|
2022-07-18 10:34:39 +02:00
|
|
|
initialFocusedBeacon,
|
2022-04-19 13:35:39 +02:00
|
|
|
roomId,
|
|
|
|
|
matrixClient,
|
|
|
|
|
onFinished,
|
|
|
|
|
}) => {
|
2022-04-14 15:14:05 +02:00
|
|
|
const liveBeacons = useLiveBeacons(roomId, matrixClient);
|
2022-07-18 10:34:39 +02:00
|
|
|
const [focusedBeaconState, setFocusedBeaconState] =
|
|
|
|
|
useState<FocusedBeaconState>({ beacon: initialFocusedBeacon, ts: 0 });
|
2022-04-14 15:14:05 +02:00
|
|
|
|
2022-04-20 10:14:24 +02:00
|
|
|
const [isSidebarOpen, setSidebarOpen] = useState(false);
|
|
|
|
|
|
2022-07-18 10:34:39 +02:00
|
|
|
const { bounds, centerGeoUri } = useInitialMapPosition(liveBeacons, focusedBeaconState);
|
2022-04-14 15:14:05 +02:00
|
|
|
|
2022-07-06 16:34:33 +02:00
|
|
|
const [mapDisplayError, setMapDisplayError] = useState<Error>();
|
|
|
|
|
|
|
|
|
|
// automatically open the sidebar if there is no map to see
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (mapDisplayError) {
|
|
|
|
|
setSidebarOpen(true);
|
|
|
|
|
}
|
|
|
|
|
}, [mapDisplayError]);
|
|
|
|
|
|
2022-07-18 10:34:39 +02:00
|
|
|
const onBeaconListItemClick = (beacon: Beacon) => {
|
|
|
|
|
setFocusedBeaconState({ beacon, ts: Date.now() });
|
|
|
|
|
};
|
|
|
|
|
|
2022-04-14 15:14:05 +02:00
|
|
|
return (
|
|
|
|
|
<BaseDialog
|
|
|
|
|
className='mx_BeaconViewDialog'
|
|
|
|
|
onFinished={onFinished}
|
|
|
|
|
fixedWidth={false}
|
|
|
|
|
>
|
|
|
|
|
<MatrixClientContext.Provider value={matrixClient}>
|
2022-07-06 16:34:33 +02:00
|
|
|
{ (!!liveBeacons?.length && !mapDisplayError) && <Map
|
2022-04-14 15:14:05 +02:00
|
|
|
id='mx_BeaconViewDialog'
|
2022-04-19 13:35:39 +02:00
|
|
|
bounds={bounds}
|
|
|
|
|
centerGeoUri={centerGeoUri}
|
2022-04-14 15:14:05 +02:00
|
|
|
interactive
|
2022-07-06 16:34:33 +02:00
|
|
|
onError={setMapDisplayError}
|
2022-04-14 15:14:05 +02:00
|
|
|
className="mx_BeaconViewDialog_map"
|
|
|
|
|
>
|
|
|
|
|
{
|
|
|
|
|
({ map }: { map: maplibregl.Map}) =>
|
|
|
|
|
<>
|
|
|
|
|
{ liveBeacons.map(beacon => <BeaconMarker
|
|
|
|
|
key={beacon.identifier}
|
|
|
|
|
map={map}
|
|
|
|
|
beacon={beacon}
|
2022-05-13 10:30:53 +02:00
|
|
|
tooltip={<BeaconStatusTooltip beacon={beacon} />}
|
2022-04-14 15:14:05 +02:00
|
|
|
/>) }
|
|
|
|
|
<ZoomButtons map={map} />
|
|
|
|
|
</>
|
|
|
|
|
}
|
2022-07-06 16:34:33 +02:00
|
|
|
</Map> }
|
|
|
|
|
{ mapDisplayError &&
|
|
|
|
|
<MapError
|
|
|
|
|
error={mapDisplayError.message as LocationShareError}
|
|
|
|
|
isMinimised
|
|
|
|
|
/>
|
|
|
|
|
}
|
|
|
|
|
{ !liveBeacons?.length && !mapDisplayError &&
|
2022-05-20 11:49:01 +02:00
|
|
|
<MapFallback
|
2022-04-19 13:35:39 +02:00
|
|
|
data-test-id='beacon-view-dialog-map-fallback'
|
2022-05-20 11:49:01 +02:00
|
|
|
className='mx_BeaconViewDialog_map'
|
2022-04-19 13:35:39 +02:00
|
|
|
>
|
|
|
|
|
<span className='mx_BeaconViewDialog_mapFallbackMessage'>{ _t('No live locations') }</span>
|
|
|
|
|
<AccessibleButton
|
|
|
|
|
kind='primary'
|
|
|
|
|
onClick={onFinished}
|
|
|
|
|
data-test-id='beacon-view-dialog-fallback-close'
|
|
|
|
|
>
|
|
|
|
|
{ _t('Close') }
|
|
|
|
|
</AccessibleButton>
|
2022-05-20 11:49:01 +02:00
|
|
|
</MapFallback>
|
2022-04-19 13:35:39 +02:00
|
|
|
}
|
2022-04-20 10:14:24 +02:00
|
|
|
{ isSidebarOpen ?
|
2022-07-18 10:34:39 +02:00
|
|
|
<DialogSidebar beacons={liveBeacons} onBeaconClick={onBeaconListItemClick} requestClose={() => setSidebarOpen(false)} /> :
|
2022-04-20 10:14:24 +02:00
|
|
|
<AccessibleButton
|
|
|
|
|
kind='primary'
|
|
|
|
|
onClick={() => setSidebarOpen(true)}
|
|
|
|
|
data-test-id='beacon-view-dialog-open-sidebar'
|
|
|
|
|
className='mx_BeaconViewDialog_viewListButton'
|
|
|
|
|
>
|
|
|
|
|
<LiveLocationIcon height={12} />
|
|
|
|
|
{ _t('View list') }
|
|
|
|
|
</AccessibleButton>
|
|
|
|
|
}
|
2022-04-21 09:06:57 +02:00
|
|
|
<DialogOwnBeaconStatus roomId={roomId} />
|
2022-04-14 15:14:05 +02:00
|
|
|
</MatrixClientContext.Provider>
|
|
|
|
|
</BaseDialog>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default BeaconViewDialog;
|