2022-03-22 14:57:12 +01: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-04-13 10:44:15 +02:00
|
|
|
import React from 'react';
|
|
|
|
|
import { Room } from 'matrix-js-sdk/src/matrix';
|
2022-03-22 14:57:12 +01:00
|
|
|
|
|
|
|
|
import { _t } from '../../../languageHandler';
|
|
|
|
|
import { useEventEmitterState } from '../../../hooks/useEventEmitter';
|
2022-03-31 10:57:12 +02:00
|
|
|
import { OwnBeaconStore, OwnBeaconStoreEvent } from '../../../stores/OwnBeaconStore';
|
2022-04-13 10:44:15 +02:00
|
|
|
import { useOwnLiveBeacons } from '../../../utils/beacon';
|
2022-06-07 12:15:09 +02:00
|
|
|
import AccessibleButton, { ButtonEvent } from '../elements/AccessibleButton';
|
2022-03-31 10:57:12 +02:00
|
|
|
import Spinner from '../elements/Spinner';
|
|
|
|
|
import StyledLiveBeaconIcon from './StyledLiveBeaconIcon';
|
|
|
|
|
import { Icon as CloseIcon } from '../../../../res/img/image-view/close.svg';
|
2022-04-12 09:24:17 +02:00
|
|
|
import LiveTimeRemaining from './LiveTimeRemaining';
|
2022-06-07 12:15:09 +02:00
|
|
|
import dispatcher from '../../../dispatcher/dispatcher';
|
|
|
|
|
import { ViewRoomPayload } from '../../../dispatcher/payloads/ViewRoomPayload';
|
|
|
|
|
import { Action } from '../../../dispatcher/actions';
|
2022-03-23 11:12:58 +01:00
|
|
|
|
2022-04-25 14:44:18 +02:00
|
|
|
const getLabel = (hasLocationPublishError: boolean, hasStopSharingError: boolean): string => {
|
|
|
|
|
if (hasLocationPublishError) {
|
2022-05-09 16:52:05 -06:00
|
|
|
return _t('An error occurred whilst sharing your live location, please try again');
|
2022-03-31 10:57:12 +02:00
|
|
|
}
|
|
|
|
|
if (hasStopSharingError) {
|
|
|
|
|
return _t('An error occurred while stopping your live location, please try again');
|
|
|
|
|
}
|
|
|
|
|
return _t('You are sharing your live location');
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
interface RoomLiveShareWarningInnerProps {
|
|
|
|
|
liveBeaconIds: string[];
|
|
|
|
|
roomId: Room['roomId'];
|
|
|
|
|
}
|
|
|
|
|
const RoomLiveShareWarningInner: React.FC<RoomLiveShareWarningInnerProps> = ({ liveBeaconIds, roomId }) => {
|
2022-03-22 14:57:12 +01:00
|
|
|
const {
|
|
|
|
|
onStopSharing,
|
2022-04-25 14:44:18 +02:00
|
|
|
onResetLocationPublishError,
|
2022-03-23 11:12:58 +01:00
|
|
|
beacon,
|
2022-03-22 14:57:12 +01:00
|
|
|
stoppingInProgress,
|
2022-03-30 14:31:19 +02:00
|
|
|
hasStopSharingError,
|
2022-04-25 14:44:18 +02:00
|
|
|
hasLocationPublishError,
|
2022-04-13 10:44:15 +02:00
|
|
|
} = useOwnLiveBeacons(liveBeaconIds);
|
2022-03-22 14:57:12 +01:00
|
|
|
|
2022-03-23 11:12:58 +01:00
|
|
|
if (!beacon) {
|
2022-03-22 14:57:12 +01:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-25 14:44:18 +02:00
|
|
|
const hasError = hasStopSharingError || hasLocationPublishError;
|
2022-03-31 10:57:12 +02:00
|
|
|
|
2022-06-07 12:15:09 +02:00
|
|
|
// eat events from buttons so navigate to tile
|
|
|
|
|
// is not triggered
|
|
|
|
|
const stopPropagationWrapper = (callback: () => void) => (e?: ButtonEvent) => {
|
|
|
|
|
e?.stopPropagation();
|
|
|
|
|
callback();
|
|
|
|
|
};
|
|
|
|
|
|
2022-03-31 10:57:12 +02:00
|
|
|
const onButtonClick = () => {
|
2022-04-25 14:44:18 +02:00
|
|
|
if (hasLocationPublishError) {
|
|
|
|
|
onResetLocationPublishError();
|
2022-03-31 10:57:12 +02:00
|
|
|
} else {
|
|
|
|
|
onStopSharing();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2022-06-07 12:15:09 +02:00
|
|
|
const onClick = () => {
|
|
|
|
|
dispatcher.dispatch<ViewRoomPayload>({
|
|
|
|
|
action: Action.ViewRoom,
|
|
|
|
|
room_id: beacon.roomId,
|
|
|
|
|
metricsTrigger: undefined,
|
|
|
|
|
event_id: beacon.beaconInfoId,
|
|
|
|
|
scroll_into_view: true,
|
|
|
|
|
highlighted: true,
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2022-03-22 14:57:12 +01:00
|
|
|
return <div
|
2022-04-28 14:03:51 +02:00
|
|
|
className='mx_RoomLiveShareWarning'
|
2022-06-07 12:15:09 +02:00
|
|
|
onClick={onClick}
|
2022-03-22 14:57:12 +01:00
|
|
|
>
|
2022-03-31 10:57:12 +02:00
|
|
|
<StyledLiveBeaconIcon className="mx_RoomLiveShareWarning_icon" withError={hasError} />
|
|
|
|
|
|
2022-03-22 14:57:12 +01:00
|
|
|
<span className="mx_RoomLiveShareWarning_label">
|
2022-04-25 14:44:18 +02:00
|
|
|
{ getLabel(hasLocationPublishError, hasStopSharingError) }
|
2022-03-22 14:57:12 +01:00
|
|
|
</span>
|
|
|
|
|
|
2022-03-30 14:31:19 +02:00
|
|
|
{ stoppingInProgress &&
|
|
|
|
|
<span className='mx_RoomLiveShareWarning_spinner'><Spinner h={16} w={16} /></span>
|
2022-03-22 14:57:12 +01:00
|
|
|
}
|
2022-03-31 10:57:12 +02:00
|
|
|
{ !stoppingInProgress && !hasError && <LiveTimeRemaining beacon={beacon} /> }
|
2022-03-30 14:31:19 +02:00
|
|
|
|
2022-03-22 14:57:12 +01:00
|
|
|
<AccessibleButton
|
2022-04-13 10:44:15 +02:00
|
|
|
className='mx_RoomLiveShareWarning_stopButton'
|
2022-03-31 10:57:12 +02:00
|
|
|
data-test-id='room-live-share-primary-button'
|
2022-06-07 12:15:09 +02:00
|
|
|
onClick={stopPropagationWrapper(onButtonClick)}
|
2022-03-22 14:57:12 +01:00
|
|
|
kind='danger'
|
|
|
|
|
element='button'
|
|
|
|
|
disabled={stoppingInProgress}
|
|
|
|
|
>
|
2022-07-13 08:50:38 +02:00
|
|
|
{ hasError ? _t('Retry') : _t('Stop') }
|
2022-03-22 14:57:12 +01:00
|
|
|
</AccessibleButton>
|
2022-04-25 14:44:18 +02:00
|
|
|
{ hasLocationPublishError && <AccessibleButton
|
2022-03-31 10:57:12 +02:00
|
|
|
data-test-id='room-live-share-wire-error-close-button'
|
2022-07-13 08:50:38 +02:00
|
|
|
title={_t('Stop and close')}
|
2022-03-31 10:57:12 +02:00
|
|
|
element='button'
|
|
|
|
|
className='mx_RoomLiveShareWarning_closeButton'
|
2022-06-07 12:15:09 +02:00
|
|
|
onClick={stopPropagationWrapper(onStopSharing)}
|
2022-03-31 10:57:12 +02:00
|
|
|
>
|
|
|
|
|
<CloseIcon className='mx_RoomLiveShareWarning_closeButtonIcon' />
|
|
|
|
|
</AccessibleButton> }
|
2022-03-22 14:57:12 +01:00
|
|
|
</div>;
|
|
|
|
|
};
|
|
|
|
|
|
2022-03-31 10:57:12 +02:00
|
|
|
interface Props {
|
|
|
|
|
roomId: Room['roomId'];
|
|
|
|
|
}
|
|
|
|
|
const RoomLiveShareWarning: React.FC<Props> = ({ roomId }) => {
|
|
|
|
|
// do we have an active geolocation.watchPosition
|
|
|
|
|
const isMonitoringLiveLocation = useEventEmitterState(
|
|
|
|
|
OwnBeaconStore.instance,
|
|
|
|
|
OwnBeaconStoreEvent.MonitoringLivePosition,
|
|
|
|
|
() => OwnBeaconStore.instance.isMonitoringLiveLocation,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const liveBeaconIds = useEventEmitterState(
|
|
|
|
|
OwnBeaconStore.instance,
|
|
|
|
|
OwnBeaconStoreEvent.LivenessChange,
|
|
|
|
|
() => OwnBeaconStore.instance.getLiveBeaconIds(roomId),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (!isMonitoringLiveLocation || !liveBeaconIds.length) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// split into outer/inner to avoid watching various parts of live beacon state
|
|
|
|
|
// when there are none
|
|
|
|
|
return <RoomLiveShareWarningInner liveBeaconIds={liveBeaconIds} roomId={roomId} />;
|
|
|
|
|
};
|
|
|
|
|
|
2022-03-22 14:57:12 +01:00
|
|
|
export default RoomLiveShareWarning;
|