Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
Apply strictNullChecks to src/components/views/context_menus/* (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
t3chguy committed Mar 14, 2023
1 parent 4c2b5df commit 7c2511a
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/components/views/context_menus/DeviceContextMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const DeviceContextMenuSection: React.FC<IDeviceContextMenuSectionProps> = ({ de

useEffect(() => {
const getDevices = async (): Promise<void> => {
return setDevices((await MediaDeviceHandler.getDevices())[deviceKind]);
return setDevices((await MediaDeviceHandler.getDevices())?.[deviceKind] ?? []);
};
getDevices();
}, [deviceKind]);
Expand Down
2 changes: 1 addition & 1 deletion src/components/views/context_menus/KebabContextMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export const KebabContextMenu: React.FC<KebabContextMenuProps> = ({ options, tit
compact
rightAligned
closeOnInteraction
{...contextMenuBelow(button.current.getBoundingClientRect())}
{...contextMenuBelow(button.current!.getBoundingClientRect())}
>
<IconizedContextMenuOptionList>{options}</IconizedContextMenuOptionList>
</IconizedContextMenu>
Expand Down
26 changes: 15 additions & 11 deletions src/components/views/context_menus/MessageContextMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ const ReplyInThreadButton: React.FC<IReplyInThreadButton> = ({ mxEvent, closeMen
if (mxEvent.getThread() && !mxEvent.isThreadRoot) {
dis.dispatch<ShowThreadPayload>({
action: Action.ShowThread,
rootEvent: mxEvent.getThread().rootEvent,
rootEvent: mxEvent.getThread()!.rootEvent!,
initialEvent: mxEvent,
scroll_into_view: true,
highlighted: true,
Expand Down Expand Up @@ -163,12 +163,12 @@ export default class MessageContextMenu extends React.Component<IProps, IState>
// to obliterate the room - https://github.com/matrix-org/synapse/issues/4042
// Similarly for encryption events, since redacting them "breaks everything"
const canRedact =
room.currentState.maySendRedactionForEvent(this.props.mxEvent, cli.credentials.userId) &&
!!room?.currentState.maySendRedactionForEvent(this.props.mxEvent, cli.getSafeUserId()) &&
this.props.mxEvent.getType() !== EventType.RoomServerAcl &&
this.props.mxEvent.getType() !== EventType.RoomEncryption;

let canPin =
room.currentState.mayClientSendStateEvent(EventType.RoomPinnedEvents, cli) &&
!!room?.currentState.mayClientSendStateEvent(EventType.RoomPinnedEvents, cli) &&
canPinEvent(this.props.mxEvent);

// HACK: Intentionally say we can't pin if the user doesn't want to use the functionality
Expand Down Expand Up @@ -249,9 +249,10 @@ export default class MessageContextMenu extends React.Component<IProps, IState>
private onPinClick = (): void => {
const cli = MatrixClientPeg.get();
const room = cli.getRoom(this.props.mxEvent.getRoomId());
if (!room) return;
const eventId = this.props.mxEvent.getId();

const pinnedIds = room?.currentState?.getStateEvents(EventType.RoomPinnedEvents, "")?.getContent().pinned || [];
const pinnedIds = room.currentState?.getStateEvents(EventType.RoomPinnedEvents, "")?.getContent().pinned || [];

if (pinnedIds.includes(eventId)) {
pinnedIds.splice(pinnedIds.indexOf(eventId), 1);
Expand All @@ -261,7 +262,7 @@ export default class MessageContextMenu extends React.Component<IProps, IState>
event_ids: [...(room.getAccountData(ReadPinsEventId)?.getContent()?.event_ids || []), eventId],
});
}
cli.sendStateEvent(this.props.mxEvent.getRoomId(), EventType.RoomPinnedEvents, { pinned: pinnedIds }, "");
cli.sendStateEvent(room.roomId, EventType.RoomPinnedEvents, { pinned: pinnedIds }, "");
this.closeMenu();
};

Expand Down Expand Up @@ -294,12 +295,13 @@ export default class MessageContextMenu extends React.Component<IProps, IState>

private onCopyLinkClick = (e: ButtonEvent): void => {
e.preventDefault(); // So that we don't open the permalink
if (!this.props.link) return;
copyPlaintext(this.props.link);
this.closeMenu();
};

private onCollapseReplyChainClick = (): void => {
this.props.collapseReplyChain();
this.props.collapseReplyChain?.();
this.closeMenu();
};

Expand Down Expand Up @@ -349,10 +351,12 @@ export default class MessageContextMenu extends React.Component<IProps, IState>
const cli = MatrixClientPeg.get();
const room = cli.getRoom(this.props.mxEvent.getRoomId());
const eventId = this.props.mxEvent.getId();
return room.getPendingEvents().filter((e) => {
const relation = e.getRelation();
return relation?.rel_type === RelationType.Annotation && relation.event_id === eventId && filter(e);
});
return (
room?.getPendingEvents().filter((e) => {
const relation = e.getRelation();
return relation?.rel_type === RelationType.Annotation && relation.event_id === eventId && filter(e);
}) ?? []
);
}

private getUnsentReactions(): MatrixEvent[] {
Expand Down Expand Up @@ -380,7 +384,7 @@ export default class MessageContextMenu extends React.Component<IProps, IState>
const eventStatus = mxEvent.status;
const unsentReactionsCount = this.getUnsentReactions().length;
const contentActionable = isContentActionable(mxEvent);
const permalink = this.props.permalinkCreator?.forEvent(this.props.mxEvent.getId());
const permalink = this.props.permalinkCreator?.forEvent(this.props.mxEvent.getId()!);
// status is SENT before remote-echo, null after
const isSent = !eventStatus || eventStatus === EventStatus.SENT;
const { timelineRenderingType, canReact, canSendMessages } = this.context;
Expand Down
2 changes: 1 addition & 1 deletion src/components/views/context_menus/RoomContextMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ const RoomContextMenu: React.FC<IProps> = ({ room, onFinished, ...props }) => {
Modal.createDialog(
DevtoolsDialog,
{
roomId: SdkContextClass.instance.roomViewStore.getRoomId(),
roomId: room.roomId,
},
"mx_DevtoolsDialog_wrapper",
);
Expand Down
9 changes: 4 additions & 5 deletions src/components/views/context_menus/ThreadListContextMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const ThreadListContextMenu: React.FC<ThreadListContextMenuProps> = ({
if (permalinkCreator) {
evt?.preventDefault();
evt?.stopPropagation();
const matrixToUrl = permalinkCreator.forEvent(mxEvent.getId());
const matrixToUrl = permalinkCreator.forEvent(mxEvent.getId()!);
await copyPlaintext(matrixToUrl);
closeThreadOptions();
}
Expand All @@ -84,9 +84,8 @@ const ThreadListContextMenu: React.FC<ThreadListContextMenuProps> = ({
onMenuToggle?.(menuDisplayed);
}, [menuDisplayed, onMenuToggle]);

const isMainSplitTimelineShown = !WidgetLayoutStore.instance.hasMaximisedWidget(
MatrixClientPeg.get().getRoom(mxEvent.getRoomId()),
);
const room = MatrixClientPeg.get().getRoom(mxEvent.getRoomId());
const isMainSplitTimelineShown = !!room && !WidgetLayoutStore.instance.hasMaximisedWidget(room);
return (
<React.Fragment>
<ContextMenuTooltipButton
Expand All @@ -104,7 +103,7 @@ const ThreadListContextMenu: React.FC<ThreadListContextMenuProps> = ({
className="mx_RoomTile_contextMenu"
compact
rightAligned
{...contextMenuBelow(button.current.getBoundingClientRect())}
{...contextMenuBelow(button.current!.getBoundingClientRect())}
>
<IconizedContextMenuOptionList>
{isMainSplitTimelineShown && (
Expand Down
20 changes: 10 additions & 10 deletions src/components/views/context_menus/WidgetContextMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ export const WidgetContextMenu: React.FC<IProps> = ({
const widgetMessaging = WidgetMessagingStore.instance.getMessagingForUid(WidgetUtils.getWidgetUid(app));
const canModify = userWidget || WidgetUtils.canUserModifyWidgets(roomId);

let streamAudioStreamButton;
if (getConfigLivestreamUrl() && WidgetType.JITSI.matches(app.type)) {
let streamAudioStreamButton: JSX.Element | undefined;
if (roomId && getConfigLivestreamUrl() && WidgetType.JITSI.matches(app.type)) {
const onStreamAudioClick = async (): Promise<void> => {
try {
await startJitsiAudioLivestream(widgetMessaging!, roomId);
Expand All @@ -87,12 +87,12 @@ export const WidgetContextMenu: React.FC<IProps> = ({
const pinnedWidgets = room ? WidgetLayoutStore.instance.getContainerWidgets(room, Container.Top) : [];
const widgetIndex = pinnedWidgets.findIndex((widget) => widget.id === app.id);

let editButton;
let editButton: JSX.Element | undefined;
if (canModify && WidgetUtils.isManagedByManager(app)) {
const _onEditClick = (): void => {
if (onEditClick) {
onEditClick();
} else {
} else if (room) {
WidgetUtils.editWidget(room, app);
}
onFinished();
Expand All @@ -101,7 +101,7 @@ export const WidgetContextMenu: React.FC<IProps> = ({
editButton = <IconizedContextMenuOption onClick={_onEditClick} label={_t("Edit")} />;
}

let snapshotButton;
let snapshotButton: JSX.Element | undefined;
const screenshotsEnabled = SettingsStore.getValue("enableWidgetScreenshots");
if (screenshotsEnabled && widgetMessaging?.hasCapability(MatrixCapabilities.Screenshots)) {
const onSnapshotClick = (): void => {
Expand All @@ -122,12 +122,12 @@ export const WidgetContextMenu: React.FC<IProps> = ({
snapshotButton = <IconizedContextMenuOption onClick={onSnapshotClick} label={_t("Take a picture")} />;
}

let deleteButton;
let deleteButton: JSX.Element | undefined;
if (onDeleteClick || canModify) {
const _onDeleteClick = (): void => {
if (onDeleteClick) {
onDeleteClick();
} else {
} else if (roomId) {
// Show delete confirmation dialog
Modal.createDialog(QuestionDialog, {
title: _t("Delete Widget"),
Expand Down Expand Up @@ -159,7 +159,7 @@ export const WidgetContextMenu: React.FC<IProps> = ({
app.creatorUserId === cli.getUserId();

const isLocalWidget = WidgetType.JITSI.matches(app.type);
let revokeButton;
let revokeButton: JSX.Element | undefined;
if (!userWidget && !isLocalWidget && isAllowedWidget) {
const opts: ApprovalOpts = { approved: undefined };
ModuleRunner.instance.invoke(WidgetLifecycle.PreLoadRequest, opts, new ElementWidget(app));
Expand All @@ -182,7 +182,7 @@ export const WidgetContextMenu: React.FC<IProps> = ({
}
}

let moveLeftButton;
let moveLeftButton: JSX.Element | undefined;
if (showUnpin && widgetIndex > 0) {
const onClick = (): void => {
if (!room) throw new Error("room must be defined");
Expand All @@ -193,7 +193,7 @@ export const WidgetContextMenu: React.FC<IProps> = ({
moveLeftButton = <IconizedContextMenuOption onClick={onClick} label={_t("Move left")} />;
}

let moveRightButton;
let moveRightButton: JSX.Element | undefined;
if (showUnpin && widgetIndex < pinnedWidgets.length - 1) {
const onClick = (): void => {
if (!room) throw new Error("room must be defined");
Expand Down

0 comments on commit 7c2511a

Please sign in to comment.