2019-08-23 18:12:40 +03:00
|
|
|
/*
|
|
|
|
Copyright 2019 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 React from 'react';
|
2021-06-29 15:26:09 +03:00
|
|
|
import { IntegrationManagers } from "../../../integrations/IntegrationManagers";
|
|
|
|
import { Room } from "matrix-js-sdk/src/models/room";
|
|
|
|
import { dialogTermsInteractionCallback, TermsNotSignedError } from "../../../Terms";
|
2019-08-23 18:12:40 +03:00
|
|
|
import classNames from 'classnames';
|
2019-12-20 04:19:56 +03:00
|
|
|
import * as ScalarMessaging from "../../../ScalarMessaging";
|
2021-06-29 15:26:09 +03:00
|
|
|
import { replaceableComponent } from "../../../utils/replaceableComponent";
|
2021-09-05 10:11:08 +03:00
|
|
|
import { IntegrationManagerInstance } from "../../../integrations/IntegrationManagerInstance";
|
|
|
|
import ScalarAuthClient from "../../../ScalarAuthClient";
|
|
|
|
import AccessibleButton from "../elements/AccessibleButton";
|
|
|
|
import IntegrationManager from "../settings/IntegrationManager";
|
2021-09-10 20:01:05 +03:00
|
|
|
import { IDialogProps } from "./IDialogProps";
|
2021-09-05 10:11:08 +03:00
|
|
|
|
2021-09-10 20:01:05 +03:00
|
|
|
interface IProps extends IDialogProps {
|
2021-09-05 10:11:08 +03:00
|
|
|
/**
|
|
|
|
* Optional room where the integration manager should be open to
|
|
|
|
*/
|
2021-09-10 20:03:29 +03:00
|
|
|
room?: Room;
|
2021-09-05 10:11:08 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Optional screen to open on the integration manager
|
|
|
|
*/
|
|
|
|
screen?: string;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Optional integration ID to open in the integration manager
|
|
|
|
*/
|
|
|
|
integrationId?: string;
|
|
|
|
}
|
2019-08-23 18:12:40 +03:00
|
|
|
|
2021-09-05 10:11:08 +03:00
|
|
|
interface IState {
|
|
|
|
managers: IntegrationManagerInstance[];
|
|
|
|
busy: boolean;
|
|
|
|
currentIndex: number;
|
|
|
|
currentConnected: boolean;
|
|
|
|
currentLoading: boolean;
|
|
|
|
currentScalarClient: ScalarAuthClient;
|
|
|
|
}
|
2019-08-23 18:12:40 +03:00
|
|
|
|
2021-09-05 10:11:08 +03:00
|
|
|
@replaceableComponent("views.dialogs.TabbedIntegrationManagerDialog")
|
|
|
|
export default class TabbedIntegrationManagerDialog extends React.Component<IProps, IState> {
|
|
|
|
constructor(props: IProps) {
|
2019-08-23 18:12:40 +03:00
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
managers: IntegrationManagers.sharedInstance().getOrderedManagers(),
|
|
|
|
busy: true,
|
|
|
|
currentIndex: 0,
|
|
|
|
currentConnected: false,
|
|
|
|
currentLoading: true,
|
|
|
|
currentScalarClient: null,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-09-05 10:11:08 +03:00
|
|
|
public componentDidMount(): void {
|
2019-08-23 18:12:40 +03:00
|
|
|
this.openManager(0, true);
|
|
|
|
}
|
|
|
|
|
2021-09-05 10:11:08 +03:00
|
|
|
private openManager = async (i: number, force = false): Promise<void> => {
|
2019-08-23 18:12:40 +03:00
|
|
|
if (i === this.state.currentIndex && !force) return;
|
|
|
|
|
|
|
|
const manager = this.state.managers[i];
|
|
|
|
const client = manager.getScalarClient();
|
|
|
|
this.setState({
|
|
|
|
busy: true,
|
|
|
|
currentIndex: i,
|
|
|
|
currentLoading: true,
|
|
|
|
currentConnected: false,
|
|
|
|
currentScalarClient: client,
|
|
|
|
});
|
|
|
|
|
|
|
|
ScalarMessaging.setOpenManagerUrl(manager.uiUrl);
|
|
|
|
|
|
|
|
client.setTermsInteractionCallback((policyInfo, agreedUrls) => {
|
|
|
|
// To avoid visual glitching of two modals stacking briefly, we customise the
|
2019-11-21 06:14:20 +03:00
|
|
|
// terms dialog sizing when it will appear for the integration manager so that
|
2019-08-23 18:12:40 +03:00
|
|
|
// it gets the same basic size as the IM's own modal.
|
|
|
|
return dialogTermsInteractionCallback(
|
2019-11-21 06:14:20 +03:00
|
|
|
policyInfo, agreedUrls, 'mx_TermsDialog_forIntegrationManager',
|
2019-08-23 18:12:40 +03:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
|
|
|
await client.connect();
|
|
|
|
if (!client.hasCredentials()) {
|
|
|
|
this.setState({
|
|
|
|
busy: false,
|
|
|
|
currentLoading: false,
|
|
|
|
currentConnected: false,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.setState({
|
|
|
|
busy: false,
|
|
|
|
currentLoading: false,
|
|
|
|
currentConnected: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
if (e instanceof TermsNotSignedError) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
console.error(e);
|
|
|
|
this.setState({
|
|
|
|
busy: false,
|
|
|
|
currentLoading: false,
|
|
|
|
currentConnected: false,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-09-05 10:11:08 +03:00
|
|
|
private renderTabs(): JSX.Element[] {
|
2019-08-23 18:12:40 +03:00
|
|
|
return this.state.managers.map((m, i) => {
|
|
|
|
const classes = classNames({
|
|
|
|
'mx_TabbedIntegrationManagerDialog_tab': true,
|
|
|
|
'mx_TabbedIntegrationManagerDialog_currentTab': this.state.currentIndex === i,
|
|
|
|
});
|
|
|
|
return (
|
|
|
|
<AccessibleButton
|
|
|
|
className={classes}
|
|
|
|
onClick={() => this.openManager(i)}
|
|
|
|
key={`tab_${i}`}
|
|
|
|
disabled={this.state.busy}
|
|
|
|
>
|
2021-07-20 00:43:11 +03:00
|
|
|
{ m.name }
|
2019-08-23 18:12:40 +03:00
|
|
|
</AccessibleButton>
|
|
|
|
);
|
2019-08-23 18:16:44 +03:00
|
|
|
});
|
2019-08-23 18:12:40 +03:00
|
|
|
}
|
|
|
|
|
2021-09-05 10:11:08 +03:00
|
|
|
public renderTab(): JSX.Element {
|
2019-08-23 18:12:40 +03:00
|
|
|
let uiUrl = null;
|
|
|
|
if (this.state.currentScalarClient) {
|
|
|
|
uiUrl = this.state.currentScalarClient.getScalarInterfaceUrlForRoom(
|
|
|
|
this.props.room,
|
|
|
|
this.props.screen,
|
|
|
|
this.props.integrationId,
|
|
|
|
);
|
|
|
|
}
|
2019-11-21 06:14:20 +03:00
|
|
|
return <IntegrationManager
|
2019-08-23 18:12:40 +03:00
|
|
|
loading={this.state.currentLoading}
|
|
|
|
connected={this.state.currentConnected}
|
|
|
|
url={uiUrl}
|
|
|
|
onFinished={() => {/* no-op */}}
|
|
|
|
/>;
|
|
|
|
}
|
|
|
|
|
2021-09-05 10:11:08 +03:00
|
|
|
public render(): JSX.Element {
|
2019-08-23 18:12:40 +03:00
|
|
|
return (
|
|
|
|
<div className='mx_TabbedIntegrationManagerDialog_container'>
|
|
|
|
<div className='mx_TabbedIntegrationManagerDialog_tabs'>
|
2021-09-05 10:11:08 +03:00
|
|
|
{ this.renderTabs() }
|
2019-08-23 18:12:40 +03:00
|
|
|
</div>
|
|
|
|
<div className='mx_TabbedIntegrationManagerDialog_currentManager'>
|
2021-09-05 10:11:08 +03:00
|
|
|
{ this.renderTab() }
|
2019-08-23 18:12:40 +03:00
|
|
|
</div>
|
|
|
|
</div>
|
2019-08-23 18:16:44 +03:00
|
|
|
);
|
2019-08-23 18:12:40 +03:00
|
|
|
}
|
|
|
|
}
|