From a968b4ce537ba12c4ec9d8d9f4e89c4104abc9b3 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Thu, 16 Dec 2021 09:57:10 +0000 Subject: [PATCH] Improve typing around LoggedInView (#7384) * Update SyncState imports * Improve typing around LoggedInView * Fix typing --- src/CallHandler.tsx | 2 +- src/components/structures/LoggedInView.tsx | 46 +++++--------------- src/components/structures/RoomStatusBar.tsx | 3 +- src/components/structures/TimelinePanel.tsx | 2 +- src/components/views/messages/MImageBody.tsx | 2 +- 5 files changed, 15 insertions(+), 40 deletions(-) diff --git a/src/CallHandler.tsx b/src/CallHandler.tsx index e7d6e99129..5d4973798d 100644 --- a/src/CallHandler.tsx +++ b/src/CallHandler.tsx @@ -26,7 +26,7 @@ import { randomUppercaseString, randomLowercaseString } from "matrix-js-sdk/src/ import EventEmitter from 'events'; import { RuleId, TweakName, Tweaks } from "matrix-js-sdk/src/@types/PushRules"; import { PushProcessor } from 'matrix-js-sdk/src/pushprocessor'; -import { SyncState } from "matrix-js-sdk/src/sync.api"; +import { SyncState } from "matrix-js-sdk/src/sync"; import { MatrixClientPeg } from './MatrixClientPeg'; import Modal from './Modal'; diff --git a/src/components/structures/LoggedInView.tsx b/src/components/structures/LoggedInView.tsx index e7fa353e51..d180a327a9 100644 --- a/src/components/structures/LoggedInView.tsx +++ b/src/components/structures/LoggedInView.tsx @@ -19,6 +19,8 @@ import { MatrixClient } from 'matrix-js-sdk/src/client'; import { MatrixEvent } from 'matrix-js-sdk/src/models/event'; import { MatrixCall } from 'matrix-js-sdk/src/webrtc/call'; import classNames from 'classnames'; +import { ISyncStateData, SyncState } from 'matrix-js-sdk/src/sync'; +import { IUsageLimit } from 'matrix-js-sdk/src/@types/partials'; import { Key } from '../../Keyboard'; import PageTypes from '../../PageTypes'; @@ -104,24 +106,8 @@ interface IProps { forceTimeline?: boolean; // see props on MatrixChat } -interface IUsageLimit { - // "hs_disabled" is NOT a specced string, but is used in Synapse - // This is tracked over at https://github.com/matrix-org/synapse/issues/9237 - // eslint-disable-next-line camelcase - limit_type: "monthly_active_user" | "hs_disabled" | string; - // eslint-disable-next-line camelcase - admin_contact?: string; -} - interface IState { - syncErrorData?: { - error: { - // This is not specced, but used in Synapse. See - // https://github.com/matrix-org/synapse/issues/9237#issuecomment-768238922 - data: IUsageLimit; - errcode: string; - }; - }; + syncErrorData?: ISyncStateData; usageLimitDismissed: boolean; usageLimitEventContent?: IUsageLimit; usageLimitEventTs?: number; @@ -304,33 +290,23 @@ class LoggedInView extends React.Component { }); }; - onSync = (syncState, oldSyncState, data) => { - const oldErrCode = ( - this.state.syncErrorData && - this.state.syncErrorData.error && - this.state.syncErrorData.error.errcode - ); + private onSync = (syncState: SyncState, oldSyncState?: SyncState, data?: ISyncStateData): void => { + const oldErrCode = this.state.syncErrorData?.error?.errcode; const newErrCode = data && data.error && data.error.errcode; if (syncState === oldSyncState && oldErrCode === newErrCode) return; - if (syncState === 'ERROR') { - this.setState({ - syncErrorData: data, - }); - } else { - this.setState({ - syncErrorData: null, - }); - } + this.setState({ + syncErrorData: syncState === SyncState.Error ? data : null, + }); - if (oldSyncState === 'PREPARED' && syncState === 'SYNCING') { + if (oldSyncState === SyncState.Prepared && syncState === SyncState.Syncing) { this.updateServerNoticeEvents(); } else { this.calculateServerLimitToast(this.state.syncErrorData, this.state.usageLimitEventContent); } }; - onRoomStateEvents = (ev, state) => { + private onRoomStateEvents = (ev: MatrixEvent): void => { const serverNoticeList = RoomListStore.instance.orderedLists[DefaultTagID.ServerNotice]; if (serverNoticeList && serverNoticeList.some(r => r.roomId === ev.getRoomId())) { this.updateServerNoticeEvents(); @@ -346,7 +322,7 @@ class LoggedInView extends React.Component { private calculateServerLimitToast(syncError: IState["syncErrorData"], usageLimitEventContent?: IUsageLimit) { const error = syncError && syncError.error && syncError.error.errcode === "M_RESOURCE_LIMIT_EXCEEDED"; if (error) { - usageLimitEventContent = syncError.error.data; + usageLimitEventContent = syncError.error.data as IUsageLimit; } // usageLimitDismissed is true when the user has explicitly hidden the toast diff --git a/src/components/structures/RoomStatusBar.tsx b/src/components/structures/RoomStatusBar.tsx index 76835d7c29..4bc13c7241 100644 --- a/src/components/structures/RoomStatusBar.tsx +++ b/src/components/structures/RoomStatusBar.tsx @@ -16,8 +16,7 @@ limitations under the License. import React from 'react'; import { EventStatus, MatrixEvent } from "matrix-js-sdk/src/models/event"; -import { SyncState } from "matrix-js-sdk/src/sync.api"; -import { ISyncStateData } from "matrix-js-sdk/src/sync"; +import { SyncState, ISyncStateData } from "matrix-js-sdk/src/sync"; import { Room } from "matrix-js-sdk/src/models/room"; import { _t, _td } from '../../languageHandler'; diff --git a/src/components/structures/TimelinePanel.tsx b/src/components/structures/TimelinePanel.tsx index f9b5694fa5..756a368e33 100644 --- a/src/components/structures/TimelinePanel.tsx +++ b/src/components/structures/TimelinePanel.tsx @@ -22,7 +22,7 @@ import { EventTimelineSet } from "matrix-js-sdk/src/models/event-timeline-set"; import { Direction, EventTimeline } from "matrix-js-sdk/src/models/event-timeline"; import { TimelineWindow } from "matrix-js-sdk/src/timeline-window"; import { EventType, RelationType } from 'matrix-js-sdk/src/@types/event'; -import { SyncState } from 'matrix-js-sdk/src/sync.api'; +import { SyncState } from 'matrix-js-sdk/src/sync'; import { debounce } from 'lodash'; import { logger } from "matrix-js-sdk/src/logger"; diff --git a/src/components/views/messages/MImageBody.tsx b/src/components/views/messages/MImageBody.tsx index e05aee6a25..86e895090d 100644 --- a/src/components/views/messages/MImageBody.tsx +++ b/src/components/views/messages/MImageBody.tsx @@ -17,7 +17,7 @@ limitations under the License. import React, { ComponentProps, createRef } from 'react'; import { Blurhash } from "react-blurhash"; -import { SyncState } from 'matrix-js-sdk/src/sync.api'; +import { SyncState } from 'matrix-js-sdk/src/sync'; import classNames from 'classnames'; import { CSSTransition, SwitchTransition } from 'react-transition-group'; import { logger } from "matrix-js-sdk/src/logger";