Files
ThreadNet-Web/src/components/views/dialogs/ModalWidgetDialog.tsx
T

157 lines
5.6 KiB
TypeScript
Raw Normal View History

2020-09-25 14:08:27 +01:00
/*
Copyright 2020 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.
*/
import * as React from 'react';
import BaseDialog from './BaseDialog';
import { _t } from '../../../languageHandler';
import AccessibleButton from "../elements/AccessibleButton";
2020-10-19 20:39:43 +01:00
import {
ClientWidgetApi,
IModalWidgetCloseRequest,
IModalWidgetOpenRequestData,
IModalWidgetReturnData,
ModalButtonKind,
Widget,
WidgetApiFromWidgetAction,
} from "matrix-widget-api";
import {StopGapWidgetDriver} from "../../../stores/widgets/StopGapWidgetDriver";
import {MatrixClientPeg} from "../../../MatrixClientPeg";
import RoomViewStore from "../../../stores/RoomViewStore";
import {OwnProfileStore} from "../../../stores/OwnProfileStore";
2020-09-25 14:08:27 +01:00
interface IProps {
2020-10-19 20:39:43 +01:00
widgetDefinition: IModalWidgetOpenRequestData;
2020-09-25 14:08:27 +01:00
sourceWidgetId: string;
2020-10-19 20:39:43 +01:00
onFinished(success: boolean, data?: IModalWidgetReturnData): void;
2020-09-25 14:08:27 +01:00
}
interface IState {
2020-10-19 20:39:43 +01:00
messaging?: ClientWidgetApi;
2020-09-25 14:08:27 +01:00
}
const MAX_BUTTONS = 3;
export default class ModalWidgetDialog extends React.PureComponent<IProps, IState> {
2020-10-19 20:39:43 +01:00
private readonly widget: Widget;
2020-09-25 14:08:27 +01:00
private appFrame: React.RefObject<HTMLIFrameElement> = React.createRef();
state: IState = {};
2020-10-19 20:39:43 +01:00
constructor(props) {
super(props);
this.widget = new Widget({
...this.props.widgetDefinition,
creatorUserId: MatrixClientPeg.get().getUserId(),
id: `modal_${this.props.sourceWidgetId}`,
});
2020-09-25 14:08:27 +01:00
}
public componentDidMount() {
2020-10-19 20:39:43 +01:00
const driver = new StopGapWidgetDriver( []);
const messaging = new ClientWidgetApi(this.widget, this.appFrame.current, driver);
2020-09-25 14:08:27 +01:00
this.setState({messaging});
}
public componentWillUnmount() {
2020-10-19 20:39:43 +01:00
this.state.messaging.off("ready", this.onReady);
this.state.messaging.off(`action:${WidgetApiFromWidgetAction.CloseModalWidget}`, this.onWidgetClose);
2020-09-25 14:08:27 +01:00
this.state.messaging.stop();
}
2020-10-19 20:39:43 +01:00
private onReady = () => {
this.state.messaging.sendWidgetConfig(this.props.widgetDefinition);
2020-09-25 14:08:27 +01:00
};
2020-10-19 20:39:43 +01:00
private onLoad = () => {
this.state.messaging.once("ready", this.onReady);
this.state.messaging.on(`action:${WidgetApiFromWidgetAction.CloseModalWidget}`, this.onWidgetClose);
};
private onWidgetClose = (ev: CustomEvent<IModalWidgetCloseRequest>) => {
this.props.onFinished(true, ev.detail.data);
2020-09-25 14:08:27 +01:00
}
public render() {
// TODO: Don't violate every single security principle
2020-10-19 20:39:43 +01:00
// TODO copied from SGWidget
const templated = this.widget.getCompleteUrl({
currentRoomId: RoomViewStore.getRoomId(),
currentUserId: MatrixClientPeg.get().getUserId(),
userDisplayName: OwnProfileStore.instance.displayName,
userHttpAvatarUrl: OwnProfileStore.instance.getHttpAvatarUrl(),
});
2020-09-25 14:08:27 +01:00
2020-10-19 20:39:43 +01:00
const parsed = new URL(templated);
// Add in some legacy support sprinkles (for non-popout widgets)
2020-09-28 10:50:31 +01:00
// TODO: Replace these with proper widget params
// See https://github.com/matrix-org/matrix-doc/pull/1958/files#r405714833
2020-10-19 20:39:43 +01:00
parsed.searchParams.set('widgetId', this.widget.id);
parsed.searchParams.set('parentUrl', window.location.href.split('#', 2)[0]);
// Replace the encoded dollar signs back to dollar signs. They have no special meaning
// in HTTP, but URL parsers encode them anyways.
const widgetUrl = parsed.toString().replace(/%24/g, '$');
2020-09-25 14:08:27 +01:00
let buttons;
if (this.props.widgetDefinition.buttons) {
// show first button rightmost for a more natural specification
buttons = this.props.widgetDefinition.buttons.slice(0, MAX_BUTTONS).reverse().map(def => {
let kind = "secondary";
switch (def.kind) {
2020-10-19 20:39:43 +01:00
case ModalButtonKind.Primary:
2020-09-25 14:08:27 +01:00
kind = "primary";
break;
2020-10-19 20:39:43 +01:00
case ModalButtonKind.Secondary:
2020-09-25 14:08:27 +01:00
kind = "primary_outline";
break
2020-10-19 20:39:43 +01:00
case ModalButtonKind.Danger:
2020-09-25 14:08:27 +01:00
kind = "danger";
break;
}
const onClick = () => {
2020-10-19 20:39:43 +01:00
this.state.messaging.notifyModalWidgetButtonClicked(def.id);
2020-09-25 14:08:27 +01:00
};
return <AccessibleButton key={def.id} kind={kind} onClick={onClick}>
{ def.label }
</AccessibleButton>;
});
}
return <BaseDialog
title={this.props.widgetDefinition.name || _t("Modal Widget")}
className="mx_ModalWidgetDialog"
contentId="mx_Dialog_content"
onFinished={this.props.onFinished}
>
<div>
<iframe
ref={this.appFrame}
2020-09-28 11:59:39 +01:00
// sandbox="allow-forms allow-scripts" TODO
2020-09-28 10:50:31 +01:00
src={widgetUrl.toString()}
2020-09-25 14:08:27 +01:00
onLoad={this.onLoad}
/>
</div>
<div className="mx_ModalWidgetDialog_buttons" style={{float: "right"}}>
{ buttons }
</div>
</BaseDialog>;
}
}