2018-06-12 11:11:43 +01:00
|
|
|
/*
|
2018-06-13 18:46:02 +01:00
|
|
|
Copyright 2018 New Vector Ltd
|
2020-04-15 00:22:19 +01:00
|
|
|
Copyright 2020 The Matrix.org Foundation C.I.C.
|
2018-06-12 11:11:43 +01:00
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
*/
|
|
|
|
|
|
2020-04-15 00:22:19 +01:00
|
|
|
import * as React from "react";
|
2023-08-08 08:16:04 +01:00
|
|
|
import { Room, RoomMember, MatrixEvent, User } from "matrix-js-sdk/src/matrix";
|
2021-10-22 17:23:32 -05:00
|
|
|
|
2018-06-12 11:11:43 +01:00
|
|
|
import { _t } from "../../../languageHandler";
|
2020-04-11 23:46:58 +01:00
|
|
|
import QRCode from "../elements/QRCode";
|
2022-03-22 17:07:37 -06:00
|
|
|
import { RoomPermalinkCreator, makeUserPermalink } from "../../../utils/permalinks/Permalinks";
|
2022-02-02 12:16:00 +00:00
|
|
|
import { selectText } from "../../../utils/strings";
|
2020-05-28 22:33:00 +01:00
|
|
|
import StyledCheckbox from "../elements/StyledCheckbox";
|
2020-09-16 16:06:17 +01:00
|
|
|
import SettingsStore from "../../../settings/SettingsStore";
|
2021-06-29 13:11:58 +01:00
|
|
|
import { UIFeature } from "../../../settings/UIFeature";
|
2021-07-03 10:44:03 +02:00
|
|
|
import BaseDialog from "./BaseDialog";
|
2022-02-02 12:16:00 +00:00
|
|
|
import CopyableText from "../elements/CopyableText";
|
2023-04-28 09:45:36 +01:00
|
|
|
import { XOR } from "../../../@types/common";
|
2018-06-12 11:11:43 +01:00
|
|
|
|
|
|
|
|
const socials = [
|
|
|
|
|
{
|
|
|
|
|
name: "Facebook",
|
2019-01-10 19:37:28 -06:00
|
|
|
img: require("../../../../res/img/social/facebook.png"),
|
2023-02-13 11:39:16 +00:00
|
|
|
url: (url: String) => `https://www.facebook.com/sharer/sharer.php?u=${url}`,
|
2018-06-12 11:11:43 +01:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "Twitter",
|
2019-01-10 19:37:28 -06:00
|
|
|
img: require("../../../../res/img/social/twitter-2.png"),
|
2023-02-13 11:39:16 +00:00
|
|
|
url: (url: string) => `https://twitter.com/home?status=${url}`,
|
2018-06-12 11:11:43 +01:00
|
|
|
},
|
|
|
|
|
/* // icon missing
|
|
|
|
|
name: 'Google Plus',
|
|
|
|
|
img: 'img/social/',
|
|
|
|
|
url: (url) => `https://plus.google.com/share?url=${url}`,
|
|
|
|
|
},*/ {
|
2018-06-13 18:46:02 +01:00
|
|
|
name: "LinkedIn",
|
2019-01-10 19:37:28 -06:00
|
|
|
img: require("../../../../res/img/social/linkedin.png"),
|
2023-02-13 11:39:16 +00:00
|
|
|
url: (url: string) => `https://www.linkedin.com/shareArticle?mini=true&url=${url}`,
|
2018-06-12 11:11:43 +01:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "Reddit",
|
2019-01-10 19:37:28 -06:00
|
|
|
img: require("../../../../res/img/social/reddit.png"),
|
2023-02-13 11:39:16 +00:00
|
|
|
url: (url: string) => `https://www.reddit.com/submit?url=${url}`,
|
2018-06-12 11:11:43 +01:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "email",
|
2019-01-10 19:37:28 -06:00
|
|
|
img: require("../../../../res/img/social/email-1.png"),
|
2023-02-13 11:39:16 +00:00
|
|
|
url: (url: string) => `mailto:?body=${url}`,
|
2018-06-12 11:11:43 +01:00
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
|
2023-04-28 09:45:36 +01:00
|
|
|
interface BaseProps {
|
2024-03-07 16:38:53 +01:00
|
|
|
/**
|
|
|
|
|
* A function that is called when the dialog is dismissed
|
|
|
|
|
*/
|
2023-02-28 10:31:48 +00:00
|
|
|
onFinished(): void;
|
2024-03-07 16:38:53 +01:00
|
|
|
/**
|
|
|
|
|
* An optional string to use as the dialog title.
|
|
|
|
|
* If not provided, an appropriate title for the target type will be used.
|
|
|
|
|
*/
|
|
|
|
|
customTitle?: string;
|
|
|
|
|
/**
|
|
|
|
|
* An optional string to use as the dialog subtitle
|
|
|
|
|
*/
|
|
|
|
|
subtitle?: string;
|
2020-04-15 00:22:19 +01:00
|
|
|
}
|
|
|
|
|
|
2023-04-28 09:45:36 +01:00
|
|
|
interface Props extends BaseProps {
|
2024-03-07 16:38:53 +01:00
|
|
|
/**
|
|
|
|
|
* The target to link to.
|
|
|
|
|
* This can be a Room, User, RoomMember, or MatrixEvent or an already computed URL.
|
|
|
|
|
* A <u>matrix.to</u> link will be generated out of it if it's not already a url.
|
|
|
|
|
*/
|
|
|
|
|
target: Room | User | RoomMember | URL;
|
2023-04-28 09:45:36 +01:00
|
|
|
permalinkCreator?: RoomPermalinkCreator;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface EventProps extends BaseProps {
|
|
|
|
|
target: MatrixEvent;
|
|
|
|
|
permalinkCreator: RoomPermalinkCreator;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-15 00:22:19 +01:00
|
|
|
interface IState {
|
|
|
|
|
linkSpecificEvent: boolean;
|
2023-02-13 17:01:43 +00:00
|
|
|
permalinkCreator: RoomPermalinkCreator | null;
|
2020-04-15 00:22:19 +01:00
|
|
|
}
|
|
|
|
|
|
2023-04-28 09:45:36 +01:00
|
|
|
export default class ShareDialog extends React.PureComponent<XOR<Props, EventProps>, IState> {
|
|
|
|
|
public constructor(props: XOR<Props, EventProps>) {
|
2018-06-12 11:11:43 +01:00
|
|
|
super(props);
|
|
|
|
|
|
2023-02-13 17:01:43 +00:00
|
|
|
let permalinkCreator: RoomPermalinkCreator | null = null;
|
2020-04-07 15:22:38 -06:00
|
|
|
if (props.target instanceof Room) {
|
|
|
|
|
permalinkCreator = new RoomPermalinkCreator(props.target);
|
|
|
|
|
permalinkCreator.load();
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-12 11:11:43 +01:00
|
|
|
this.state = {
|
2018-06-14 13:41:12 +01:00
|
|
|
// MatrixEvent defaults to share linkSpecificEvent
|
|
|
|
|
linkSpecificEvent: this.props.target instanceof MatrixEvent,
|
2020-04-07 15:22:38 -06:00
|
|
|
permalinkCreator,
|
2018-06-12 11:11:43 +01:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-13 11:39:16 +00:00
|
|
|
public static onLinkClick(e: React.MouseEvent): void {
|
2018-06-12 11:11:43 +01:00
|
|
|
e.preventDefault();
|
2023-02-13 11:39:16 +00:00
|
|
|
selectText(e.currentTarget);
|
2018-06-12 11:11:43 +01:00
|
|
|
}
|
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
private onLinkSpecificEventCheckboxClick = (): void => {
|
2018-06-12 11:11:43 +01:00
|
|
|
this.setState({
|
2018-06-14 13:41:12 +01:00
|
|
|
linkSpecificEvent: !this.state.linkSpecificEvent,
|
2018-06-12 11:11:43 +01:00
|
|
|
});
|
2022-02-02 12:16:00 +00:00
|
|
|
};
|
2018-06-12 11:11:43 +01:00
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
private getUrl(): string {
|
2024-03-07 16:38:53 +01:00
|
|
|
if (this.props.target instanceof URL) {
|
|
|
|
|
return this.props.target.toString();
|
|
|
|
|
} else if (this.props.target instanceof Room) {
|
2020-04-15 00:16:11 +01:00
|
|
|
if (this.state.linkSpecificEvent) {
|
|
|
|
|
const events = this.props.target.getLiveTimeline().getEvents();
|
2023-04-28 09:45:36 +01:00
|
|
|
return this.state.permalinkCreator!.forEvent(events[events.length - 1].getId()!);
|
2020-04-15 00:16:11 +01:00
|
|
|
} else {
|
2023-04-28 09:45:36 +01:00
|
|
|
return this.state.permalinkCreator!.forShareableRoom();
|
2020-04-15 00:16:11 +01:00
|
|
|
}
|
|
|
|
|
} else if (this.props.target instanceof User || this.props.target instanceof RoomMember) {
|
2023-04-28 09:45:36 +01:00
|
|
|
return makeUserPermalink(this.props.target.userId);
|
|
|
|
|
} else if (this.state.linkSpecificEvent) {
|
|
|
|
|
return this.props.permalinkCreator!.forEvent(this.props.target.getId()!);
|
|
|
|
|
} else {
|
|
|
|
|
return this.props.permalinkCreator!.forShareableRoom();
|
2020-04-15 00:16:11 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-13 17:01:43 +00:00
|
|
|
public render(): React.ReactNode {
|
2023-04-28 09:45:36 +01:00
|
|
|
let title: string | undefined;
|
|
|
|
|
let checkbox: JSX.Element | undefined;
|
2018-06-12 11:11:43 +01:00
|
|
|
|
2024-03-07 16:38:53 +01:00
|
|
|
if (this.props.target instanceof URL) {
|
|
|
|
|
title = this.props.customTitle ?? _t("share|title_link");
|
|
|
|
|
} else if (this.props.target instanceof Room) {
|
|
|
|
|
title = this.props.customTitle ?? _t("share|title_room");
|
2018-06-12 11:11:43 +01:00
|
|
|
|
|
|
|
|
const events = this.props.target.getLiveTimeline().getEvents();
|
|
|
|
|
if (events.length > 0) {
|
|
|
|
|
checkbox = (
|
|
|
|
|
<div>
|
2020-05-28 22:33:00 +01:00
|
|
|
<StyledCheckbox
|
|
|
|
|
checked={this.state.linkSpecificEvent}
|
|
|
|
|
onChange={this.onLinkSpecificEventCheckboxClick}
|
2022-12-12 12:24:14 +01:00
|
|
|
>
|
2023-10-03 19:17:26 +01:00
|
|
|
{_t("share|permalink_most_recent")}
|
2020-05-28 22:33:00 +01:00
|
|
|
</StyledCheckbox>
|
2022-12-12 12:24:14 +01:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
2018-06-12 11:11:43 +01:00
|
|
|
} else if (this.props.target instanceof User || this.props.target instanceof RoomMember) {
|
2024-03-07 16:38:53 +01:00
|
|
|
title = this.props.customTitle ?? _t("share|title_user");
|
2018-06-14 13:35:35 +01:00
|
|
|
} else if (this.props.target instanceof MatrixEvent) {
|
2024-03-07 16:38:53 +01:00
|
|
|
title = this.props.customTitle ?? _t("share|title_message");
|
2018-06-14 13:41:12 +01:00
|
|
|
checkbox = (
|
2022-12-12 12:24:14 +01:00
|
|
|
<div>
|
2020-05-28 22:33:00 +01:00
|
|
|
<StyledCheckbox
|
|
|
|
|
checked={this.state.linkSpecificEvent}
|
|
|
|
|
onChange={this.onLinkSpecificEventCheckboxClick}
|
|
|
|
|
>
|
2023-10-03 19:17:26 +01:00
|
|
|
{_t("share|permalink_message")}
|
2020-05-28 22:33:00 +01:00
|
|
|
</StyledCheckbox>
|
2018-06-12 11:11:43 +01:00
|
|
|
</div>
|
2018-06-14 13:35:35 +01:00
|
|
|
);
|
2018-06-12 11:11:43 +01:00
|
|
|
}
|
|
|
|
|
|
2020-04-15 00:16:11 +01:00
|
|
|
const matrixToUrl = this.getUrl();
|
2018-06-12 11:11:43 +01:00
|
|
|
const encodedUrl = encodeURIComponent(matrixToUrl);
|
|
|
|
|
|
2020-09-16 16:06:17 +01:00
|
|
|
const showQrCode = SettingsStore.getValue(UIFeature.ShareQRCode);
|
|
|
|
|
const showSocials = SettingsStore.getValue(UIFeature.ShareSocial);
|
|
|
|
|
|
|
|
|
|
let qrSocialSection;
|
|
|
|
|
if (showQrCode || showSocials) {
|
|
|
|
|
qrSocialSection = (
|
|
|
|
|
<>
|
|
|
|
|
<hr />
|
|
|
|
|
<div className="mx_ShareDialog_split">
|
|
|
|
|
{showQrCode && (
|
|
|
|
|
<div className="mx_ShareDialog_qrcode_container">
|
|
|
|
|
<QRCode data={matrixToUrl} width={256} />
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
{showSocials && (
|
|
|
|
|
<div className="mx_ShareDialog_social_container">
|
|
|
|
|
{socials.map((social) => (
|
|
|
|
|
<a
|
|
|
|
|
rel="noreferrer noopener"
|
|
|
|
|
target="_blank"
|
|
|
|
|
key={social.name}
|
|
|
|
|
title={social.name}
|
|
|
|
|
href={social.url(encodedUrl)}
|
|
|
|
|
className="mx_ShareDialog_social_icon"
|
|
|
|
|
>
|
|
|
|
|
<img src={social.img} alt={social.name} height={64} width={64} />
|
|
|
|
|
</a>
|
|
|
|
|
))}
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-29 01:11:08 +01:00
|
|
|
return (
|
|
|
|
|
<BaseDialog
|
|
|
|
|
title={title}
|
|
|
|
|
className="mx_ShareDialog"
|
|
|
|
|
contentId="mx_Dialog_content"
|
|
|
|
|
onFinished={this.props.onFinished}
|
2018-06-12 11:11:43 +01:00
|
|
|
>
|
2024-03-07 16:38:53 +01:00
|
|
|
{this.props.subtitle && <p>{this.props.subtitle}</p>}
|
2018-06-12 11:11:43 +01:00
|
|
|
<div className="mx_ShareDialog_content">
|
2022-02-02 12:16:00 +00:00
|
|
|
<CopyableText getTextToCopy={() => matrixToUrl}>
|
2023-10-03 19:17:26 +01:00
|
|
|
<a title={_t("share|link_title")} href={matrixToUrl} onClick={ShareDialog.onLinkClick}>
|
2018-06-12 11:11:43 +01:00
|
|
|
{matrixToUrl}
|
|
|
|
|
</a>
|
2022-02-02 12:16:00 +00:00
|
|
|
</CopyableText>
|
2018-06-12 11:11:43 +01:00
|
|
|
{checkbox}
|
2020-09-16 16:06:17 +01:00
|
|
|
{qrSocialSection}
|
2018-06-12 11:11:43 +01:00
|
|
|
</div>
|
|
|
|
|
</BaseDialog>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|