From 1fb8c17c36f08c964efbf927ecc4b25f1ccc3fe0 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Thu, 19 Dec 2024 16:30:17 +0000 Subject: [PATCH] Iterate Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- src/components/structures/LoggedInView.tsx | 3 ++- .../views/settings/devices/useOwnDevices.ts | 2 +- src/hooks/useAccountData.ts | 4 +-- .../handlers/AccountSettingsHandler.ts | 4 +-- .../right_panel/RoomSummaryCard-test.tsx | 25 +++++++++---------- 5 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/components/structures/LoggedInView.tsx b/src/components/structures/LoggedInView.tsx index 019f9cd1a8..88e7e93790 100644 --- a/src/components/structures/LoggedInView.tsx +++ b/src/components/structures/LoggedInView.tsx @@ -16,6 +16,7 @@ import { IUsageLimit, SyncStateData, SyncState, + EventType, } from "matrix-js-sdk/src/matrix"; import { MatrixCall } from "matrix-js-sdk/src/webrtc/call"; import classNames from "classnames"; @@ -161,7 +162,7 @@ class LoggedInView extends React.Component { this._matrixClient.on(ClientEvent.AccountData, this.onAccountData); // check push rules on start up as well - monitorSyncedPushRules(this._matrixClient.getAccountData("m.push_rules"), this._matrixClient); + monitorSyncedPushRules(this._matrixClient.getAccountData(EventType.PushRules), this._matrixClient); this._matrixClient.on(ClientEvent.Sync, this.onSync); // Call `onSync` with the current state as well this.onSync(this._matrixClient.getSyncState(), null, this._matrixClient.getSyncStateData() ?? undefined); diff --git a/src/components/views/settings/devices/useOwnDevices.ts b/src/components/views/settings/devices/useOwnDevices.ts index 51a05f6242..52b8e0aa63 100644 --- a/src/components/views/settings/devices/useOwnDevices.ts +++ b/src/components/views/settings/devices/useOwnDevices.ts @@ -116,7 +116,7 @@ export const useOwnDevices = (): DevicesState => { const notificationSettings = new Map(); Object.keys(devices).forEach((deviceId) => { - const eventType = `${LOCAL_NOTIFICATION_SETTINGS_PREFIX.name}.${deviceId}`; + const eventType = `${LOCAL_NOTIFICATION_SETTINGS_PREFIX.name}.${deviceId}` as const; const event = matrixClient.getAccountData(eventType); if (event) { notificationSettings.set(deviceId, event.getContent()); diff --git a/src/hooks/useAccountData.ts b/src/hooks/useAccountData.ts index 0f55969e29..b2fe464e55 100644 --- a/src/hooks/useAccountData.ts +++ b/src/hooks/useAccountData.ts @@ -7,14 +7,14 @@ Please see LICENSE files in the repository root for full details. */ import { useCallback, useState } from "react"; -import { ClientEvent, MatrixClient, MatrixEvent } from "matrix-js-sdk/src/matrix"; +import { AccountDataEvents, ClientEvent, MatrixClient, MatrixEvent } from "matrix-js-sdk/src/matrix"; import { useTypedEventEmitter } from "./useEventEmitter"; const tryGetContent = (ev?: MatrixEvent): T | undefined => ev?.getContent(); // Hook to simplify listening to Matrix account data -export const useAccountData = (cli: MatrixClient, eventType: string): T => { +export const useAccountData = (cli: MatrixClient, eventType: keyof AccountDataEvents): T => { const [value, setValue] = useState(() => tryGetContent(cli.getAccountData(eventType))); const handler = useCallback( diff --git a/src/settings/handlers/AccountSettingsHandler.ts b/src/settings/handlers/AccountSettingsHandler.ts index 60fb6eed19..9187bd0d49 100644 --- a/src/settings/handlers/AccountSettingsHandler.ts +++ b/src/settings/handlers/AccountSettingsHandler.ts @@ -144,7 +144,7 @@ export default class AccountSettingsHandler extends MatrixClientBackedSettingsHa eventType: K, field: F, value: AccountDataEvents[K][F], - legacyEventType?: string, + legacyEventType?: keyof AccountDataEvents, ): Promise { let content = this.getSettings(eventType); if (legacyEventType && !content?.[field]) { @@ -213,7 +213,7 @@ export default class AccountSettingsHandler extends MatrixClientBackedSettingsHa return this.client && !this.client.isGuest(); } - private getSettings(eventType = "im.vector.web.settings"): any { + private getSettings(eventType: keyof AccountDataEvents = "im.vector.web.settings"): any { // TODO: [TS] Types on return if (!this.client) return null; diff --git a/test/unit-tests/components/views/right_panel/RoomSummaryCard-test.tsx b/test/unit-tests/components/views/right_panel/RoomSummaryCard-test.tsx index 80d2609577..52e7b5a118 100644 --- a/test/unit-tests/components/views/right_panel/RoomSummaryCard-test.tsx +++ b/test/unit-tests/components/views/right_panel/RoomSummaryCard-test.tsx @@ -338,19 +338,18 @@ describe("", () => { }); it("does not show public room label for a DM", async () => { - mockClient.getAccountData.mockImplementation( - (eventType) => - ({ - [EventType.Direct]: new MatrixEvent({ - type: EventType.Direct, - content: { - "@bob:sesame.st": ["some-room-id"], - // this room is a DM with ernie - "@ernie:sesame.st": ["some-other-room-id", room.roomId], - }, - }), - })[eventType], - ); + mockClient.getAccountData.mockImplementation((eventType) => { + if (eventType === EventType.Direct) { + return new MatrixEvent({ + type: EventType.Direct, + content: { + "@bob:sesame.st": ["some-room-id"], + // this room is a DM with ernie + "@ernie:sesame.st": ["some-other-room-id", room.roomId], + }, + }); + } + }); getComponent(); await flushPromises();