|
| 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, { useEffect, useState } from 'react'; |
| 18 | +import { BeaconLocationState } from 'matrix-js-sdk/src/content-helpers'; |
| 19 | + |
| 20 | +import { Icon as ExternalLinkIcon } from '../../../../res/img/external-link.svg'; |
| 21 | +import { _t } from '../../../languageHandler'; |
| 22 | +import { makeMapSiteLink, parseGeoUri } from '../../../utils/location'; |
| 23 | +import CopyableText from '../elements/CopyableText'; |
| 24 | +import TooltipTarget from '../elements/TooltipTarget'; |
| 25 | + |
| 26 | +interface Props { |
| 27 | + latestLocationState?: BeaconLocationState; |
| 28 | +} |
| 29 | + |
| 30 | +const ShareLatestLocation: React.FC<Props> = ({ latestLocationState }) => { |
| 31 | + const [coords, setCoords] = useState(null); |
| 32 | + useEffect(() => { |
| 33 | + if (!latestLocationState) { |
| 34 | + return; |
| 35 | + } |
| 36 | + const coords = parseGeoUri(latestLocationState.uri); |
| 37 | + setCoords(coords); |
| 38 | + }, [latestLocationState]); |
| 39 | + |
| 40 | + if (!latestLocationState || !coords) { |
| 41 | + return null; |
| 42 | + } |
| 43 | + |
| 44 | + const latLonString = `${coords.latitude},${coords.longitude}`; |
| 45 | + const mapLink = makeMapSiteLink(coords); |
| 46 | + |
| 47 | + return <> |
| 48 | + <TooltipTarget label={_t('Open in OpenStreetMap')}> |
| 49 | + <a |
| 50 | + data-test-id='open-location-in-osm' |
| 51 | + href={mapLink} |
| 52 | + target='_blank' |
| 53 | + rel='noreferrer noopener' |
| 54 | + > |
| 55 | + <ExternalLinkIcon className='mx_ShareLatestLocation_icon' /> |
| 56 | + </a> |
| 57 | + </TooltipTarget> |
| 58 | + <CopyableText |
| 59 | + className='mx_ShareLatestLocation_copy' |
| 60 | + border={false} |
| 61 | + getTextToCopy={() => latLonString} |
| 62 | + /> |
| 63 | + </>; |
| 64 | +}; |
| 65 | + |
| 66 | +export default ShareLatestLocation; |
0 commit comments