Files
element-web/src/utils/beacon/useBeacon.ts
Kerry e59edb7101 Live location sharing - beacon in timeline happy path (#8285)
* extract location markers into generic Marker

Signed-off-by: Kerry Archibald <kerrya@element.io>

* wrap marker in smartmarker

Signed-off-by: Kerry Archibald <kerrya@element.io>

* test smartmarker

Signed-off-by: Kerry Archibald <kerrya@element.io>

* working map in location body

Signed-off-by: Kerry Archibald <kerrya@element.io>

* remove skinned sdk

Signed-off-by: Kerry Archibald <kerrya@element.io>

* use new ZoomButtons in MLocationBody

Signed-off-by: Kerry Archibald <kerrya@element.io>

* test LocationViewDialog

Signed-off-by: Kerry Archibald <kerrya@element.io>

* update commentt

Signed-off-by: Kerry Archibald <kerrya@element.io>

* lint

Signed-off-by: Kerry Archibald <kerrya@element.io>

* lint

Signed-off-by: Kerry Archibald <kerrya@element.io>

* extract livetimeremaining into own component

Signed-off-by: Kerry Archibald <kerrya@element.io>

* extract more beacon state utils

Signed-off-by: Kerry Archibald <kerrya@element.io>

* update tests for roomlivesharewarning

Signed-off-by: Kerry Archibald <kerrya@element.io>

* add idle status to live beacon icon

* add beacon map and status chin

Signed-off-by: Kerry Archibald <kerrya@element.io>

* add handling for bubbles

Signed-off-by: Kerry Archibald <kerrya@element.io>

* tests for BeaconBody

Signed-off-by: Kerry Archibald <kerrya@element.io>

* i18n

Signed-off-by: Kerry Archibald <kerrya@element.io>

* move displaystatus check up to mbeaconbody

Signed-off-by: Kerry Archibald <kerrya@element.io>

* test BeaconStatus

Signed-off-by: Kerry Archibald <kerrya@element.io>

* rename BeaconStatusChin -> BeaconStatus

Signed-off-by: Kerry Archibald <kerrya@element.io>

* make BeaconStatus generic

Signed-off-by: Kerry Archibald <kerrya@element.io>

* lint

Signed-off-by: Kerry Archibald <kerrya@element.io>

* adjust spinner size

Signed-off-by: Kerry Archibald <kerrya@element.io>

* polish and copyrights

Signed-off-by: Kerry Archibald <kerrya@element.io>

* lint

Signed-off-by: Kerry Archibald <kerrya@element.io>

* better comment

Signed-off-by: Kerry Archibald <kerrya@element.io>
2022-04-13 10:44:15 +02:00

79 lines
2.8 KiB
TypeScript

/*
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 { useContext, useEffect, useState } from "react";
import {
Beacon,
BeaconEvent,
MatrixEvent,
getBeaconInfoIdentifier,
} from "matrix-js-sdk/src/matrix";
import MatrixClientContext from "../../contexts/MatrixClientContext";
import { useEventEmitterState } from "../../hooks/useEventEmitter";
export const useBeacon = (beaconInfoEvent: MatrixEvent): Beacon | undefined => {
const matrixClient = useContext(MatrixClientContext);
const [beacon, setBeacon] = useState<Beacon>();
useEffect(() => {
const roomId = beaconInfoEvent.getRoomId();
const beaconIdentifier = getBeaconInfoIdentifier(beaconInfoEvent);
const room = matrixClient.getRoom(roomId);
const beaconInstance = room.currentState.beacons.get(beaconIdentifier);
// TODO could this be less stupid?
// Beacons are identified by their `state_key`,
// where `state_key` is always owner mxid for access control.
// Thus, only one beacon is allowed per-user per-room.
// See https://github.com/matrix-org/matrix-spec-proposals/pull/3672
// When a user creates a new beacon any previous
// beacon is replaced and should assume a 'stopped' state
// Here we check that this event is the latest beacon for this user
// If it is not the beacon instance is set to undefined.
// Retired beacons don't get a beacon instance.
if (beaconInstance?.beaconInfoId === beaconInfoEvent.getId()) {
setBeacon(beaconInstance);
} else {
setBeacon(undefined);
}
}, [beaconInfoEvent, matrixClient]);
// beacon update will fire when this beacon is superceded
// check the updated event id for equality to the matrix event
const beaconInstanceEventId = useEventEmitterState(
beacon,
BeaconEvent.Update,
() => beacon?.beaconInfoId,
);
useEffect(() => {
if (beaconInstanceEventId && beaconInstanceEventId !== beaconInfoEvent.getId()) {
setBeacon(undefined);
}
}, [beaconInstanceEventId, beaconInfoEvent]);
useEffect(() => {
if (beacon) {
beacon.monitorLiveness();
}
}, [beacon]);
return beacon;
};