Skip to content
This repository was archived by the owner on Jul 8, 2025. It is now read-only.

Commit 1d9a66b

Browse files
Kerrytoomore
authored andcommitted
Apply strictNullChecks to src/components/views/dialogs/devtools/* (#10391)
1 parent 56fd2ea commit 1d9a66b

File tree

9 files changed

+34
-26
lines changed

9 files changed

+34
-26
lines changed

src/components/views/dialogs/devtools/AccountData.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
Copyright 2022 Michael Telatynski <[email protected]>
3+
Copyright 2023 The Matrix.org Foundation C.I.C.
34
45
Licensed under the Apache License, Version 2.0 (the "License");
56
you may not use this file except in compliance with the License.
@@ -29,7 +30,7 @@ export const AccountDataEventEditor: React.FC<IEditorProps> = ({ mxEvent, onBack
2930
const fields = useMemo(() => [eventTypeField(mxEvent?.getType())], [mxEvent]);
3031

3132
const onSend = async ([eventType]: string[], content?: IContent): Promise<void> => {
32-
await cli.setAccountData(eventType, content);
33+
await cli.setAccountData(eventType, content || {});
3334
};
3435

3536
const defaultContent = mxEvent ? stringify(mxEvent.getContent()) : undefined;
@@ -43,7 +44,7 @@ export const RoomAccountDataEventEditor: React.FC<IEditorProps> = ({ mxEvent, on
4344
const fields = useMemo(() => [eventTypeField(mxEvent?.getType())], [mxEvent]);
4445

4546
const onSend = async ([eventType]: string[], content?: IContent): Promise<void> => {
46-
await cli.setRoomAccountData(context.room.roomId, eventType, content);
47+
await cli.setRoomAccountData(context.room.roomId, eventType, content || {});
4748
};
4849

4950
const defaultContent = mxEvent ? stringify(mxEvent.getContent()) : undefined;
@@ -58,7 +59,7 @@ interface IProps extends IDevtoolsProps {
5859

5960
const BaseAccountDataExplorer: React.FC<IProps> = ({ events, Editor, actionLabel, onBack, setTool }) => {
6061
const [query, setQuery] = useState("");
61-
const [event, setEvent] = useState<MatrixEvent>(null);
62+
const [event, setEvent] = useState<MatrixEvent | null>(null);
6263

6364
if (event) {
6465
const onBack = (): void => {

src/components/views/dialogs/devtools/BaseTool.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
Copyright 2022 Michael Telatynski <[email protected]>
3+
Copyright 2023 The Matrix.org Foundation C.I.C.
34
45
Licensed under the Apache License, Version 2.0 (the "License");
56
you may not use this file except in compliance with the License.
@@ -38,7 +39,7 @@ interface IProps extends IMinProps {
3839
}
3940

4041
const BaseTool: React.FC<XOR<IMinProps, IProps>> = ({ className, actionLabel, onBack, onAction, children }) => {
41-
const [message, setMessage] = useState<string>(null);
42+
const [message, setMessage] = useState<string | null>(null);
4243

4344
const onBackClick = (): void => {
4445
if (message) {
@@ -48,7 +49,7 @@ const BaseTool: React.FC<XOR<IMinProps, IProps>> = ({ className, actionLabel, on
4849
}
4950
};
5051

51-
let actionButton: JSX.Element;
52+
let actionButton: ReactNode = null;
5253
if (message) {
5354
children = message;
5455
} else if (onAction) {

src/components/views/dialogs/devtools/Event.tsx

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
Copyright 2022 Michael Telatynski <[email protected]>
3+
Copyright 2023 The Matrix.org Foundation C.I.C.
34
45
Licensed under the Apache License, Version 2.0 (the "License");
56
you may not use this file except in compliance with the License.
@@ -55,7 +56,7 @@ export const stateKeyField = (defaultValue?: string): IFieldDef => ({
5556
const validateEventContent = withValidation<any, Error | undefined>({
5657
deriveData({ value }) {
5758
try {
58-
JSON.parse(value);
59+
JSON.parse(value!);
5960
} catch (e) {
6061
return e;
6162
}
@@ -75,7 +76,7 @@ const validateEventContent = withValidation<any, Error | undefined>({
7576
export const EventEditor: React.FC<IEventEditorProps> = ({ fieldDefs, defaultContent = "{\n\n}", onSend, onBack }) => {
7677
const [fieldData, setFieldData] = useState<string[]>(fieldDefs.map((def) => def.default ?? ""));
7778
const [content, setContent] = useState<string>(defaultContent);
78-
const contentField = useRef<Field>();
79+
const contentField = useRef<Field>(null);
7980

8081
const fields = fieldDefs.map((def, i) => (
8182
<Field
@@ -96,12 +97,12 @@ export const EventEditor: React.FC<IEventEditorProps> = ({ fieldDefs, defaultCon
9697
/>
9798
));
9899

99-
const onAction = async (): Promise<string> => {
100-
const valid = await contentField.current.validate({});
100+
const onAction = async (): Promise<string | undefined> => {
101+
const valid = contentField.current ? await contentField.current.validate({}) : false;
101102

102103
if (!valid) {
103-
contentField.current.focus();
104-
contentField.current.validate({ focused: true });
104+
contentField.current?.focus();
105+
contentField.current?.validate({ focused: true });
105106
return;
106107
}
107108

@@ -140,7 +141,7 @@ export interface IEditorProps extends Pick<IDevtoolsProps, "onBack"> {
140141
}
141142

142143
interface IViewerProps extends Required<IEditorProps> {
143-
Editor: React.FC<Required<IEditorProps>>;
144+
Editor: React.FC<IEditorProps>;
144145
}
145146

146147
export const EventViewer: React.FC<IViewerProps> = ({ mxEvent, onBack, Editor }) => {
@@ -168,7 +169,7 @@ export const EventViewer: React.FC<IViewerProps> = ({ mxEvent, onBack, Editor })
168169
const getBaseEventId = (baseEvent: MatrixEvent): string => {
169170
// show the replacing event, not the original, if it is an edit
170171
const mxEvent = baseEvent.replacingEvent() ?? baseEvent;
171-
return mxEvent.getWireContent()["m.relates_to"]?.event_id ?? baseEvent.getId();
172+
return mxEvent.getWireContent()["m.relates_to"]?.event_id ?? baseEvent.getId()!;
172173
};
173174

174175
export const TimelineEventEditor: React.FC<IEditorProps> = ({ mxEvent, onBack }) => {
@@ -178,10 +179,10 @@ export const TimelineEventEditor: React.FC<IEditorProps> = ({ mxEvent, onBack })
178179
const fields = useMemo(() => [eventTypeField(mxEvent?.getType())], [mxEvent]);
179180

180181
const onSend = ([eventType]: string[], content?: IContent): Promise<unknown> => {
181-
return cli.sendEvent(context.room.roomId, eventType, content);
182+
return cli.sendEvent(context.room.roomId, eventType, content || {});
182183
};
183184

184-
let defaultContent: string;
185+
let defaultContent = "";
185186

186187
if (mxEvent) {
187188
const originalContent = mxEvent.getContent();

src/components/views/dialogs/devtools/FilteredList.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
Copyright 2022 Michael Telatynski <[email protected]>
3+
Copyright 2023 The Matrix.org Foundation C.I.C.
34
45
Licensed under the Apache License, Version 2.0 (the "License");
56
you may not use this file except in compliance with the License.
@@ -37,7 +38,7 @@ const FilteredList: React.FC<IProps> = ({ children, query, onChange }) => {
3738
let filteredChildren = children;
3839
if (query) {
3940
const lcQuery = query.toLowerCase();
40-
filteredChildren = children.filter((child) => child.key.toString().toLowerCase().includes(lcQuery));
41+
filteredChildren = children.filter((child) => child.key?.toString().toLowerCase().includes(lcQuery));
4142
}
4243
setFilteredChildren(filteredChildren);
4344
setTruncateAt(INITIAL_LOAD_TILES);

src/components/views/dialogs/devtools/RoomState.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
Copyright 2022 Michael Telatynski <[email protected]>
3+
Copyright 2023 The Matrix.org Foundation C.I.C.
34
45
Licensed under the Apache License, Version 2.0 (the "License");
56
you may not use this file except in compliance with the License.

src/components/views/dialogs/devtools/ServersInRoom.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
Copyright 2022 Michael Telatynski <[email protected]>
3+
Copyright 2023 The Matrix.org Foundation C.I.C.
34
45
Licensed under the Apache License, Version 2.0 (the "License");
56
you may not use this file except in compliance with the License.
@@ -27,7 +28,7 @@ const ServersInRoom: React.FC<IDevtoolsProps> = ({ onBack }) => {
2728
const servers: Record<string, number> = {};
2829
context.room.currentState.getStateEvents(EventType.RoomMember).forEach((ev) => {
2930
if (ev.getContent().membership !== "join") return; // only count joined users
30-
const server = ev.getSender().split(":")[1];
31+
const server = ev.getSender()!.split(":")[1];
3132
servers[server] = (servers[server] ?? 0) + 1;
3233
});
3334
return servers;

src/components/views/dialogs/devtools/SettingExplorer.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
Copyright 2022 Michael Telatynski <[email protected]>
3-
Copyright 2018-2021 The Matrix.org Foundation C.I.C.
3+
Copyright 2018-2023 The Matrix.org Foundation C.I.C.
44
55
Licensed under the Apache License, Version 2.0 (the "License");
66
you may not use this file except in compliance with the License.
@@ -27,7 +27,7 @@ import { SETTINGS } from "../../../../settings/Settings";
2727
import Field from "../../elements/Field";
2828

2929
const SettingExplorer: React.FC<IDevtoolsProps> = ({ onBack }) => {
30-
const [setting, setSetting] = useState<string>(null);
30+
const [setting, setSetting] = useState<string | null>(null);
3131
const [editing, setEditing] = useState(false);
3232

3333
if (setting && editing) {
@@ -73,7 +73,7 @@ const CanEditLevelField: React.FC<ICanEditLevelFieldProps> = ({ setting, roomId,
7373
);
7474
};
7575

76-
function renderExplicitSettingValues(setting: string, roomId: string): string {
76+
function renderExplicitSettingValues(setting: string, roomId?: string): string {
7777
const vals: Record<string, number | null> = {};
7878
for (const level of LEVEL_ORDER) {
7979
try {
@@ -94,12 +94,12 @@ interface IEditSettingProps extends Pick<IDevtoolsProps, "onBack"> {
9494

9595
const EditSetting: React.FC<IEditSettingProps> = ({ setting, onBack }) => {
9696
const context = useContext(DevtoolsContext);
97-
const [explicitValue, setExplicitValue] = useState(renderExplicitSettingValues(setting, null));
97+
const [explicitValue, setExplicitValue] = useState(renderExplicitSettingValues(setting));
9898
const [explicitRoomValue, setExplicitRoomValue] = useState(
9999
renderExplicitSettingValues(setting, context.room.roomId),
100100
);
101101

102-
const onSave = async (): Promise<string> => {
102+
const onSave = async (): Promise<string | undefined> => {
103103
try {
104104
const parsedExplicit = JSON.parse(explicitValue);
105105
const parsedExplicitRoom = JSON.parse(explicitRoomValue);
@@ -232,7 +232,7 @@ const ViewSetting: React.FC<IViewSettingProps> = ({ setting, onEdit, onBack }) =
232232
<div>
233233
{_t("Values at explicit levels:")}
234234
<pre>
235-
<code>{renderExplicitSettingValues(setting, null)}</code>
235+
<code>{renderExplicitSettingValues(setting)}</code>
236236
</pre>
237237
</div>
238238

src/components/views/dialogs/devtools/VerificationExplorer.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
Copyright 2022 Michael Telatynski <[email protected]>
3+
Copyright 2023 The Matrix.org Foundation C.I.C.
34
45
Licensed under the Apache License, Version 2.0 (the "License");
56
you may not use this file except in compliance with the License.
@@ -87,7 +88,7 @@ const VerificationExplorer: Tool = ({ onBack }: IDevtoolsProps) => {
8788

8889
const requests = useTypedEventEmitterState(cli, CryptoEvent.VerificationRequest, () => {
8990
return (
90-
cli.crypto.inRoomVerificationRequests["requestsByRoomId"]?.get(context.room.roomId) ??
91+
cli.crypto?.inRoomVerificationRequests["requestsByRoomId"]?.get(context.room.roomId) ??
9192
new Map<string, VerificationRequest>()
9293
);
9394
});

src/components/views/dialogs/devtools/WidgetExplorer.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
Copyright 2022 Michael Telatynski <[email protected]>
3+
Copyright 2023 The Matrix.org Foundation C.I.C.
34
45
Licensed under the Apache License, Version 2.0 (the "License");
56
you may not use this file except in compliance with the License.
@@ -28,7 +29,7 @@ import { StateEventEditor } from "./RoomState";
2829
const WidgetExplorer: React.FC<IDevtoolsProps> = ({ onBack }) => {
2930
const context = useContext(DevtoolsContext);
3031
const [query, setQuery] = useState("");
31-
const [widget, setWidget] = useState<IApp>(null);
32+
const [widget, setWidget] = useState<IApp | null>(null);
3233

3334
const widgets = useEventEmitterState(WidgetStore.instance, UPDATE_EVENT, () => {
3435
return WidgetStore.instance.getApps(context.room.roomId);
@@ -46,7 +47,7 @@ const WidgetExplorer: React.FC<IDevtoolsProps> = ({ onBack }) => {
4647
).reduce((p, c) => {
4748
p.push(...c);
4849
return p;
49-
}, []);
50+
}, [] as MatrixEvent[]);
5051
const event = allState.find((ev) => ev.getId() === widget.eventId);
5152
if (!event) {
5253
// "should never happen"

0 commit comments

Comments
 (0)