Skip to content

Commit 277a3c0

Browse files
authored
Conform more of the codebase to strict typescript (matrix-org#10841)
1 parent af78a5a commit 277a3c0

12 files changed

+89
-60
lines changed

src/IdentityAuthClient.tsx

+5-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,11 @@ export default class IdentityAuthClient {
6464

6565
private writeToken(): void {
6666
if (this.tempClient) return; // temporary client: ignore
67-
window.localStorage.setItem("mx_is_access_token", this.accessToken);
67+
if (this.accessToken) {
68+
window.localStorage.setItem("mx_is_access_token", this.accessToken);
69+
} else {
70+
window.localStorage.removeItem("mx_is_access_token");
71+
}
6872
}
6973

7074
private readToken(): string | null {

src/components/structures/RightPanel.tsx

+44-29
Original file line numberDiff line numberDiff line change
@@ -44,26 +44,37 @@ import TimelineCard from "../views/right_panel/TimelineCard";
4444
import { UPDATE_EVENT } from "../../stores/AsyncStore";
4545
import { IRightPanelCard, IRightPanelCardState } from "../../stores/right-panel/RightPanelStoreIPanelState";
4646
import { Action } from "../../dispatcher/actions";
47+
import { XOR } from "../../@types/common";
4748

48-
interface IProps {
49-
room?: Room; // if showing panels for a given room, this is set
49+
interface BaseProps {
5050
overwriteCard?: IRightPanelCard; // used to display a custom card and ignoring the RightPanelStore (used for UserView)
5151
resizeNotifier: ResizeNotifier;
52-
permalinkCreator?: RoomPermalinkCreator;
5352
e2eStatus?: E2EStatus;
5453
}
5554

55+
interface RoomlessProps extends BaseProps {
56+
room?: undefined;
57+
permalinkCreator?: undefined;
58+
}
59+
60+
interface RoomProps extends BaseProps {
61+
room: Room;
62+
permalinkCreator: RoomPermalinkCreator;
63+
}
64+
65+
type Props = XOR<RoomlessProps, RoomProps>;
66+
5667
interface IState {
5768
phase?: RightPanelPhases;
5869
searchQuery: string;
5970
cardState?: IRightPanelCardState;
6071
}
6172

62-
export default class RightPanel extends React.Component<IProps, IState> {
73+
export default class RightPanel extends React.Component<Props, IState> {
6374
public static contextType = MatrixClientContext;
6475
public context!: React.ContextType<typeof MatrixClientContext>;
6576

66-
public constructor(props: IProps, context: React.ContextType<typeof MatrixClientContext>) {
77+
public constructor(props: Props, context: React.ContextType<typeof MatrixClientContext>) {
6778
super(props, context);
6879

6980
this.state = {
@@ -89,7 +100,7 @@ export default class RightPanel extends React.Component<IProps, IState> {
89100
RightPanelStore.instance.off(UPDATE_EVENT, this.onRightPanelStoreUpdate);
90101
}
91102

92-
public static getDerivedStateFromProps(props: IProps): Partial<IState> {
103+
public static getDerivedStateFromProps(props: Props): Partial<IState> {
93104
let currentCard: IRightPanelCard | undefined;
94105
if (props.room) {
95106
currentCard = RightPanelStore.instance.currentCardForRoom(props.room.roomId);
@@ -169,32 +180,36 @@ export default class RightPanel extends React.Component<IProps, IState> {
169180
}
170181
break;
171182
case RightPanelPhases.SpaceMemberList:
172-
card = (
173-
<MemberList
174-
roomId={cardState?.spaceId ?? roomId}
175-
key={cardState?.spaceId ?? roomId}
176-
onClose={this.onClose}
177-
searchQuery={this.state.searchQuery}
178-
onSearchQueryChanged={this.onSearchQueryChanged}
179-
/>
180-
);
183+
if (!!cardState?.spaceId || !!roomId) {
184+
card = (
185+
<MemberList
186+
roomId={cardState?.spaceId ?? roomId!}
187+
key={cardState?.spaceId ?? roomId!}
188+
onClose={this.onClose}
189+
searchQuery={this.state.searchQuery}
190+
onSearchQueryChanged={this.onSearchQueryChanged}
191+
/>
192+
);
193+
}
181194
break;
182195

183196
case RightPanelPhases.RoomMemberInfo:
184197
case RightPanelPhases.SpaceMemberInfo:
185198
case RightPanelPhases.EncryptionPanel: {
186-
const roomMember = cardState?.member instanceof RoomMember ? cardState.member : undefined;
187-
card = (
188-
<UserInfo
189-
user={cardState?.member}
190-
room={this.context.getRoom(roomMember?.roomId) ?? this.props.room}
191-
key={roomId ?? cardState?.member?.userId}
192-
onClose={this.onClose}
193-
phase={phase}
194-
verificationRequest={cardState?.verificationRequest}
195-
verificationRequestPromise={cardState?.verificationRequestPromise}
196-
/>
197-
);
199+
if (!!cardState?.member) {
200+
const roomMember = cardState.member instanceof RoomMember ? cardState.member : undefined;
201+
card = (
202+
<UserInfo
203+
user={cardState.member}
204+
room={this.context.getRoom(roomMember?.roomId) ?? this.props.room}
205+
key={roomId ?? cardState.member.userId}
206+
onClose={this.onClose}
207+
phase={phase}
208+
verificationRequest={cardState.verificationRequest}
209+
verificationRequestPromise={cardState.verificationRequestPromise}
210+
/>
211+
);
212+
}
198213
break;
199214
}
200215
case RightPanelPhases.Room3pidMemberInfo:
@@ -261,10 +276,10 @@ export default class RightPanel extends React.Component<IProps, IState> {
261276
break;
262277

263278
case RightPanelPhases.ThreadPanel:
264-
if (!!roomId) {
279+
if (!!this.props.room) {
265280
card = (
266281
<ThreadPanel
267-
roomId={roomId}
282+
roomId={this.props.room.roomId}
268283
resizeNotifier={this.props.resizeNotifier}
269284
onClose={this.onClose}
270285
permalinkCreator={this.props.permalinkCreator}

src/components/structures/RoomView.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -1488,6 +1488,7 @@ export class RoomView extends React.Component<IRoomProps, IRoomState> {
14881488
// rate limited because a power level change will emit an event for every member in the room.
14891489
private updateRoomMembers = throttle(
14901490
() => {
1491+
if (!this.state.room) return;
14911492
this.updateDMState();
14921493
this.updateE2EStatus(this.state.room);
14931494
},

src/components/structures/TimelinePanel.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,7 @@ class TimelinePanel extends React.Component<IProps, IState> {
740740

741741
if (!this.messagePanel.current?.getScrollState()) return;
742742

743-
if (!this.messagePanel.current.getScrollState().stuckAtBottom) {
743+
if (!this.messagePanel.current.getScrollState()?.stuckAtBottom) {
744744
// we won't load this event now, because we don't want to push any
745745
// events off the other end of the timeline. But we need to note
746746
// that we can now paginate.
@@ -981,7 +981,7 @@ class TimelinePanel extends React.Component<IProps, IState> {
981981
{ leading: true, trailing: true },
982982
);
983983

984-
private readMarkerTimeout(readMarkerPosition: number): number {
984+
private readMarkerTimeout(readMarkerPosition: number | null): number {
985985
return readMarkerPosition === 0
986986
? this.context?.readMarkerInViewThresholdMs ?? this.state.readMarkerInViewThresholdMs
987987
: this.context?.readMarkerOutOfViewThresholdMs ?? this.state.readMarkerOutOfViewThresholdMs;

src/components/views/dialogs/VerificationRequestDialog.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ export default class VerificationRequestDialog extends React.Component<IProps, I
5151
const member = this.props.member || (otherUserId ? MatrixClientPeg.get().getUser(otherUserId) : null);
5252
const title = request?.isSelfVerification ? _t("Verify other device") : _t("Verification Request");
5353

54+
if (!member) return null;
55+
5456
return (
5557
<BaseDialog
5658
className="mx_InfoDialog"

src/components/views/rooms/EditMessageComposer.tsx

+3-6
Original file line numberDiff line numberDiff line change
@@ -152,13 +152,10 @@ class EditMessageComposer extends React.Component<IEditMessageComposerProps, ISt
152152
}
153153

154154
private getRoom(): Room {
155-
const roomId = this.props.editState.getEvent().getRoomId();
156-
const room = this.props.mxClient.getRoom(roomId);
157-
// Something is very wrong if we encounter this
158-
if (!room) {
159-
throw new Error(`Cannot find room for event ${roomId}`);
155+
if (!this.context.room) {
156+
throw new Error(`Cannot render without room`);
160157
}
161-
return room;
158+
return this.context.room;
162159
}
163160

164161
private onKeyDown = (event: KeyboardEvent): void => {

src/components/views/rooms/NotificationBadge.tsx

+18-14
Original file line numberDiff line numberDiff line change
@@ -126,19 +126,23 @@ export default class NotificationBadge extends React.PureComponent<XOR<IProps, I
126126
tooltip = <Tooltip className="mx_NotificationBadge_tooltip" label={label} />;
127127
}
128128

129-
return (
130-
<StatelessNotificationBadge
131-
label={label}
132-
symbol={notification.symbol}
133-
count={notification.count}
134-
color={notification.color}
135-
onClick={onClick}
136-
onMouseOver={this.onMouseOver}
137-
onMouseLeave={this.onMouseLeave}
138-
tabIndex={tabIndex}
139-
>
140-
{tooltip}
141-
</StatelessNotificationBadge>
142-
);
129+
const commonProps: React.ComponentProps<typeof StatelessNotificationBadge> = {
130+
label,
131+
symbol: notification.symbol,
132+
count: notification.count,
133+
color: notification.color,
134+
onMouseOver: this.onMouseOver,
135+
onMouseLeave: this.onMouseLeave,
136+
};
137+
138+
if (onClick) {
139+
return (
140+
<StatelessNotificationBadge {...commonProps} onClick={onClick} tabIndex={tabIndex}>
141+
{tooltip}
142+
</StatelessNotificationBadge>
143+
);
144+
}
145+
146+
return <StatelessNotificationBadge {...commonProps}>{tooltip}</StatelessNotificationBadge>;
143147
}
144148
}

src/components/views/settings/tabs/user/KeyboardUserSettingsTab.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ const KeyboardUserSettingsTab: React.FC = () => {
7474
return (
7575
<SettingsTab>
7676
<SettingsSection heading={_t("Keyboard")}>
77-
{visibleCategories.map(([categoryName, category]: [CategoryName, ICategory]) => {
77+
{visibleCategories.map(([categoryName, category]) => {
7878
return (
7979
<KeyboardShortcutSection key={categoryName} categoryName={categoryName} category={category} />
8080
);

src/stores/CallStore.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ export class CallStore extends AsyncStoreWithClient<{}> {
146146
this.calls.set(room.roomId, call);
147147
this.callListeners.set(
148148
call,
149-
new Map<CallEvent, (...args: unknown[]) => unknown>([
149+
new Map<CallEvent, (...args: any[]) => unknown>([
150150
[CallEvent.ConnectionState, onConnectionState],
151151
[CallEvent.Destroy, onDestroy],
152152
]),

src/stores/RoomScrollStateStore.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,12 @@ export class RoomScrollStateStore {
4242
return this.scrollStateMap.get(roomId);
4343
}
4444

45-
public setScrollState(roomId: string, scrollState: ScrollState): void {
46-
this.scrollStateMap.set(roomId, scrollState);
45+
public setScrollState(roomId: string, scrollState: ScrollState | null): void {
46+
if (scrollState === null) {
47+
this.scrollStateMap.delete(roomId);
48+
} else {
49+
this.scrollStateMap.set(roomId, scrollState);
50+
}
4751
}
4852
}
4953

src/utils/FormattingUtils.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@ export function formatCryptoKey(key: string): string {
7575
*
7676
* @return {number}
7777
*/
78-
export function hashCode(str: string): number {
78+
export function hashCode(str?: string): number {
7979
let hash = 0;
8080
let chr: number;
81-
if (str.length === 0) {
81+
if (!str?.length) {
8282
return hash;
8383
}
8484
for (let i = 0; i < str.length; i++) {
@@ -89,7 +89,7 @@ export function hashCode(str: string): number {
8989
return Math.abs(hash);
9090
}
9191

92-
export function getUserNameColorClass(userId: string): string {
92+
export function getUserNameColorClass(userId?: string): string {
9393
const colorNumber = (hashCode(userId) % 8) + 1;
9494
return `mx_Username_color${colorNumber}`;
9595
}

test/components/views/rooms/EditMessageComposer-test.tsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,9 @@ describe("<EditMessageComposer/>", () => {
151151
it("should throw when room for message is not found", () => {
152152
mockClient.getRoom.mockReturnValue(null);
153153
const editState = new EditorStateTransfer(editedEvent);
154-
expect(() => getComponent(editState)).toThrow("Cannot find room for event !abc:test");
154+
expect(() => getComponent(editState, { ...defaultRoomContext, room: undefined })).toThrow(
155+
"Cannot render without room",
156+
);
155157
});
156158

157159
describe("createEditContent", () => {

0 commit comments

Comments
 (0)