Live location sharing - refresh beacon expiry in room (#8116)
* add duration dropdown to live location picker Signed-off-by: Kerry Archibald <kerrya@element.io> * tidy comments Signed-off-by: Kerry Archibald <kerrya@element.io> * setup component Signed-off-by: Kerry Archibald <kerrya@element.io> * replace references to beaconInfoId with beacon.identifier Signed-off-by: Kerry Archibald <kerrya@element.io> * icon Signed-off-by: Kerry Archibald <kerrya@element.io> * component for styled live beacon icon Signed-off-by: Kerry Archibald <kerrya@element.io> * emit liveness change whenever livebeaconIds changes Signed-off-by: Kerry Archibald <kerrya@element.io> * Handle multiple live beacons in room share warning, test Signed-off-by: Kerry Archibald <kerrya@element.io> * un xdescribe beaconstore tests Signed-off-by: Kerry Archibald <kerrya@element.io> * missed copyrights Signed-off-by: Kerry Archibald <kerrya@element.io> * i18n Signed-off-by: Kerry Archibald <kerrya@element.io> * refresh beacon time remaining Signed-off-by: Kerry Archibald <kerrya@element.io> * kill timeout Signed-off-by: Kerry Archibald <kerrya@element.io> * use useInterval Signed-off-by: Kerry Archibald <kerrya@element.io> * beacon not optional in useMsRemaining Signed-off-by: Kerry Archibald <kerrya@element.io> * just use single "you are sharing" message Signed-off-by: Kerry Archibald <kerrya@element.io> * trigger Signed-off-by: Kerry Archibald <kerrya@element.io> * i18n Signed-off-by: Kerry Archibald <kerrya@element.io> * i18n again Signed-off-by: Kerry Archibald <kerrya@element.io>
This commit is contained in:
parent
9b2944294c
commit
752ad6a9f9
4 changed files with 201 additions and 43 deletions
|
@ -14,9 +14,9 @@ See the License for the specific language governing permissions and
|
|||
limitations under the License.
|
||||
*/
|
||||
|
||||
import React, { useEffect, useState } from 'react';
|
||||
import React, { useCallback, useEffect, useState } from 'react';
|
||||
import classNames from 'classnames';
|
||||
import { Room } from 'matrix-js-sdk/src/matrix';
|
||||
import { Room, Beacon } from 'matrix-js-sdk/src/matrix';
|
||||
|
||||
import { _t } from '../../../languageHandler';
|
||||
import { useEventEmitterState } from '../../../hooks/useEventEmitter';
|
||||
|
@ -26,25 +26,57 @@ import StyledLiveBeaconIcon from './StyledLiveBeaconIcon';
|
|||
import { formatDuration } from '../../../DateUtils';
|
||||
import { getBeaconMsUntilExpiry, sortBeaconsByLatestExpiry } from '../../../utils/beacon';
|
||||
import Spinner from '../elements/Spinner';
|
||||
import { useInterval } from '../../../hooks/useTimeout';
|
||||
|
||||
interface Props {
|
||||
roomId: Room['roomId'];
|
||||
}
|
||||
|
||||
const MINUTE_MS = 60000;
|
||||
const HOUR_MS = MINUTE_MS * 60;
|
||||
|
||||
const getUpdateInterval = (ms: number) => {
|
||||
// every 10 mins when more than an hour
|
||||
if (ms > HOUR_MS) {
|
||||
return MINUTE_MS * 10;
|
||||
}
|
||||
// every minute when more than a minute
|
||||
if (ms > MINUTE_MS) {
|
||||
return MINUTE_MS;
|
||||
}
|
||||
// otherwise every second
|
||||
return 1000;
|
||||
};
|
||||
const useMsRemaining = (beacon: Beacon): number => {
|
||||
const [msRemaining, setMsRemaining] = useState(() => getBeaconMsUntilExpiry(beacon));
|
||||
|
||||
useEffect(() => {
|
||||
setMsRemaining(getBeaconMsUntilExpiry(beacon));
|
||||
}, [beacon]);
|
||||
|
||||
const updateMsRemaining = useCallback(() => {
|
||||
const ms = getBeaconMsUntilExpiry(beacon);
|
||||
setMsRemaining(ms);
|
||||
}, [beacon]);
|
||||
|
||||
useInterval(updateMsRemaining, getUpdateInterval(msRemaining));
|
||||
|
||||
return msRemaining;
|
||||
};
|
||||
|
||||
/**
|
||||
* It's technically possible to have multiple live beacons in one room
|
||||
* Select the latest expiry to display,
|
||||
* and kill all beacons on stop sharing
|
||||
*/
|
||||
type LiveBeaconsState = {
|
||||
liveBeaconIds: string[];
|
||||
msRemaining?: number;
|
||||
beacon?: Beacon;
|
||||
onStopSharing?: () => void;
|
||||
stoppingInProgress?: boolean;
|
||||
};
|
||||
|
||||
const useLiveBeacons = (roomId: Room['roomId']): LiveBeaconsState => {
|
||||
const [stoppingInProgress, setStoppingInProgress] = useState(false);
|
||||
|
||||
const liveBeaconIds = useEventEmitterState(
|
||||
OwnBeaconStore.instance,
|
||||
OwnBeaconStoreEvent.LivenessChange,
|
||||
|
@ -57,7 +89,7 @@ const useLiveBeacons = (roomId: Room['roomId']): LiveBeaconsState => {
|
|||
}, [liveBeaconIds]);
|
||||
|
||||
if (!liveBeaconIds?.length) {
|
||||
return { liveBeaconIds };
|
||||
return {};
|
||||
}
|
||||
|
||||
// select the beacon with latest expiry to display expiry time
|
||||
|
@ -77,40 +109,43 @@ const useLiveBeacons = (roomId: Room['roomId']): LiveBeaconsState => {
|
|||
}
|
||||
};
|
||||
|
||||
const msRemaining = getBeaconMsUntilExpiry(beacon);
|
||||
return { onStopSharing, beacon, stoppingInProgress };
|
||||
};
|
||||
|
||||
return { liveBeaconIds, onStopSharing, msRemaining, stoppingInProgress };
|
||||
const LiveTimeRemaining: React.FC<{ beacon: Beacon }> = ({ beacon }) => {
|
||||
const msRemaining = useMsRemaining(beacon);
|
||||
|
||||
const timeRemaining = formatDuration(msRemaining);
|
||||
const liveTimeRemaining = _t(`%(timeRemaining)s left`, { timeRemaining });
|
||||
|
||||
return <span
|
||||
data-test-id='room-live-share-expiry'
|
||||
className="mx_RoomLiveShareWarning_expiry"
|
||||
>{ liveTimeRemaining }</span>;
|
||||
};
|
||||
|
||||
const RoomLiveShareWarning: React.FC<Props> = ({ roomId }) => {
|
||||
const {
|
||||
liveBeaconIds,
|
||||
onStopSharing,
|
||||
msRemaining,
|
||||
beacon,
|
||||
stoppingInProgress,
|
||||
} = useLiveBeacons(roomId);
|
||||
|
||||
if (!liveBeaconIds?.length) {
|
||||
if (!beacon) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const timeRemaining = formatDuration(msRemaining);
|
||||
const liveTimeRemaining = _t(`%(timeRemaining)s left`, { timeRemaining });
|
||||
|
||||
return <div
|
||||
className={classNames('mx_RoomLiveShareWarning')}
|
||||
>
|
||||
<StyledLiveBeaconIcon className="mx_RoomLiveShareWarning_icon" />
|
||||
<span className="mx_RoomLiveShareWarning_label">
|
||||
{ _t('You are sharing %(count)s live locations', { count: liveBeaconIds.length }) }
|
||||
{ _t('You are sharing your live location') }
|
||||
</span>
|
||||
|
||||
{ stoppingInProgress ?
|
||||
<span className='mx_RoomLiveShareWarning_spinner'><Spinner h={16} w={16} /></span> :
|
||||
<span
|
||||
data-test-id='room-live-share-expiry'
|
||||
className="mx_RoomLiveShareWarning_expiry"
|
||||
>{ liveTimeRemaining }</span>
|
||||
<LiveTimeRemaining beacon={beacon} />
|
||||
}
|
||||
<AccessibleButton
|
||||
data-test-id='room-live-share-stop-sharing'
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue