Files
ThreadNet-Web/src/components/views/elements/AppPermission.tsx
T

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

167 lines
6.3 KiB
TypeScript
Raw Normal View History

2019-07-23 17:52:00 +01:00
/*
Copyright 2017 Vector Creations Ltd
Copyright 2018, 2019 New Vector Ltd
2020-07-10 19:07:11 +01:00
Copyright 2019, 2020 The Matrix.org Foundation C.I.C.
2019-07-23 17:52:00 +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.
*/
2017-07-26 11:28:43 +01:00
import React from "react";
import { RoomMember } from "matrix-js-sdk/src/matrix";
2021-10-22 17:23:32 -05:00
2017-07-28 16:39:18 +01:00
import { _t } from "../../../languageHandler";
2020-07-10 19:07:11 +01:00
import SdkConfig from "../../../SdkConfig";
2018-06-26 11:59:16 +01:00
import WidgetUtils from "../../../utils/WidgetUtils";
2021-06-29 13:11:58 +01:00
import { MatrixClientPeg } from "../../../MatrixClientPeg";
2021-07-20 12:42:21 +02:00
import MemberAvatar from "../avatars/MemberAvatar";
import BaseAvatar from "../avatars/BaseAvatar";
import Heading from "../typography/Heading";
2021-07-20 12:42:21 +02:00
import AccessibleButton from "./AccessibleButton";
import { parseUrl } from "../../../utils/UrlUtils";
import { Icon as HelpIcon } from "../../../../res/img/feather-customised/help-circle.svg";
import TooltipTarget from "./TooltipTarget";
2021-07-20 12:19:00 +02:00
interface IProps {
url: string;
creatorUserId: string;
roomId: string;
onPermissionGranted: () => void;
isRoomEncrypted?: boolean;
}
interface IState {
roomMember: RoomMember | null;
2021-07-20 12:19:00 +02:00
isWrapped: boolean;
widgetDomain: string | null;
2021-07-20 12:19:00 +02:00
}
2017-07-26 11:28:43 +01:00
2021-07-20 12:19:00 +02:00
export default class AppPermission extends React.Component<IProps, IState> {
public static defaultProps: Partial<IProps> = {
onPermissionGranted: () => {},
};
public constructor(props: IProps) {
2017-07-26 11:28:43 +01:00
super(props);
// The first step is to pick apart the widget so we can render information about it
const urlInfo = this.parseWidgetUrl();
// The second step is to find the user's profile so we can show it on the prompt
const room = MatrixClientPeg.safeGet().getRoom(this.props.roomId);
let roomMember: RoomMember | null = null;
if (room) roomMember = room.getMember(this.props.creatorUserId);
// Set all this into the initial state
this.state = {
2021-08-06 13:54:00 +02:00
roomMember,
...urlInfo,
};
2017-07-26 11:28:43 +01:00
}
private parseWidgetUrl(): { isWrapped: boolean; widgetDomain: string | null } {
const widgetUrl = parseUrl(this.props.url);
2017-07-26 16:47:58 +01:00
// HACK: We're relying on the query params when we should be relying on the widget's `data`.
// This is a workaround for Scalar.
if (WidgetUtils.isScalarUrl(this.props.url) && widgetUrl.searchParams.has("url")) {
const unwrappedUrl = parseUrl(widgetUrl.searchParams.get("url")!);
return {
widgetDomain: unwrappedUrl.host || unwrappedUrl.hostname,
isWrapped: true,
};
} else {
return {
widgetDomain: widgetUrl.host || widgetUrl.hostname,
isWrapped: false,
};
2017-07-26 11:28:43 +01:00
}
}
public render(): React.ReactNode {
2020-07-10 19:07:11 +01:00
const brand = SdkConfig.get().brand;
const displayName = this.state.roomMember ? this.state.roomMember.name : this.props.creatorUserId;
const userId = displayName === this.props.creatorUserId ? null : this.props.creatorUserId;
const avatar = this.state.roomMember ? (
<MemberAvatar member={this.state.roomMember} size="38px" />
) : (
<BaseAvatar name={this.props.creatorUserId} size="38px" />
);
const warningTooltipText = (
<div>
{_t("analytics|shared_data_heading")}
<ul>
<li>{_t("widget|shared_data_name")}</li>
<li>{_t("widget|shared_data_avatar")}</li>
<li>{_t("widget|shared_data_mxid")}</li>
<li>{_t("widget|shared_data_device_id")}</li>
<li>{_t("widget|shared_data_theme")}</li>
<li>{_t("widget|shared_data_lang")}</li>
<li>{_t("widget|shared_data_url", { brand })}</li>
<li>{_t("widget|shared_data_room_id")}</li>
<li>{_t("widget|shared_data_widget_id")}</li>
</ul>
</div>
);
const warningTooltip = (
<TooltipTarget
label={warningTooltipText}
tooltipClassName="mx_Tooltip--appPermission mx_Tooltip--appPermission--dark"
tooltipTargetClassName="mx_TextWithTooltip_target mx_TextWithTooltip_target--helpIcon"
className="mx_TextWithTooltip_tooltip"
>
<HelpIcon className="mx_Icon mx_Icon_12" />
</TooltipTarget>
);
// Due to i18n limitations, we can't dedupe the code for variables in these two messages.
const warning = this.state.isWrapped
2021-07-13 16:04:50 +01:00
? _t(
"widget|shared_data_warning_im",
2021-06-29 13:11:58 +01:00
{ widgetDomain: this.state.widgetDomain },
{ helpIcon: () => warningTooltip },
)
: _t(
"widget|shared_data_warning",
2021-06-29 13:11:58 +01:00
{ widgetDomain: this.state.widgetDomain },
{ helpIcon: () => warningTooltip },
);
const encryptionWarning = this.props.isRoomEncrypted ? _t("widget|unencrypted_warning") : null;
2017-07-26 11:28:43 +01:00
return (
<div className="mx_AppPermission">
<div className="mx_AppPermission_content">
<div className="mx_AppPermission_content_bolder">{_t("widget|added_by")}</div>
<div>
{avatar}
2023-06-29 11:30:25 +01:00
<Heading size="4">{displayName}</Heading>
<div>{userId}</div>
</div>
<div>{warning}</div>
<div>
{_t("widget|cookie_warning")}&nbsp;{encryptionWarning}
</div>
<div>
<AccessibleButton kind="primary_sm" onClick={this.props.onPermissionGranted}>
{_t("action|continue")}
</AccessibleButton>
</div>
2017-07-26 16:47:58 +01:00
</div>
2017-07-26 11:28:43 +01:00
</div>
);
}
}