2022-03-01 18:00:07 +01: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-03-02 14:00:40 +01:00
|
|
|
import React, { SyntheticEvent, useContext, useState } from 'react';
|
2022-03-01 16:51:05 -07:00
|
|
|
import { Room } from 'matrix-js-sdk/src/models/room';
|
2022-03-02 14:27:16 +00:00
|
|
|
import { IEventRelation } from 'matrix-js-sdk/src/models/event';
|
2022-03-01 18:00:07 +01:00
|
|
|
|
|
|
|
|
import MatrixClientContext from '../../../contexts/MatrixClientContext';
|
|
|
|
|
import ContextMenu, { AboveLeftOf } from '../../structures/ContextMenu';
|
|
|
|
|
import LocationPicker, { ILocationPickerProps } from "./LocationPicker";
|
2022-03-18 10:52:24 +01:00
|
|
|
import { shareLiveLocation, shareLocation } from './shareLocation';
|
2022-03-02 14:00:40 +01:00
|
|
|
import SettingsStore from '../../../settings/SettingsStore';
|
2022-03-03 11:30:46 +01:00
|
|
|
import ShareDialogButtons from './ShareDialogButtons';
|
2022-03-09 18:14:07 +01:00
|
|
|
import ShareType from './ShareType';
|
|
|
|
|
import { LocationShareType } from './shareLocation';
|
2022-03-18 10:52:24 +01:00
|
|
|
import { OwnProfileStore } from '../../../stores/OwnProfileStore';
|
2022-03-01 18:00:07 +01:00
|
|
|
|
2022-03-09 18:14:07 +01:00
|
|
|
type Props = Omit<ILocationPickerProps, 'onChoose' | 'shareType'> & {
|
2022-03-01 18:00:07 +01:00
|
|
|
onFinished: (ev?: SyntheticEvent) => void;
|
|
|
|
|
menuPosition: AboveLeftOf;
|
|
|
|
|
openMenu: () => void;
|
|
|
|
|
roomId: Room["roomId"];
|
2022-03-02 14:27:16 +00:00
|
|
|
relation?: IEventRelation;
|
2022-03-01 18:00:07 +01:00
|
|
|
};
|
|
|
|
|
|
2022-03-02 14:00:40 +01:00
|
|
|
const getEnabledShareTypes = (): LocationShareType[] => {
|
2022-03-15 15:04:52 +00:00
|
|
|
const enabledShareTypes = [LocationShareType.Own];
|
2022-03-02 14:00:40 +01:00
|
|
|
|
2022-03-15 15:04:52 +00:00
|
|
|
if (SettingsStore.getValue("feature_location_share_live")) {
|
|
|
|
|
enabledShareTypes.push(LocationShareType.Live);
|
2022-03-02 14:00:40 +01:00
|
|
|
}
|
2022-03-15 15:04:52 +00:00
|
|
|
|
|
|
|
|
if (SettingsStore.getValue("feature_location_share_pin_drop")) {
|
|
|
|
|
enabledShareTypes.push(LocationShareType.Pin);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return enabledShareTypes;
|
2022-03-02 14:00:40 +01:00
|
|
|
};
|
|
|
|
|
|
2022-03-01 18:00:07 +01:00
|
|
|
const LocationShareMenu: React.FC<Props> = ({
|
2022-03-02 14:27:16 +00:00
|
|
|
menuPosition,
|
|
|
|
|
onFinished,
|
|
|
|
|
sender,
|
|
|
|
|
roomId,
|
|
|
|
|
openMenu,
|
|
|
|
|
relation,
|
2022-03-01 18:00:07 +01:00
|
|
|
}) => {
|
|
|
|
|
const matrixClient = useContext(MatrixClientContext);
|
2022-03-02 14:00:40 +01:00
|
|
|
const enabledShareTypes = getEnabledShareTypes();
|
|
|
|
|
|
2022-03-03 11:30:46 +01:00
|
|
|
const multipleShareTypesEnabled = enabledShareTypes.length > 1;
|
|
|
|
|
|
|
|
|
|
const [shareType, setShareType] = useState<LocationShareType | undefined>(
|
|
|
|
|
multipleShareTypesEnabled ? undefined : LocationShareType.Own,
|
2022-03-02 14:00:40 +01:00
|
|
|
);
|
2022-03-01 18:00:07 +01:00
|
|
|
|
2022-03-18 10:52:24 +01:00
|
|
|
const displayName = OwnProfileStore.instance.displayName;
|
|
|
|
|
|
|
|
|
|
const onLocationSubmit = shareType === LocationShareType.Live ?
|
|
|
|
|
shareLiveLocation(matrixClient, roomId, displayName, openMenu) :
|
|
|
|
|
shareLocation(matrixClient, roomId, shareType, relation, openMenu);
|
|
|
|
|
|
2022-03-01 18:00:07 +01:00
|
|
|
return <ContextMenu
|
|
|
|
|
{...menuPosition}
|
|
|
|
|
onFinished={onFinished}
|
|
|
|
|
managed={false}
|
|
|
|
|
>
|
|
|
|
|
<div className="mx_LocationShareMenu">
|
2022-03-18 10:52:24 +01:00
|
|
|
{ shareType ?
|
|
|
|
|
<LocationPicker
|
|
|
|
|
sender={sender}
|
|
|
|
|
shareType={shareType}
|
|
|
|
|
onChoose={onLocationSubmit}
|
|
|
|
|
onFinished={onFinished}
|
|
|
|
|
/> :
|
|
|
|
|
<ShareType setShareType={setShareType} enabledShareTypes={enabledShareTypes} />
|
|
|
|
|
}
|
2022-03-03 11:30:46 +01:00
|
|
|
<ShareDialogButtons displayBack={!!shareType && multipleShareTypesEnabled} onBack={() => setShareType(undefined)} onCancel={onFinished} />
|
2022-03-01 18:00:07 +01:00
|
|
|
</div>
|
|
|
|
|
</ContextMenu>;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default LocationShareMenu;
|