Files
ThreadNet-Web/src/components/views/location/LocationShareMenu.tsx
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

95 lines
3.8 KiB
TypeScript
Raw Normal View History

2022-03-01 18:00:07 +01:00
/*
2024-09-09 14:57:16 +01:00
Copyright 2024 New Vector Ltd.
2022-03-01 18:00:07 +01:00
Copyright 2022 The Matrix.org Foundation C.I.C.
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
2024-09-09 14:57:16 +01:00
Please see LICENSE files in the repository root for full details.
2022-03-01 18:00:07 +01:00
*/
2025-02-05 13:25:06 +00:00
import React, { type SyntheticEvent, useContext, useState } from "react";
import { type Room, type IEventRelation } from "matrix-js-sdk/src/matrix";
2022-03-01 18:00:07 +01:00
import MatrixClientContext from "../../../contexts/MatrixClientContext";
2025-02-05 13:25:06 +00:00
import ContextMenu, { type MenuProps } from "../../structures/ContextMenu";
import LocationPicker, { type ILocationPickerProps } from "./LocationPicker";
import { shareLiveLocation, shareLocation, LocationShareType } 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 { OwnProfileStore } from "../../../stores/OwnProfileStore";
import { EnableLiveShare } from "./EnableLiveShare";
import { useFeatureEnabled } from "../../../hooks/useSettings";
import { SettingLevel } from "../../../settings/SettingLevel";
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: MenuProps;
2022-03-01 18:00:07 +01:00
openMenu: () => void;
roomId: Room["roomId"];
relation?: IEventRelation;
2022-03-01 18:00:07 +01:00
};
2023-02-13 11:39:16 +00:00
const getEnabledShareTypes = (relation?: IEventRelation): LocationShareType[] => {
const enabledShareTypes = [LocationShareType.Own];
// live locations cannot have a relation
// hide the option when composer has a relation
if (!relation) {
enabledShareTypes.push(LocationShareType.Live);
}
2022-03-15 15:04:52 +00:00
2022-08-08 10:57:38 +02:00
enabledShareTypes.push(LocationShareType.Pin);
2022-03-15 15:04:52 +00:00
return enabledShareTypes;
2022-03-02 14:00:40 +01:00
};
2022-03-01 18:00:07 +01:00
const LocationShareMenu: React.FC<Props> = ({ menuPosition, onFinished, sender, roomId, openMenu, relation }) => {
const matrixClient = useContext(MatrixClientContext);
const enabledShareTypes = getEnabledShareTypes(relation);
const isLiveShareEnabled = useFeatureEnabled("feature_location_share_live");
2022-03-02 14:00:40 +01:00
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
const displayName = OwnProfileStore.instance.displayName;
const userId = matrixClient.getSafeUserId();
const onLocationSubmit =
shareType === LocationShareType.Live
? shareLiveLocation(matrixClient, roomId, displayName || userId, openMenu)
: shareLocation(matrixClient, roomId, shareType ?? LocationShareType.Own, relation, openMenu);
const onLiveShareEnableSubmit = (): void => {
SettingsStore.setValue("feature_location_share_live", null, SettingLevel.DEVICE, true);
};
const shouldAdvertiseLiveLabsFlag = shareType === LocationShareType.Live && !isLiveShareEnabled;
2022-03-01 18:00:07 +01:00
return (
<ContextMenu {...menuPosition} onFinished={onFinished} managed={false}>
<div className="mx_LocationShareMenu">
{shouldAdvertiseLiveLabsFlag && <EnableLiveShare onSubmit={onLiveShareEnableSubmit} />}
{!shouldAdvertiseLiveLabsFlag && !!shareType && (
<LocationPicker
sender={sender}
shareType={shareType}
onChoose={onLocationSubmit}
onFinished={onFinished}
2022-12-12 12:24:14 +01:00
/>
)}
{!shareType && <ShareType setShareType={setShareType} enabledShareTypes={enabledShareTypes} />}
2022-03-03 11:30:46 +01:00
<ShareDialogButtons
displayBack={!!shareType && multipleShareTypesEnabled}
2022-03-03 11:30:46 +01:00
onBack={() => setShareType(undefined)}
onCancel={onFinished}
/>
2022-03-03 11:30:46 +01:00
</div>
2022-03-01 18:00:07 +01:00
</ContextMenu>
);
};
export default LocationShareMenu;