Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 72f8c98

Browse files
committed
Wire up dialog functions and ILAG-needed surface
1 parent 32873d3 commit 72f8c98

File tree

4 files changed

+150
-2
lines changed

4 files changed

+150
-2
lines changed

src/Lifecycle.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ export async function hydrateSession(credentials: IMatrixClientCreds): Promise<M
557557
*
558558
* @returns {Promise} promise which resolves to the new MatrixClient once it has been started
559559
*/
560-
async function doSetLoggedIn(
560+
export async function doSetLoggedIn(
561561
credentials: IMatrixClientCreds,
562562
clearStorageEnabled: boolean,
563563
): Promise<MatrixClient> {
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
Copyright 2022 The Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
import React, { createRef } from "react";
18+
19+
import ScrollableBaseModal, { IScrollableBaseState } from "./ScrollableBaseModal";
20+
import { IDialogProps } from "./IDialogProps";
21+
import { DialogContent, DialogProps } from "@matrix-org/react-sdk-module-api/lib/components/DialogContent";
22+
import { _t } from "../../../languageHandler";
23+
import { logger } from "matrix-js-sdk/src/logger";
24+
25+
interface IProps extends IDialogProps {
26+
contentFactory: (props: DialogProps, ref: React.Ref<DialogContent>) => React.ReactNode;
27+
contentProps: DialogProps;
28+
title: string;
29+
}
30+
31+
interface IState extends IScrollableBaseState {
32+
// nothing special
33+
}
34+
35+
export class ModuleUiDialog extends ScrollableBaseModal<IProps, IState> {
36+
private contentRef = createRef<DialogContent>();
37+
38+
public constructor(props: IProps) {
39+
super(props);
40+
41+
this.state = {
42+
title: this.props.title,
43+
canSubmit: true,
44+
actionLabel: _t("OK"),
45+
};
46+
}
47+
48+
protected async submit() {
49+
try {
50+
const model = await this.contentRef.current.trySubmit();
51+
this.props.onFinished(true, model);
52+
} catch (e) {
53+
logger.error("Error during submission of module dialog:", e);
54+
}
55+
}
56+
57+
protected cancel(): void {
58+
this.props.onFinished(false);
59+
}
60+
61+
protected renderContent(): React.ReactNode {
62+
return <div className="mx_ModuleUiDialog">
63+
{ this.props.contentFactory(this.props.contentProps, this.contentRef) }
64+
</div>;
65+
}
66+
}

src/components/views/rooms/RoomPreviewBar.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,9 @@ export default class RoomPreviewBar extends React.Component<IProps, IState> {
319319
}
320320
case MessageCase.NotLoggedIn: {
321321
const opts: RoomPreviewOpts = { canJoin: false };
322-
ModuleRunner.instance.invoke(RoomViewLifecycle.PreviewRoomNotLoggedIn, opts, this.props.room.roomId);
322+
if (this.props.room?.roomId) {
323+
ModuleRunner.instance.invoke(RoomViewLifecycle.PreviewRoomNotLoggedIn, opts, this.props.room.roomId);
324+
}
323325
if (opts.canJoin) {
324326
title = _t("Join the room to participate");
325327
primaryActionLabel = _t("Join");

src/modules/ProxiedModuleApi.ts

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ import { ModuleApi } from "@matrix-org/react-sdk-module-api/lib/ModuleApi";
1818
import { TranslationStringsObject } from "@matrix-org/react-sdk-module-api/lib/types/translations";
1919
import { Optional } from "matrix-events-sdk";
2020
import { _t } from "../languageHandler";
21+
import { DialogProps } from "@matrix-org/react-sdk-module-api/lib/components/DialogContent";
22+
import Modal from "../Modal";
23+
import { ModuleUiDialog } from "../components/views/dialogs/ModuleUiDialog";
24+
import React from "react";
25+
import { AccountCredentials } from "@matrix-org/react-sdk-module-api/lib/types/credentials";
26+
import * as Matrix from "matrix-js-sdk/src/matrix";
27+
import SdkConfig from "../SdkConfig";
28+
import PlatformPeg from "../PlatformPeg";
29+
import { doSetLoggedIn } from "../Lifecycle";
30+
import dispatcher from "../dispatcher/dispatcher";
2131

2232
export class ProxiedModuleApi implements ModuleApi {
2333
private cachedTranslations: Optional<TranslationStringsObject>;
@@ -33,4 +43,74 @@ export class ProxiedModuleApi implements ModuleApi {
3343
public translateString(s: string, variables?: Record<string, unknown>): string {
3444
return _t(s, variables);
3545
}
46+
47+
public openDialog<M extends object, P extends DialogProps = DialogProps, C extends React.Component = React.Component>(title: string, body: (props: P, ref: React.RefObject<C>) => React.ReactNode): Promise<{ didSubmit: boolean, model: M }> {
48+
return new Promise<{ didSubmit: boolean, model: M }>((resolve) => {
49+
Modal.createTrackedDialog("ModuleDialog", "", ModuleUiDialog, {
50+
title: title,
51+
contentFactory: body,
52+
contentProps: <DialogProps>{
53+
moduleApi: this,
54+
},
55+
}, "mx_CompoundDialog").finished.then(([didSubmit, model]) => {
56+
resolve({ didSubmit, model });
57+
});
58+
});
59+
}
60+
61+
public async registerAccount(username: string, password: string, displayName?: string): Promise<AccountCredentials> {
62+
const hsUrl = SdkConfig.get("validated_server_config").hsUrl;
63+
const client = Matrix.createClient({ baseUrl: hsUrl });
64+
const req = {
65+
username,
66+
password,
67+
initial_device_display_name: SdkConfig.get("default_device_display_name") || PlatformPeg.get().getDefaultDeviceDisplayName(),
68+
auth: undefined,
69+
inhibit_login: false,
70+
};
71+
const creds = await (client.registerRequest(req).catch(resp => client.registerRequest({
72+
...req,
73+
auth: {
74+
session: resp.data.session,
75+
type: "m.login.dummy",
76+
},
77+
})));
78+
79+
if (displayName) {
80+
const profileClient = Matrix.createClient({
81+
baseUrl: hsUrl,
82+
userId: creds.user_id,
83+
deviceId: creds.device_id,
84+
accessToken: creds.access_token,
85+
});
86+
await profileClient.setDisplayName(displayName);
87+
}
88+
89+
return {
90+
homeserverUrl: hsUrl,
91+
userId: creds.user_id,
92+
deviceId: creds.device_id,
93+
accessToken: creds.access_token,
94+
};
95+
}
96+
97+
public async useAccount(credentials: AccountCredentials): Promise<void> {
98+
await doSetLoggedIn({
99+
...credentials,
100+
guest: false,
101+
}, true);
102+
}
103+
104+
public async switchToRoom(roomId: string, andJoin?: boolean): Promise<void> {
105+
dispatcher.dispatch({
106+
action: "view_room",
107+
room_id: roomId,
108+
});
109+
110+
if (andJoin) {
111+
dispatcher.dispatch({
112+
action: "join_room",
113+
});
114+
}
115+
}
36116
}

0 commit comments

Comments
 (0)