|
| 1 | +/* |
| 2 | +Copyright 2022 The Matrix.org Foundation C.I.C. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +import React, { useContext } from 'react'; |
| 18 | +import { Beacon, BeaconEvent } from 'matrix-js-sdk/src/matrix'; |
| 19 | +import { LocationAssetType } from 'matrix-js-sdk/src/@types/location'; |
| 20 | + |
| 21 | +import MatrixClientContext from '../../../contexts/MatrixClientContext'; |
| 22 | +import { useEventEmitterState } from '../../../hooks/useEventEmitter'; |
| 23 | +import { humanizeTime } from '../../../utils/humanize'; |
| 24 | +import { _t } from '../../../languageHandler'; |
| 25 | +import MemberAvatar from '../avatars/MemberAvatar'; |
| 26 | +import CopyableText from '../elements/CopyableText'; |
| 27 | +import BeaconStatus from './BeaconStatus'; |
| 28 | +import { BeaconDisplayStatus } from './displayStatus'; |
| 29 | +import StyledLiveBeaconIcon from './StyledLiveBeaconIcon'; |
| 30 | + |
| 31 | +interface Props { |
| 32 | + beacon: Beacon; |
| 33 | +} |
| 34 | + |
| 35 | +const BeaconListItem: React.FC<Props> = ({ beacon }) => { |
| 36 | + const latestLocationState = useEventEmitterState( |
| 37 | + beacon, |
| 38 | + BeaconEvent.LocationUpdate, |
| 39 | + () => beacon.latestLocationState, |
| 40 | + ); |
| 41 | + const matrixClient = useContext(MatrixClientContext); |
| 42 | + const room = matrixClient.getRoom(beacon.roomId); |
| 43 | + |
| 44 | + if (!latestLocationState || !beacon.isLive) { |
| 45 | + return null; |
| 46 | + } |
| 47 | + |
| 48 | + const isSelfLocation = beacon.beaconInfo.assetType === LocationAssetType.Self; |
| 49 | + const beaconMember = isSelfLocation ? |
| 50 | + room.getMember(beacon.beaconInfoOwner) : |
| 51 | + undefined; |
| 52 | + |
| 53 | + const humanizedUpdateTime = humanizeTime(latestLocationState.timestamp); |
| 54 | + |
| 55 | + return <li className='mx_BeaconListItem'> |
| 56 | + { isSelfLocation ? |
| 57 | + <MemberAvatar |
| 58 | + className='mx_BeaconListItem_avatar' |
| 59 | + member={beaconMember} |
| 60 | + height={32} |
| 61 | + width={32} |
| 62 | + /> : |
| 63 | + <StyledLiveBeaconIcon className='mx_BeaconListItem_avatarIcon' /> |
| 64 | + } |
| 65 | + <div className='mx_BeaconListItem_info'> |
| 66 | + <BeaconStatus |
| 67 | + className='mx_BeaconListItem_status' |
| 68 | + beacon={beacon} |
| 69 | + label={beaconMember?.name || beacon.beaconInfo.description || beacon.beaconInfoOwner} |
| 70 | + displayStatus={BeaconDisplayStatus.Active} |
| 71 | + > |
| 72 | + <CopyableText |
| 73 | + border={false} |
| 74 | + getTextToCopy={() => latestLocationState?.uri} |
| 75 | + /> |
| 76 | + </BeaconStatus> |
| 77 | + <span className='mx_BeaconListItem_lastUpdated'>{ _t("Updated %(humanizedUpdateTime)s", { humanizedUpdateTime }) }</span> |
| 78 | + </div> |
| 79 | + </li>; |
| 80 | +}; |
| 81 | + |
| 82 | +export default BeaconListItem; |
0 commit comments