Skip to content

Commit

Permalink
Conform more of the codebase to strict typescript (#10841)
Browse files Browse the repository at this point in the history
  • Loading branch information
t3chguy committed May 25, 2023
1 parent af78a5a commit 277a3c0
Show file tree
Hide file tree
Showing 12 changed files with 89 additions and 60 deletions.
6 changes: 5 additions & 1 deletion src/IdentityAuthClient.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ export default class IdentityAuthClient {

private writeToken(): void {
if (this.tempClient) return; // temporary client: ignore
window.localStorage.setItem("mx_is_access_token", this.accessToken);
if (this.accessToken) {
window.localStorage.setItem("mx_is_access_token", this.accessToken);
} else {
window.localStorage.removeItem("mx_is_access_token");
}
}

private readToken(): string | null {
Expand Down
73 changes: 44 additions & 29 deletions src/components/structures/RightPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,26 +44,37 @@ import TimelineCard from "../views/right_panel/TimelineCard";
import { UPDATE_EVENT } from "../../stores/AsyncStore";
import { IRightPanelCard, IRightPanelCardState } from "../../stores/right-panel/RightPanelStoreIPanelState";
import { Action } from "../../dispatcher/actions";
import { XOR } from "../../@types/common";

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

interface RoomlessProps extends BaseProps {
room?: undefined;
permalinkCreator?: undefined;
}

interface RoomProps extends BaseProps {
room: Room;
permalinkCreator: RoomPermalinkCreator;
}

type Props = XOR<RoomlessProps, RoomProps>;

interface IState {
phase?: RightPanelPhases;
searchQuery: string;
cardState?: IRightPanelCardState;
}

export default class RightPanel extends React.Component<IProps, IState> {
export default class RightPanel extends React.Component<Props, IState> {
public static contextType = MatrixClientContext;
public context!: React.ContextType<typeof MatrixClientContext>;

public constructor(props: IProps, context: React.ContextType<typeof MatrixClientContext>) {
public constructor(props: Props, context: React.ContextType<typeof MatrixClientContext>) {
super(props, context);

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

public static getDerivedStateFromProps(props: IProps): Partial<IState> {
public static getDerivedStateFromProps(props: Props): Partial<IState> {
let currentCard: IRightPanelCard | undefined;
if (props.room) {
currentCard = RightPanelStore.instance.currentCardForRoom(props.room.roomId);
Expand Down Expand Up @@ -169,32 +180,36 @@ export default class RightPanel extends React.Component<IProps, IState> {
}
break;
case RightPanelPhases.SpaceMemberList:
card = (
<MemberList
roomId={cardState?.spaceId ?? roomId}
key={cardState?.spaceId ?? roomId}
onClose={this.onClose}
searchQuery={this.state.searchQuery}
onSearchQueryChanged={this.onSearchQueryChanged}
/>
);
if (!!cardState?.spaceId || !!roomId) {
card = (
<MemberList
roomId={cardState?.spaceId ?? roomId!}
key={cardState?.spaceId ?? roomId!}
onClose={this.onClose}
searchQuery={this.state.searchQuery}
onSearchQueryChanged={this.onSearchQueryChanged}
/>
);
}
break;

case RightPanelPhases.RoomMemberInfo:
case RightPanelPhases.SpaceMemberInfo:
case RightPanelPhases.EncryptionPanel: {
const roomMember = cardState?.member instanceof RoomMember ? cardState.member : undefined;
card = (
<UserInfo
user={cardState?.member}
room={this.context.getRoom(roomMember?.roomId) ?? this.props.room}
key={roomId ?? cardState?.member?.userId}
onClose={this.onClose}
phase={phase}
verificationRequest={cardState?.verificationRequest}
verificationRequestPromise={cardState?.verificationRequestPromise}
/>
);
if (!!cardState?.member) {
const roomMember = cardState.member instanceof RoomMember ? cardState.member : undefined;
card = (
<UserInfo
user={cardState.member}
room={this.context.getRoom(roomMember?.roomId) ?? this.props.room}
key={roomId ?? cardState.member.userId}
onClose={this.onClose}
phase={phase}
verificationRequest={cardState.verificationRequest}
verificationRequestPromise={cardState.verificationRequestPromise}
/>
);
}
break;
}
case RightPanelPhases.Room3pidMemberInfo:
Expand Down Expand Up @@ -261,10 +276,10 @@ export default class RightPanel extends React.Component<IProps, IState> {
break;

case RightPanelPhases.ThreadPanel:
if (!!roomId) {
if (!!this.props.room) {
card = (
<ThreadPanel
roomId={roomId}
roomId={this.props.room.roomId}
resizeNotifier={this.props.resizeNotifier}
onClose={this.onClose}
permalinkCreator={this.props.permalinkCreator}
Expand Down
1 change: 1 addition & 0 deletions src/components/structures/RoomView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1488,6 +1488,7 @@ export class RoomView extends React.Component<IRoomProps, IRoomState> {
// rate limited because a power level change will emit an event for every member in the room.
private updateRoomMembers = throttle(
() => {
if (!this.state.room) return;
this.updateDMState();
this.updateE2EStatus(this.state.room);
},
Expand Down
4 changes: 2 additions & 2 deletions src/components/structures/TimelinePanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,7 @@ class TimelinePanel extends React.Component<IProps, IState> {

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

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

private readMarkerTimeout(readMarkerPosition: number): number {
private readMarkerTimeout(readMarkerPosition: number | null): number {
return readMarkerPosition === 0
? this.context?.readMarkerInViewThresholdMs ?? this.state.readMarkerInViewThresholdMs
: this.context?.readMarkerOutOfViewThresholdMs ?? this.state.readMarkerOutOfViewThresholdMs;
Expand Down
2 changes: 2 additions & 0 deletions src/components/views/dialogs/VerificationRequestDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ export default class VerificationRequestDialog extends React.Component<IProps, I
const member = this.props.member || (otherUserId ? MatrixClientPeg.get().getUser(otherUserId) : null);
const title = request?.isSelfVerification ? _t("Verify other device") : _t("Verification Request");

if (!member) return null;

return (
<BaseDialog
className="mx_InfoDialog"
Expand Down
9 changes: 3 additions & 6 deletions src/components/views/rooms/EditMessageComposer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,10 @@ class EditMessageComposer extends React.Component<IEditMessageComposerProps, ISt
}

private getRoom(): Room {
const roomId = this.props.editState.getEvent().getRoomId();
const room = this.props.mxClient.getRoom(roomId);
// Something is very wrong if we encounter this
if (!room) {
throw new Error(`Cannot find room for event ${roomId}`);
if (!this.context.room) {
throw new Error(`Cannot render without room`);
}
return room;
return this.context.room;
}

private onKeyDown = (event: KeyboardEvent): void => {
Expand Down
32 changes: 18 additions & 14 deletions src/components/views/rooms/NotificationBadge.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,19 +126,23 @@ export default class NotificationBadge extends React.PureComponent<XOR<IProps, I
tooltip = <Tooltip className="mx_NotificationBadge_tooltip" label={label} />;
}

return (
<StatelessNotificationBadge
label={label}
symbol={notification.symbol}
count={notification.count}
color={notification.color}
onClick={onClick}
onMouseOver={this.onMouseOver}
onMouseLeave={this.onMouseLeave}
tabIndex={tabIndex}
>
{tooltip}
</StatelessNotificationBadge>
);
const commonProps: React.ComponentProps<typeof StatelessNotificationBadge> = {
label,
symbol: notification.symbol,
count: notification.count,
color: notification.color,
onMouseOver: this.onMouseOver,
onMouseLeave: this.onMouseLeave,
};

if (onClick) {
return (
<StatelessNotificationBadge {...commonProps} onClick={onClick} tabIndex={tabIndex}>
{tooltip}
</StatelessNotificationBadge>
);
}

return <StatelessNotificationBadge {...commonProps}>{tooltip}</StatelessNotificationBadge>;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ const KeyboardUserSettingsTab: React.FC = () => {
return (
<SettingsTab>
<SettingsSection heading={_t("Keyboard")}>
{visibleCategories.map(([categoryName, category]: [CategoryName, ICategory]) => {
{visibleCategories.map(([categoryName, category]) => {
return (
<KeyboardShortcutSection key={categoryName} categoryName={categoryName} category={category} />
);
Expand Down
2 changes: 1 addition & 1 deletion src/stores/CallStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export class CallStore extends AsyncStoreWithClient<{}> {
this.calls.set(room.roomId, call);
this.callListeners.set(
call,
new Map<CallEvent, (...args: unknown[]) => unknown>([
new Map<CallEvent, (...args: any[]) => unknown>([
[CallEvent.ConnectionState, onConnectionState],
[CallEvent.Destroy, onDestroy],
]),
Expand Down
8 changes: 6 additions & 2 deletions src/stores/RoomScrollStateStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,12 @@ export class RoomScrollStateStore {
return this.scrollStateMap.get(roomId);
}

public setScrollState(roomId: string, scrollState: ScrollState): void {
this.scrollStateMap.set(roomId, scrollState);
public setScrollState(roomId: string, scrollState: ScrollState | null): void {
if (scrollState === null) {
this.scrollStateMap.delete(roomId);
} else {
this.scrollStateMap.set(roomId, scrollState);
}
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/utils/FormattingUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ export function formatCryptoKey(key: string): string {
*
* @return {number}
*/
export function hashCode(str: string): number {
export function hashCode(str?: string): number {
let hash = 0;
let chr: number;
if (str.length === 0) {
if (!str?.length) {
return hash;
}
for (let i = 0; i < str.length; i++) {
Expand All @@ -89,7 +89,7 @@ export function hashCode(str: string): number {
return Math.abs(hash);
}

export function getUserNameColorClass(userId: string): string {
export function getUserNameColorClass(userId?: string): string {
const colorNumber = (hashCode(userId) % 8) + 1;
return `mx_Username_color${colorNumber}`;
}
Expand Down
4 changes: 3 additions & 1 deletion test/components/views/rooms/EditMessageComposer-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,9 @@ describe("<EditMessageComposer/>", () => {
it("should throw when room for message is not found", () => {
mockClient.getRoom.mockReturnValue(null);
const editState = new EditorStateTransfer(editedEvent);
expect(() => getComponent(editState)).toThrow("Cannot find room for event !abc:test");
expect(() => getComponent(editState, { ...defaultRoomContext, room: undefined })).toThrow(
"Cannot render without room",
);
});

describe("createEditContent", () => {
Expand Down

0 comments on commit 277a3c0

Please sign in to comment.