Show an error dialog if we fail to send location (#7528)

This commit is contained in:
Andy Balaam 2022-01-13 13:23:00 +00:00 committed by GitHub
parent 44b9b6ca57
commit 25cd1a8a43
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 49 additions and 23 deletions

View file

@ -14,26 +14,33 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
import React, { ReactElement } from 'react'; import React, { ReactElement, useContext } from 'react';
import classNames from 'classnames'; import classNames from 'classnames';
import { RoomMember } from 'matrix-js-sdk/src/models/room-member'; import { RoomMember } from 'matrix-js-sdk/src/models/room-member';
import { logger } from "matrix-js-sdk/src/logger";
import { MatrixClient } from 'matrix-js-sdk/src/client';
import { makeLocationContent } from "matrix-js-sdk/src/content-helpers";
import { _t } from '../../../languageHandler'; import { _t } from '../../../languageHandler';
import LocationPicker from './LocationPicker'; import LocationPicker from './LocationPicker';
import { CollapsibleButton, ICollapsibleButtonProps } from '../rooms/CollapsibleButton'; import { CollapsibleButton, ICollapsibleButtonProps } from '../rooms/CollapsibleButton';
import ContextMenu, { aboveLeftOf, useContextMenu, AboveLeftOf } from "../../structures/ContextMenu"; import ContextMenu, { aboveLeftOf, useContextMenu, AboveLeftOf } from "../../structures/ContextMenu";
import Modal from '../../../Modal';
import QuestionDialog from '../dialogs/QuestionDialog';
import MatrixClientContext from '../../../contexts/MatrixClientContext';
interface IProps extends Pick<ICollapsibleButtonProps, "narrowMode"> { interface IProps extends Pick<ICollapsibleButtonProps, "narrowMode"> {
roomId: string;
sender: RoomMember; sender: RoomMember;
shareLocation: (uri: string, ts: number) => boolean;
menuPosition: AboveLeftOf; menuPosition: AboveLeftOf;
narrowMode: boolean; narrowMode: boolean;
} }
export const LocationButton: React.FC<IProps> = ( export const LocationButton: React.FC<IProps> = (
{ sender, shareLocation, menuPosition, narrowMode }, { roomId, sender, menuPosition, narrowMode },
) => { ) => {
const [menuDisplayed, button, openMenu, closeMenu] = useContextMenu(); const [menuDisplayed, button, openMenu, closeMenu] = useContextMenu();
const matrixClient = useContext(MatrixClientContext);
let contextMenu: ReactElement; let contextMenu: ReactElement;
if (menuDisplayed) { if (menuDisplayed) {
@ -47,7 +54,7 @@ export const LocationButton: React.FC<IProps> = (
> >
<LocationPicker <LocationPicker
sender={sender} sender={sender}
onChoose={shareLocation} onChoose={shareLocation(matrixClient, roomId, openMenu)}
onFinished={closeMenu} onFinished={closeMenu}
/> />
</ContextMenu>; </ContextMenu>;
@ -75,6 +82,36 @@ export const LocationButton: React.FC<IProps> = (
</React.Fragment>; </React.Fragment>;
}; };
const shareLocation = (client: MatrixClient, roomId: string, openMenu: () => void) =>
(uri: string, ts: number) => {
if (!uri) return false;
try {
const text = textForLocation(uri, ts, null);
client.sendMessage(
roomId,
makeLocationContent(text, uri, ts, null),
);
} catch (e) {
logger.error("We couldnt send your location", e);
const analyticsAction = 'We couldnt send your location';
const params = {
title: _t("We couldnt send your location"),
description: _t(
"Element could not send your location. Please try again later."),
button: _t('Try again'),
cancelButton: _t('Cancel'),
onFinished: (tryAgain: boolean) => {
if (tryAgain) {
openMenu();
}
},
};
Modal.createTrackedDialog(analyticsAction, '', QuestionDialog, params);
}
return true;
};
export function textForLocation( export function textForLocation(
uri: string, uri: string,
ts: number, ts: number,

View file

@ -19,9 +19,7 @@ import { MatrixEvent, IEventRelation } from "matrix-js-sdk/src/models/event";
import { Room } from "matrix-js-sdk/src/models/room"; import { Room } from "matrix-js-sdk/src/models/room";
import { RoomMember } from "matrix-js-sdk/src/models/room-member"; import { RoomMember } from "matrix-js-sdk/src/models/room-member";
import { RelationType } from 'matrix-js-sdk/src/@types/event'; import { RelationType } from 'matrix-js-sdk/src/@types/event';
import { logger } from "matrix-js-sdk/src/logger";
import { POLL_START_EVENT_TYPE } from "matrix-js-sdk/src/@types/polls"; import { POLL_START_EVENT_TYPE } from "matrix-js-sdk/src/@types/polls";
import { makeLocationContent } from "matrix-js-sdk/src/content-helpers";
import { _t } from '../../../languageHandler'; import { _t } from '../../../languageHandler';
import { MatrixClientPeg } from '../../../MatrixClientPeg'; import { MatrixClientPeg } from '../../../MatrixClientPeg';
@ -60,7 +58,7 @@ import ErrorDialog from "../dialogs/ErrorDialog";
import PollCreateDialog from "../elements/PollCreateDialog"; import PollCreateDialog from "../elements/PollCreateDialog";
import { SettingUpdatedPayload } from "../../../dispatcher/payloads/SettingUpdatedPayload"; import { SettingUpdatedPayload } from "../../../dispatcher/payloads/SettingUpdatedPayload";
import { CollapsibleButton, ICollapsibleButtonProps } from './CollapsibleButton'; import { CollapsibleButton, ICollapsibleButtonProps } from './CollapsibleButton';
import { LocationButton, textForLocation } from '../location/LocationButton'; import LocationButton from '../location/LocationButton';
let instanceCount = 0; let instanceCount = 0;
const NARROW_MODE_BREAKPOINT = 500; const NARROW_MODE_BREAKPOINT = 500;
@ -452,20 +450,6 @@ export default class MessageComposer extends React.Component<IProps, IState> {
return true; return true;
}; };
private shareLocation = (uri: string, ts: number): boolean => {
if (!uri) return false;
try {
const text = textForLocation(uri, ts, null);
MatrixClientPeg.get().sendMessage(
this.props.room.roomId,
makeLocationContent(text, uri, ts, null),
);
} catch (e) {
logger.error("Error sending location:", e);
}
return true;
};
private sendMessage = async () => { private sendMessage = async () => {
if (this.state.haveRecording && this.voiceRecordingButton.current) { if (this.state.haveRecording && this.voiceRecordingButton.current) {
// There shouldn't be any text message to send when a voice recording is active, so // There shouldn't be any text message to send when a voice recording is active, so
@ -530,11 +514,14 @@ export default class MessageComposer extends React.Component<IProps, IState> {
<UploadButton key="controls_upload" roomId={this.props.room.roomId} relation={this.props.relation} />, <UploadButton key="controls_upload" roomId={this.props.room.roomId} relation={this.props.relation} />,
); );
if (SettingsStore.getValue("feature_location_share")) { if (SettingsStore.getValue("feature_location_share")) {
const sender = this.props.room.getMember(
MatrixClientPeg.get().getUserId(),
);
buttons.push( buttons.push(
<LocationButton <LocationButton
sender={this.props.room.getMember(MatrixClientPeg.get().getUserId())}
key="location" key="location"
shareLocation={this.shareLocation} roomId={this.props.room.roomId}
sender={sender}
menuPosition={menuPosition} menuPosition={menuPosition}
narrowMode={this.state.narrowMode} narrowMode={this.state.narrowMode}
/>, />,

View file

@ -2135,6 +2135,8 @@
"Can't load this message": "Can't load this message", "Can't load this message": "Can't load this message",
"toggle event": "toggle event", "toggle event": "toggle event",
"Share location": "Share location", "Share location": "Share location",
"We couldnt send your location": "We couldnt send your location",
"Element could not send your location. Please try again later.": "Element could not send your location. Please try again later.",
"Failed to load group members": "Failed to load group members", "Failed to load group members": "Failed to load group members",
"Filter community members": "Filter community members", "Filter community members": "Filter community members",
"Are you sure you want to remove '%(roomName)s' from %(groupId)s?": "Are you sure you want to remove '%(roomName)s' from %(groupId)s?", "Are you sure you want to remove '%(roomName)s' from %(groupId)s?": "Are you sure you want to remove '%(roomName)s' from %(groupId)s?",