2022-04-20 13:57:50 +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, { HTMLProps, useContext } from "react";
|
2023-08-15 16:00:17 +01:00
|
|
|
import { Beacon, BeaconEvent, LocationAssetType } from "matrix-js-sdk/src/matrix";
|
2022-04-20 13:57:50 +02:00
|
|
|
|
|
|
|
|
import MatrixClientContext from "../../../contexts/MatrixClientContext";
|
|
|
|
|
import { useEventEmitterState } from "../../../hooks/useEventEmitter";
|
|
|
|
|
import { humanizeTime } from "../../../utils/humanize";
|
2022-07-18 10:34:39 +02:00
|
|
|
import { preventDefaultWrapper } from "../../../utils/NativeEventUtils";
|
2022-04-20 13:57:50 +02:00
|
|
|
import { _t } from "../../../languageHandler";
|
|
|
|
|
import MemberAvatar from "../avatars/MemberAvatar";
|
|
|
|
|
import BeaconStatus from "./BeaconStatus";
|
|
|
|
|
import { BeaconDisplayStatus } from "./displayStatus";
|
|
|
|
|
import StyledLiveBeaconIcon from "./StyledLiveBeaconIcon";
|
2022-05-27 11:58:39 +02:00
|
|
|
import ShareLatestLocation from "./ShareLatestLocation";
|
2022-04-20 13:57:50 +02:00
|
|
|
|
|
|
|
|
interface Props {
|
|
|
|
|
beacon: Beacon;
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-18 10:34:39 +02:00
|
|
|
const BeaconListItem: React.FC<Props & HTMLProps<HTMLLIElement>> = ({ beacon, ...rest }) => {
|
2022-04-20 13:57:50 +02:00
|
|
|
const latestLocationState = useEventEmitterState(
|
|
|
|
|
beacon,
|
|
|
|
|
BeaconEvent.LocationUpdate,
|
|
|
|
|
() => beacon.latestLocationState,
|
|
|
|
|
);
|
|
|
|
|
const matrixClient = useContext(MatrixClientContext);
|
|
|
|
|
const room = matrixClient.getRoom(beacon.roomId);
|
|
|
|
|
|
2023-03-02 22:58:05 +13:00
|
|
|
if (!latestLocationState || !beacon.isLive || !room) {
|
2022-04-20 13:57:50 +02:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-02 22:58:05 +13:00
|
|
|
const isSelfLocation = beacon.beaconInfo?.assetType === LocationAssetType.Self;
|
|
|
|
|
const beaconMember = isSelfLocation ? room.getMember(beacon.beaconInfoOwner) : null;
|
2022-04-20 13:57:50 +02:00
|
|
|
|
2023-03-02 22:58:05 +13:00
|
|
|
const humanizedUpdateTime = (latestLocationState.timestamp && humanizeTime(latestLocationState.timestamp)) || "";
|
2022-04-20 13:57:50 +02:00
|
|
|
|
2022-07-18 10:34:39 +02:00
|
|
|
return (
|
|
|
|
|
<li className="mx_BeaconListItem" {...rest}>
|
2022-04-20 13:57:50 +02:00
|
|
|
{isSelfLocation ? (
|
2023-08-24 04:48:35 +01:00
|
|
|
<MemberAvatar className="mx_BeaconListItem_avatar" member={beaconMember} size="32px" />
|
2022-04-20 13:57:50 +02:00
|
|
|
) : (
|
|
|
|
|
<StyledLiveBeaconIcon className="mx_BeaconListItem_avatarIcon" />
|
|
|
|
|
)}
|
|
|
|
|
<div className="mx_BeaconListItem_info">
|
|
|
|
|
<BeaconStatus
|
|
|
|
|
className="mx_BeaconListItem_status"
|
|
|
|
|
beacon={beacon}
|
2023-03-02 22:58:05 +13:00
|
|
|
label={beaconMember?.name || beacon.beaconInfo?.description || beacon.beaconInfoOwner}
|
2022-04-20 13:57:50 +02:00
|
|
|
displayStatus={BeaconDisplayStatus.Active}
|
|
|
|
|
>
|
2022-07-18 10:34:39 +02:00
|
|
|
{/* eat events from interactive share buttons
|
|
|
|
|
so parent click handlers are not triggered */}
|
|
|
|
|
<div className="mx_BeaconListItem_interactions" onClick={preventDefaultWrapper(() => {})}>
|
|
|
|
|
<ShareLatestLocation latestLocationState={latestLocationState} />
|
|
|
|
|
</div>
|
2022-04-20 13:57:50 +02:00
|
|
|
</BeaconStatus>
|
|
|
|
|
<span className="mx_BeaconListItem_lastUpdated">
|
|
|
|
|
{_t("Updated %(humanizedUpdateTime)s", { humanizedUpdateTime })}
|
|
|
|
|
</span>
|
|
|
|
|
</div>
|
|
|
|
|
</li>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default BeaconListItem;
|