Skip to content

Commit 962b964

Browse files
authored
Merge pull request matrix-org#5412 from matrix-org/travis/msc2790-disabled-buttons
Disable buttons when required by MSC2790
2 parents f0ca8e1 + 1b6e47c commit 962b964

File tree

3 files changed

+40
-6
lines changed

3 files changed

+40
-6
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
"linkifyjs": "^2.1.9",
8080
"lodash": "^4.17.19",
8181
"matrix-js-sdk": "github:matrix-org/matrix-js-sdk#develop",
82-
"matrix-widget-api": "^0.1.0-beta.5",
82+
"matrix-widget-api": "^0.1.0-beta.8",
8383
"minimist": "^1.2.5",
8484
"pako": "^1.0.11",
8585
"parse5": "^5.1.1",

src/components/views/dialogs/ModalWidgetDialog.tsx

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ import {
2323
IModalWidgetCloseRequest,
2424
IModalWidgetOpenRequestData,
2525
IModalWidgetReturnData,
26+
ISetModalButtonEnabledActionRequest,
27+
IWidgetApiAcknowledgeResponseData,
28+
IWidgetApiErrorResponseData,
29+
BuiltInModalButtonID,
30+
ModalButtonID,
2631
ModalButtonKind,
2732
Widget,
2833
WidgetApiFromWidgetAction,
@@ -31,6 +36,7 @@ import {StopGapWidgetDriver} from "../../../stores/widgets/StopGapWidgetDriver";
3136
import {MatrixClientPeg} from "../../../MatrixClientPeg";
3237
import RoomViewStore from "../../../stores/RoomViewStore";
3338
import {OwnProfileStore} from "../../../stores/OwnProfileStore";
39+
import { arrayFastClone } from "../../../utils/arrays";
3440

3541
interface IProps {
3642
widgetDefinition: IModalWidgetOpenRequestData;
@@ -40,15 +46,19 @@ interface IProps {
4046

4147
interface IState {
4248
messaging?: ClientWidgetApi;
49+
disabledButtonIds: ModalButtonID[];
4350
}
4451

4552
const MAX_BUTTONS = 3;
4653

4754
export default class ModalWidgetDialog extends React.PureComponent<IProps, IState> {
4855
private readonly widget: Widget;
56+
private readonly possibleButtons: ModalButtonID[];
4957
private appFrame: React.RefObject<HTMLIFrameElement> = React.createRef();
5058

51-
state: IState = {};
59+
state: IState = {
60+
disabledButtonIds: [],
61+
};
5262

5363
constructor(props) {
5464
super(props);
@@ -58,6 +68,7 @@ export default class ModalWidgetDialog extends React.PureComponent<IProps, IStat
5868
creatorUserId: MatrixClientPeg.get().getUserId(),
5969
id: `modal_${this.props.sourceWidgetId}`,
6070
});
71+
this.possibleButtons = (this.props.widgetDefinition.buttons || []).map(b => b.id);
6172
}
6273

6374
public componentDidMount() {
@@ -79,12 +90,35 @@ export default class ModalWidgetDialog extends React.PureComponent<IProps, IStat
7990
private onLoad = () => {
8091
this.state.messaging.once("ready", this.onReady);
8192
this.state.messaging.on(`action:${WidgetApiFromWidgetAction.CloseModalWidget}`, this.onWidgetClose);
93+
this.state.messaging.on(`action:${WidgetApiFromWidgetAction.SetModalButtonEnabled}`, this.onButtonEnableToggle);
8294
};
8395

8496
private onWidgetClose = (ev: CustomEvent<IModalWidgetCloseRequest>) => {
8597
this.props.onFinished(true, ev.detail.data);
8698
}
8799

100+
private onButtonEnableToggle = (ev: CustomEvent<ISetModalButtonEnabledActionRequest>) => {
101+
ev.preventDefault();
102+
const isClose = ev.detail.data.button === BuiltInModalButtonID.Close;
103+
if (isClose || !this.possibleButtons.includes(ev.detail.data.button)) {
104+
return this.state.messaging.transport.reply(ev.detail, {
105+
error: {message: "Invalid button"},
106+
} as IWidgetApiErrorResponseData);
107+
}
108+
109+
let buttonIds: ModalButtonID[];
110+
if (ev.detail.data.enabled) {
111+
buttonIds = arrayFastClone(this.state.disabledButtonIds).filter(i => i !== ev.detail.data.button);
112+
} else {
113+
// use a set to swap the operation to avoid memory leaky arrays.
114+
const tempSet = new Set(this.state.disabledButtonIds);
115+
tempSet.add(ev.detail.data.button);
116+
buttonIds = Array.from(tempSet);
117+
}
118+
this.setState({disabledButtonIds: buttonIds});
119+
this.state.messaging.transport.reply(ev.detail, {} as IWidgetApiAcknowledgeResponseData);
120+
};
121+
88122
public render() {
89123
const templated = this.widget.getCompleteUrl({
90124
currentRoomId: RoomViewStore.getRoomId(),

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6532,10 +6532,10 @@ matrix-react-test-utils@^0.2.2:
65326532
resolved "https://registry.yarnpkg.com/matrix-react-test-utils/-/matrix-react-test-utils-0.2.2.tgz#c87144d3b910c7edc544a6699d13c7c2bf02f853"
65336533
integrity sha512-49+7gfV6smvBIVbeloql+37IeWMTD+fiywalwCqk8Dnz53zAFjKSltB3rmWHso1uecLtQEcPtCijfhzcLXAxTQ==
65346534

6535-
matrix-widget-api@^0.1.0-beta.5:
6536-
version "0.1.0-beta.5"
6537-
resolved "https://registry.yarnpkg.com/matrix-widget-api/-/matrix-widget-api-0.1.0-beta.5.tgz#dd7f24a177aa590d812bd4e92e2c3ac225c5557e"
6538-
integrity sha512-J3GBJtVMFuEM/EWFylc0IlkPjdgmWxrkGYPaZ0LSmxp+OlNJxYfnWPR6F6HveW+Z8C1i0vq+BTueofSqKv2zDg==
6535+
matrix-widget-api@^0.1.0-beta.8:
6536+
version "0.1.0-beta.8"
6537+
resolved "https://registry.yarnpkg.com/matrix-widget-api/-/matrix-widget-api-0.1.0-beta.8.tgz#17e85c03c46353373890b869b1fd46162bdb0026"
6538+
integrity sha512-sWqyWs0RQqny/BimZUOxUd9BTJBzQmJlJ1i3lsSh1JBygV+aK5xQsONL97fc4i6/nwQPK72uCVDF+HwTtkpAbQ==
65396539
dependencies:
65406540
events "^3.2.0"
65416541

0 commit comments

Comments
 (0)