Skip to content

Commit 8c2a035

Browse files
committed
Add support for Module API 1.3.0
Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
1 parent 0f7e394 commit 8c2a035

File tree

7 files changed

+216
-4
lines changed

7 files changed

+216
-4
lines changed

src/components/views/rooms/RoomPreviewBar.tsx

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import { UIFeature } from "../../../settings/UIFeature";
3131
import { ModuleRunner } from "../../../modules/ModuleRunner";
3232
import { Icon as AskToJoinIcon } from "../../../../res/img/element-icons/ask-to-join.svg";
3333
import Field from "../elements/Field";
34+
import ModuleApi from "../../../modules/Api.ts";
3435

3536
const MemberEventHtmlReasonField = "io.element.html_reason";
3637

@@ -116,7 +117,7 @@ interface IState {
116117
reason?: string;
117118
}
118119

119-
export default class RoomPreviewBar extends React.Component<IProps, IState> {
120+
class RoomPreviewBar extends React.Component<IProps, IState> {
120121
public static defaultProps = {
121122
onJoinClick() {},
122123
};
@@ -747,3 +748,20 @@ export default class RoomPreviewBar extends React.Component<IProps, IState> {
747748
);
748749
}
749750
}
751+
752+
const WrappedRoomPreviewBar = (props: IProps): JSX.Element => {
753+
const moduleRenderer = ModuleApi.customComponents.roomPreviewBarRenderer;
754+
if (moduleRenderer) {
755+
return moduleRenderer(
756+
{
757+
roomId: props.room?.roomId ?? props.roomId,
758+
roomAlias: props.room?.getCanonicalAlias() ?? props.roomAlias,
759+
},
760+
(props) => <RoomPreviewBar {...props} />,
761+
);
762+
}
763+
764+
return <RoomPreviewBar {...props} />;
765+
};
766+
767+
export default WrappedRoomPreviewBar;

src/modules/Api.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ import { WidgetPermissionCustomisations } from "../customisations/WidgetPermissi
2121
import { WidgetVariableCustomisations } from "../customisations/WidgetVariables.ts";
2222
import { ConfigApi } from "./ConfigApi.ts";
2323
import { I18nApi } from "./I18nApi.ts";
24-
import { CustomComponentsApi } from "./customComponentApi.ts";
24+
import { CustomComponentsApi } from "./customComponentApi.tsx";
25+
import { WatchableProfile } from "./Profile.ts";
26+
import { NavigationApi } from "./Navigation.ts";
27+
import { openDialog } from "./Dialog.tsx";
28+
import { overwriteAccountAuth } from "./Auth.ts";
2529

