This repository has been archived by the owner on Sep 11, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 830
Live location sharing: live share warning in room #8100
Merged
Merged
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
b568a53
add duration dropdown to live location picker
fc00cb8
tidy comments
2638ffb
setup component
08c733e
replace references to beaconInfoId with beacon.identifier
1bbed14
icon
c34317f
component for styled live beacon icon
1afc8bd
emit liveness change whenever livebeaconIds changes
549fa37
Handle multiple live beacons in room share warning, test
c2de95a
un xdescribe beaconstore tests
508c07e
missed copyrights
fe689b1
i18n
bbe6e32
tidy
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
res/css/components/views/beacon/_RoomLiveShareWarning.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
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. | ||
*/ | ||
|
||
.mx_RoomLiveShareWarning { | ||
width: 100%; | ||
|
||
display: flex; | ||
flex-direction: row; | ||
align-items: center; | ||
|
||
box-sizing: border-box; | ||
padding: $spacing-12 $spacing-16; | ||
|
||
color: $primary-content; | ||
background-color: $system; | ||
} | ||
|
||
.mx_RoomLiveShareWarning_icon { | ||
height: 32px; | ||
width: 32px; | ||
margin-right: $spacing-8; | ||
} | ||
|
||
.mx_RoomLiveShareWarning_label { | ||
flex: 1; | ||
font-size: $font-15px; | ||
} | ||
|
||
.mx_RoomLiveShareWarning_expiry { | ||
color: $secondary-content; | ||
font-size: $font-12px; | ||
margin-right: $spacing-16; | ||
} | ||
|
||
.mx_RoomLiveShareWarning_spinner { | ||
margin-right: $spacing-16; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,126 @@ | ||||||||||
/* | ||||||||||
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. | ||||||||||
*/ | ||||||||||
|
||||||||||
import React, { useEffect, useState } from 'react'; | ||||||||||
import classNames from 'classnames'; | ||||||||||
import { Room } from 'matrix-js-sdk/src/matrix'; | ||||||||||
|
||||||||||
import { _t } from '../../../languageHandler'; | ||||||||||
import { useEventEmitterState } from '../../../hooks/useEventEmitter'; | ||||||||||
import { OwnBeaconStore, OwnBeaconStoreEvent } from '../../../stores/OwnBeaconStore'; | ||||||||||
import AccessibleButton from '../elements/AccessibleButton'; | ||||||||||
import StyledLiveBeaconIcon from './StyledLiveBeaconIcon'; | ||||||||||
import { formatDuration } from '../../../DateUtils'; | ||||||||||
import { getBeaconMsUntilExpiry, sortBeaconsByLatestExpiry } from '../../../utils/beacon'; | ||||||||||
import Spinner from '../elements/Spinner'; | ||||||||||
|
||||||||||
interface Props { | ||||||||||
roomId: Room['roomId']; | ||||||||||
} | ||||||||||
|
||||||||||
/** | ||||||||||
* 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; | ||||||||||
onStopSharing?: () => void; | ||||||||||
stoppingInProgress?: boolean; | ||||||||||
}; | ||||||||||
const useLiveBeacons = (roomId: Room['roomId']): LiveBeaconsState => { | ||||||||||
const [stoppingInProgress, setStoppingInProgress] = useState(false); | ||||||||||
const liveBeaconIds = useEventEmitterState( | ||||||||||
OwnBeaconStore.instance, | ||||||||||
OwnBeaconStoreEvent.LivenessChange, | ||||||||||
() => OwnBeaconStore.instance.getLiveBeaconIds(roomId), | ||||||||||
); | ||||||||||
|
||||||||||
// reset stopping in progress on change in live ids | ||||||||||
useEffect(() => { | ||||||||||
setStoppingInProgress(false); | ||||||||||
}, [liveBeaconIds, setStoppingInProgress]); | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
This shouldn't be needed given it won't ever change |
||||||||||
|
||||||||||
if (!liveBeaconIds?.length) { | ||||||||||
return { liveBeaconIds }; | ||||||||||
} | ||||||||||
|
||||||||||
// select the beacon with latest expiry to display expiry time | ||||||||||
const beacon = liveBeaconIds.map(beaconId => OwnBeaconStore.instance.getBeaconById(beaconId)) | ||||||||||
.sort(sortBeaconsByLatestExpiry) | ||||||||||
.shift(); | ||||||||||
|
||||||||||
const onStopSharing = async () => { | ||||||||||
setStoppingInProgress(true); | ||||||||||
try { | ||||||||||
await Promise.all(liveBeaconIds.map(beaconId => OwnBeaconStore.instance.stopBeacon(beaconId))); | ||||||||||
} catch (error) { | ||||||||||
// only clear loading in case of error | ||||||||||
// to avoid flash of not-loading state | ||||||||||
// after beacons have been stopped but we wait for sync | ||||||||||
setStoppingInProgress(false); | ||||||||||
} | ||||||||||
}; | ||||||||||
|
||||||||||
const msRemaining = getBeaconMsUntilExpiry(beacon); | ||||||||||
|
||||||||||
return { liveBeaconIds, onStopSharing, msRemaining, stoppingInProgress }; | ||||||||||
}; | ||||||||||
|
||||||||||
const RoomLiveShareWarning: React.FC<Props> = ({ roomId }) => { | ||||||||||
const { | ||||||||||
liveBeaconIds, | ||||||||||
onStopSharing, | ||||||||||
msRemaining, | ||||||||||
stoppingInProgress, | ||||||||||
} = useLiveBeacons(roomId); | ||||||||||
|
||||||||||
if (!liveBeaconIds?.length) { | ||||||||||
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 }) } | ||||||||||
</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> | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
} | ||||||||||
<AccessibleButton | ||||||||||
data-test-id='room-live-share-stop-sharing' | ||||||||||
onClick={onStopSharing} | ||||||||||
kind='danger' | ||||||||||
element='button' | ||||||||||
disabled={stoppingInProgress} | ||||||||||
> | ||||||||||
{ _t('Stop sharing') } | ||||||||||
</AccessibleButton> | ||||||||||
</div>; | ||||||||||
}; | ||||||||||
|
||||||||||
export default RoomLiveShareWarning; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,11 +27,12 @@ import { | |
import defaultDispatcher from "../dispatcher/dispatcher"; | ||
import { ActionPayload } from "../dispatcher/payloads"; | ||
import { AsyncStoreWithClient } from "./AsyncStoreWithClient"; | ||
import { arrayHasDiff } from "../utils/arrays"; | ||
|
||
const isOwnBeacon = (beacon: Beacon, userId: string): boolean => beacon.beaconInfoOwner === userId; | ||
|
||
export enum OwnBeaconStoreEvent { | ||
LivenessChange = 'OwnBeaconStore.LivenessChange' | ||
LivenessChange = 'OwnBeaconStore.LivenessChange', | ||
} | ||
|
||
type OwnBeaconStoreState = { | ||
|
@@ -41,6 +42,7 @@ type OwnBeaconStoreState = { | |
}; | ||
export class OwnBeaconStore extends AsyncStoreWithClient<OwnBeaconStoreState> { | ||
private static internalInstance = new OwnBeaconStore(); | ||
// users beacons, keyed by event type | ||
public readonly beacons = new Map<string, Beacon>(); | ||
public readonly beaconsByRoomId = new Map<Room['roomId'], Set<string>>(); | ||
private liveBeaconIds = []; | ||
|
@@ -86,8 +88,12 @@ export class OwnBeaconStore extends AsyncStoreWithClient<OwnBeaconStoreState> { | |
return this.liveBeaconIds.filter(beaconId => this.beaconsByRoomId.get(roomId)?.has(beaconId)); | ||
} | ||
|
||
public stopBeacon = async (beaconInfoId: string): Promise<void> => { | ||
const beacon = this.beacons.get(beaconInfoId); | ||
public getBeaconById(beaconId: string): Beacon | undefined { | ||
return this.beacons.get(beaconId); | ||
} | ||
|
||
public stopBeacon = async (beaconInfoType: string): Promise<void> => { | ||
const beacon = this.beacons.get(beaconInfoType); | ||
// if no beacon, or beacon is already explicitly set isLive: false | ||
// do nothing | ||
if (!beacon?.beaconInfo?.live) { | ||
|
@@ -107,27 +113,27 @@ export class OwnBeaconStore extends AsyncStoreWithClient<OwnBeaconStoreState> { | |
|
||
private onBeaconLiveness = (isLive: boolean, beacon: Beacon): void => { | ||
// check if we care about this beacon | ||
if (!this.beacons.has(beacon.beaconInfoId)) { | ||
if (!this.beacons.has(beacon.identifier)) { | ||
return; | ||
} | ||
|
||
if (!isLive && this.liveBeaconIds.includes(beacon.beaconInfoId)) { | ||
if (!isLive && this.liveBeaconIds.includes(beacon.identifier)) { | ||
this.liveBeaconIds = | ||
this.liveBeaconIds.filter(beaconId => beaconId !== beacon.beaconInfoId); | ||
this.liveBeaconIds.filter(beaconId => beaconId !== beacon.identifier); | ||
} | ||
|
||
if (isLive && !this.liveBeaconIds.includes(beacon.beaconInfoId)) { | ||
this.liveBeaconIds.push(beacon.beaconInfoId); | ||
if (isLive && !this.liveBeaconIds.includes(beacon.identifier)) { | ||
this.liveBeaconIds.push(beacon.identifier); | ||
} | ||
|
||
// beacon expired, update beacon to un-alive state | ||
if (!isLive) { | ||
this.stopBeacon(beacon.beaconInfoId); | ||
this.stopBeacon(beacon.identifier); | ||
} | ||
|
||
// TODO start location polling here | ||
|
||
this.emit(OwnBeaconStoreEvent.LivenessChange, this.hasLiveBeacons()); | ||
this.emit(OwnBeaconStoreEvent.LivenessChange, this.getLiveBeaconIds()); | ||
}; | ||
|
||
private initialiseBeaconState = () => { | ||
|
@@ -146,27 +152,25 @@ export class OwnBeaconStore extends AsyncStoreWithClient<OwnBeaconStoreState> { | |
}; | ||
|
||
private addBeacon = (beacon: Beacon): void => { | ||
this.beacons.set(beacon.beaconInfoId, beacon); | ||
this.beacons.set(beacon.identifier, beacon); | ||
|
||
if (!this.beaconsByRoomId.has(beacon.roomId)) { | ||
this.beaconsByRoomId.set(beacon.roomId, new Set<string>()); | ||
} | ||
|
||
this.beaconsByRoomId.get(beacon.roomId).add(beacon.beaconInfoId); | ||
this.beaconsByRoomId.get(beacon.roomId).add(beacon.identifier); | ||
|
||
beacon.monitorLiveness(); | ||
}; | ||
|
||
private checkLiveness = (): void => { | ||
const prevLiveness = this.hasLiveBeacons(); | ||
const prevLiveness = this.getLiveBeaconIds(); | ||
this.liveBeaconIds = [...this.beacons.values()] | ||
.filter(beacon => beacon.isLive) | ||
.map(beacon => beacon.beaconInfoId); | ||
|
||
const newLiveness = this.hasLiveBeacons(); | ||
.map(beacon => beacon.identifier); | ||
|
||
if (prevLiveness !== newLiveness) { | ||
this.emit(OwnBeaconStoreEvent.LivenessChange, newLiveness); | ||
if (arrayHasDiff(prevLiveness, this.liveBeaconIds)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. prevLiveness is no longer an appropriate identifier, it implies a boolean type. |
||
this.emit(OwnBeaconStoreEvent.LivenessChange, this.liveBeaconIds); | ||
} | ||
}; | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.