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

Commit

Permalink
Wire up dialog functions and ILAG-needed surface
Browse files Browse the repository at this point in the history
  • Loading branch information
turt2live committed Apr 6, 2022
1 parent b45582b commit d093606
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ export async function hydrateSession(credentials: IMatrixClientCreds): Promise<M
*
* @returns {Promise} promise which resolves to the new MatrixClient once it has been started
*/
async function doSetLoggedIn(
export async function doSetLoggedIn(
credentials: IMatrixClientCreds,
clearStorageEnabled: boolean,
): Promise<MatrixClient> {
Expand Down
66 changes: 66 additions & 0 deletions src/components/views/dialogs/ModuleUiDialog.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
Copyright 2022 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, { createRef } from "react";

import ScrollableBaseModal, { IScrollableBaseState } from "./ScrollableBaseModal";
import { IDialogProps } from "./IDialogProps";
import { DialogContent, DialogProps } from "@matrix-org/react-sdk-module-api/lib/components/DialogContent";
import { _t } from "../../../languageHandler";
import { logger } from "matrix-js-sdk/src/logger";

interface IProps extends IDialogProps {
contentFactory: (props: DialogProps, ref: React.Ref<DialogContent>) => React.ReactNode;
contentProps: DialogProps;
title: string;
}

interface IState extends IScrollableBaseState {
// nothing special
}

export class ModuleUiDialog extends ScrollableBaseModal<IProps, IState> {
private contentRef = createRef<DialogContent>();

public constructor(props: IProps) {
super(props);

this.state = {
title: this.props.title,
canSubmit: true,
actionLabel: _t("OK"),
};
}

protected async submit() {
try {
const model = await this.contentRef.current.trySubmit();
this.props.onFinished(true, model);
} catch (e) {
logger.error("Error during submission of module dialog:", e);
}
}

protected cancel(): void {
this.props.onFinished(false);
}

protected renderContent(): React.ReactNode {
return <div className="mx_ModuleUiDialog">
{ this.props.contentFactory(this.props.contentProps, this.contentRef) }
</div>;
}
}
4 changes: 3 additions & 1 deletion src/components/views/rooms/RoomPreviewBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,9 @@ export default class RoomPreviewBar extends React.Component<IProps, IState> {
}
case MessageCase.NotLoggedIn: {
const opts: RoomPreviewOpts = { canJoin: false };
ModuleRunner.instance.invoke(RoomViewLifecycle.PreviewRoomNotLoggedIn, opts, this.props.room.roomId);
if (this.props.room?.roomId) {
ModuleRunner.instance.invoke(RoomViewLifecycle.PreviewRoomNotLoggedIn, opts, this.props.room.roomId);
}
if (opts.canJoin) {
title = _t("Join the room to participate");
primaryActionLabel = _t("Join");
Expand Down
80 changes: 80 additions & 0 deletions src/modules/ProxiedModuleApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ import { ModuleApi } from "@matrix-org/react-sdk-module-api/lib/ModuleApi";
import { TranslationStringsObject } from "@matrix-org/react-sdk-module-api/lib/types/translations";
import { Optional } from "matrix-events-sdk";
import { _t } from "../languageHandler";
import { DialogProps } from "@matrix-org/react-sdk-module-api/lib/components/DialogContent";
import Modal from "../Modal";
import { ModuleUiDialog } from "../components/views/dialogs/ModuleUiDialog";
import React from "react";
import { AccountCredentials } from "@matrix-org/react-sdk-module-api/lib/types/credentials";
import * as Matrix from "matrix-js-sdk/src/matrix";
import SdkConfig from "../SdkConfig";
import PlatformPeg from "../PlatformPeg";
import { doSetLoggedIn } from "../Lifecycle";
import dispatcher from "../dispatcher/dispatcher";

export class ProxiedModuleApi implements ModuleApi {
private cachedTranslations: Optional<TranslationStringsObject>;
Expand All @@ -33,4 +43,74 @@ export class ProxiedModuleApi implements ModuleApi {
public translateString(s: string, variables?: Record<string, unknown>): string {
return _t(s, variables);
}

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 }> {
return new Promise<{ didSubmit: boolean, model: M }>((resolve) => {
Modal.createTrackedDialog("ModuleDialog", "", ModuleUiDialog, {
title: title,
contentFactory: body,
contentProps: <DialogProps>{
moduleApi: this,
},
}, "mx_CompoundDialog").finished.then(([didSubmit, model]) => {
resolve({ didSubmit, model });
});
});
}

public async registerAccount(username: string, password: string, displayName?: string): Promise<AccountCredentials> {
const hsUrl = SdkConfig.get("validated_server_config").hsUrl;
const client = Matrix.createClient({ baseUrl: hsUrl });
const req = {
username,
password,
initial_device_display_name: SdkConfig.get("default_device_display_name") || PlatformPeg.get().getDefaultDeviceDisplayName(),
auth: undefined,
inhibit_login: false,
};
const creds = await (client.registerRequest(req).catch(resp => client.registerRequest({
...req,
auth: {
session: resp.data.session,
type: "m.login.dummy",
},
})));

if (displayName) {
const profileClient = Matrix.createClient({
baseUrl: hsUrl,
userId: creds.user_id,
deviceId: creds.device_id,
accessToken: creds.access_token,
});
await profileClient.setDisplayName(displayName);
}

return {
homeserverUrl: hsUrl,
userId: creds.user_id,
deviceId: creds.device_id,
accessToken: creds.access_token,
};
}

public async useAccount(credentials: AccountCredentials): Promise<void> {
await doSetLoggedIn({
...credentials,
guest: false,
}, true);
}

public async switchToRoom(roomId: string, andJoin?: boolean): Promise<void> {
dispatcher.dispatch({
action: "view_room",
room_id: roomId,
});

if (andJoin) {
dispatcher.dispatch({
action: "join_room",
});
}
}
}

0 comments on commit d093606

Please sign in to comment.