2630
const legacyCustomisationsFactory = <T extends object>(baseCustomisations: T) => {
2731
let used = false;
@@ -57,6 +61,11 @@ class ModuleApi implements Api {
5761
legacyCustomisationsFactory(WidgetVariableCustomisations);
5862
/* eslint-enable @typescript-eslint/naming-convention */
5963

64+
public readonly navigation = new NavigationApi();
65+
public readonly openDialog = openDialog;
66+
public readonly overwriteAccountAuth = overwriteAccountAuth;
67+
public readonly profile = new WatchableProfile();
68+
6069
public readonly config = new ConfigApi();
6170
public readonly i18n = new I18nApi();
6271
public readonly customComponents = new CustomComponentsApi();

src/modules/Auth.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
Copyright 2025 New Vector Ltd.
3+
4+
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
5+
Please see LICENSE files in the repository root for full details.
6+
*/
7+
8+
import { type AccountAuthInfo } from "@element-hq/element-web-module-api";
9+
10+
import type { OverwriteLoginPayload } from "../dispatcher/payloads/OverwriteLoginPayload.ts";
11+
import { Action } from "../dispatcher/actions.ts";
12+
import defaultDispatcher from "../dispatcher/dispatcher.ts";
13+
import type { ActionPayload } from "../dispatcher/payloads.ts";
14+
15+
export async function overwriteAccountAuth(accountInfo: AccountAuthInfo): Promise<void> {
16+
const { promise, resolve } = Promise.withResolvers<void>();
17+
18+
const onAction = (payload: ActionPayload): void => {
19+
if (payload.action === Action.OnLoggedIn) {
20+
// We want to wait for the new login to complete before returning.
21+
// See `Action.OnLoggedIn` in dispatcher.
22+
resolve();
23+
}
24+
};
25+
const dispatcherRef = defaultDispatcher.register(onAction);
26+
27+
defaultDispatcher.dispatch<OverwriteLoginPayload>(
28+
{
29+
action: Action.OverwriteLogin,
30+
credentials: {
31+
...accountInfo,
32+
guest: false,
33+
},
34+
},
35+
true,
36+
); // require to be sync to match inherited interface behaviour
37+
38+
// wait for login to complete
39+
await promise;
40+
defaultDispatcher.unregister(dispatcherRef);
41+
}

src/modules/Dialog.tsx

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
Copyright 2025 New Vector Ltd.
3+
4+
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
5+
Please see LICENSE files in the repository root for full details.
6+
*/
7+
8+
import React, { type ComponentType, type JSX, useCallback } from "react";
9+
import { type DialogProps, type DialogOptions, type DialogHandle } from "@element-hq/element-web-module-api";
10+
11+
import Modal from "../Modal";
12+
import BaseDialog from "../components/views/dialogs/BaseDialog.tsx";
13+
14+
const OuterDialog = <M, P extends object>({
15+
title,
16+
Dialog,
17+
props,
18+
onFinished,
19+
}: {
20+
title: string;
21+
Dialog: ComponentType<DialogProps<M> & P>;
22+
props: P;
23+
onFinished(ok: boolean, model: M | null): void;
24+
}): JSX.Element => {
25+
const close = useCallback(() => onFinished(false, null), [onFinished]);
26+
const submit = useCallback((model: M) => onFinished(true, model), [onFinished]);
27+
return (
28+
<BaseDialog onFinished={close} title={title}>
29+
<Dialog {...props} onSubmit={submit} onCancel={close} />
30+
</BaseDialog>
31+
);
32+
};
33+
34+
export function openDialog<M, P extends object>(
35+
initialOptions: DialogOptions,
36+
Dialog: ComponentType<P & DialogProps<M>>,
37+
props: P,
38+
): DialogHandle<M> {
39+
const { close, finished } = Modal.createDialog(OuterDialog<M, P>, {
40+
title: initialOptions.title,
41+
Dialog,
42+
props,
43+
});
44+
45+
return {
46+
finished: finished.then(([ok, model]) => ({
47+
ok: ok ?? false,
48+
model: model ?? null,
49+
})),
50+
close: () => close(false, null),
51+
};
52+
}

src/modules/Navigation.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
Copyright 2025 New Vector Ltd.
3+
4+
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
5+
Please see LICENSE files in the repository root for full details.
6+
*/
7+
8+
import { type NavigationApi as INavigationApi } from "@element-hq/element-web-module-api";
9+
10+
import { navigateToPermalink } from "../utils/permalinks/navigator.ts";
11+
import { parsePermalink } from "../utils/permalinks/Permalinks.ts";
12+
import { getCachedRoomIDForAlias } from "../RoomAliasCache.ts";
13+
import { MatrixClientPeg } from "../MatrixClientPeg.ts";
14+
import dispatcher from "../dispatcher/dispatcher.ts";
15+
import { Action } from "../dispatcher/actions.ts";
16+
import SettingsStore from "../settings/SettingsStore.ts";
17+
18+
export class NavigationApi implements INavigationApi {
19+
public async toMatrixToLink(link: string, join = false): Promise<void> {
20+
navigateToPermalink(link);
21+
22+
const parts = parsePermalink(link);
23+
if (parts?.roomIdOrAlias && join) {
24+
let roomId: string | undefined = parts.roomIdOrAlias;
25+
if (roomId.startsWith("#")) {
26+
roomId = getCachedRoomIDForAlias(parts.roomIdOrAlias);
27+
if (!roomId) {
28+
// alias resolution failed
29+
const result = await MatrixClientPeg.safeGet().getRoomIdForAlias(parts.roomIdOrAlias);
30+
roomId = result.room_id;
31+
}
32+
}
33+
34+
if (roomId) {
35+
dispatcher.dispatch({
36+
action: Action.JoinRoom,
37+
canAskToJoin: SettingsStore.getValue("feature_ask_to_join"),
38+
roomId,
39+
});
40+
}
41+
}
42+
}
43+
}

src/modules/Profile.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
Copyright 2025 New Vector Ltd.
3+
4+
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
5+
Please see LICENSE files in the repository root for full details.
6+
*/
7+
8+
import { type Profile, Watchable } from "@element-hq/element-web-module-api";
9+
10+
import { OwnProfileStore } from "../stores/OwnProfileStore.ts";
11+
import { UPDATE_EVENT } from "../stores/AsyncStore.ts";
12+
13+
export class WatchableProfile extends Watchable<Profile> {
14+
public constructor() {
15+
super({});
16+
this.value = this.profile;
17+
18+
OwnProfileStore.instance.on(UPDATE_EVENT, this.onProfileChange);
19+
}
20+
21+
private get profile(): Profile {
22+
return {
23+
userId: OwnProfileStore.instance.matrixClient?.getUserId() ?? undefined,
24+
displayName: OwnProfileStore.instance.displayName ?? undefined,
25+
};
26+
}
27+
28+
private readonly onProfileChange = (): void => {
29+
this.value = this.profile;
30+
};
31+
}

src/modules/customComponentApi.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import type {
1212
CustomComponentsApi as ICustomComponentsApi,
1313
CustomMessageRenderFunction,
1414
CustomMessageComponentProps as ModuleCustomMessageComponentProps,
15-
OriginalComponentProps,
15+
OriginalMessageComponentProps,
1616
CustomMessageRenderHints as ModuleCustomCustomMessageRenderHints,
1717
MatrixEvent as ModuleMatrixEvent,
1818
} from "@element-hq/element-web-module-api";
@@ -72,6 +72,7 @@ export class CustomComponentsApi implements ICustomComponentsApi {
7272
): void {
7373
this.registeredMessageRenderers.push({ eventTypeOrFilter: eventTypeOrFilter, renderer, hints });
7474
}
75+
7576
/**
7677
* Select the correct renderer based on the event information.
7778
* @param mxEvent The message event being rendered.
@@ -100,7 +101,7 @@ export class CustomComponentsApi implements ICustomComponentsApi {
100101
*/
101102
public renderMessage(
102103
props: CustomMessageComponentProps,
103-
originalComponent?: (props?: OriginalComponentProps) => React.JSX.Element,
104+
originalComponent?: (props?: OriginalMessageComponentProps) => React.JSX.Element,
104105
): React.JSX.Element | null {
105106
const moduleEv = CustomComponentsApi.getModuleMatrixEvent(props.mxEvent);
106107
const renderer = moduleEv && this.selectRenderer(moduleEv);
@@ -134,4 +135,21 @@ export class CustomComponentsApi implements ICustomComponentsApi {
134135
}
135136
return null;
136137
}
138+
139+
private _roomPreviewBarRenderer?: CustomRoomPreviewBarRenderFunction;
140+
141+
/**
142+
* Get the custom room preview bar renderer, if any has been registered.
143+
*/
144+
public get roomPreviewBarRenderer(): CustomRoomPreviewBarRenderFunction | undefined {
145+
return this._roomPreviewBarRenderer;
146+
}
147+
148+
/**
149+
* Register a custom room preview bar renderer.
150+
* @param renderer - the function that will render the custom room preview bar.
151+
*/
152+
public registerRoomPreviewBar(renderer: CustomRoomPreviewBarRenderFunction): void {
153+
this._roomPreviewBarRenderer = renderer;
154+
}
137155
}

0 commit comments

Comments
 (0)