Apply strictNullChecks to src/components/views/dialogs/devtools/* (#10391)

This commit is contained in:
Kerry 2023-03-17 12:58:41 +13:00 committed by GitHub
parent 5211b628d6
commit b2c046689e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 34 additions and 26 deletions

View file

@ -1,5 +1,6 @@
/* /*
Copyright 2022 Michael Telatynski <7t3chguy@gmail.com> Copyright 2022 Michael Telatynski <7t3chguy@gmail.com>
Copyright 2023 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@ -29,7 +30,7 @@ export const AccountDataEventEditor: React.FC<IEditorProps> = ({ mxEvent, onBack
const fields = useMemo(() => [eventTypeField(mxEvent?.getType())], [mxEvent]); const fields = useMemo(() => [eventTypeField(mxEvent?.getType())], [mxEvent]);
const onSend = async ([eventType]: string[], content?: IContent): Promise<void> => { const onSend = async ([eventType]: string[], content?: IContent): Promise<void> => {
await cli.setAccountData(eventType, content); await cli.setAccountData(eventType, content || {});
}; };
const defaultContent = mxEvent ? stringify(mxEvent.getContent()) : undefined; const defaultContent = mxEvent ? stringify(mxEvent.getContent()) : undefined;
@ -43,7 +44,7 @@ export const RoomAccountDataEventEditor: React.FC<IEditorProps> = ({ mxEvent, on
const fields = useMemo(() => [eventTypeField(mxEvent?.getType())], [mxEvent]); const fields = useMemo(() => [eventTypeField(mxEvent?.getType())], [mxEvent]);
const onSend = async ([eventType]: string[], content?: IContent): Promise<void> => { const onSend = async ([eventType]: string[], content?: IContent): Promise<void> => {
await cli.setRoomAccountData(context.room.roomId, eventType, content); await cli.setRoomAccountData(context.room.roomId, eventType, content || {});
}; };
const defaultContent = mxEvent ? stringify(mxEvent.getContent()) : undefined; const defaultContent = mxEvent ? stringify(mxEvent.getContent()) : undefined;
@ -58,7 +59,7 @@ interface IProps extends IDevtoolsProps {
const BaseAccountDataExplorer: React.FC<IProps> = ({ events, Editor, actionLabel, onBack, setTool }) => { const BaseAccountDataExplorer: React.FC<IProps> = ({ events, Editor, actionLabel, onBack, setTool }) => {
const [query, setQuery] = useState(""); const [query, setQuery] = useState("");
const [event, setEvent] = useState<MatrixEvent>(null); const [event, setEvent] = useState<MatrixEvent | null>(null);
if (event) { if (event) {
const onBack = (): void => { const onBack = (): void => {

View file

@ -1,5 +1,6 @@
/* /*
Copyright 2022 Michael Telatynski <7t3chguy@gmail.com> Copyright 2022 Michael Telatynski <7t3chguy@gmail.com>
Copyright 2023 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@ -38,7 +39,7 @@ interface IProps extends IMinProps {
} }
const BaseTool: React.FC<XOR<IMinProps, IProps>> = ({ className, actionLabel, onBack, onAction, children }) => { const BaseTool: React.FC<XOR<IMinProps, IProps>> = ({ className, actionLabel, onBack, onAction, children }) => {
const [message, setMessage] = useState<string>(null); const [message, setMessage] = useState<string | null>(null);
const onBackClick = (): void => { const onBackClick = (): void => {
if (message) { if (message) {
@ -48,7 +49,7 @@ const BaseTool: React.FC<XOR<IMinProps, IProps>> = ({ className, actionLabel, on
} }
}; };
let actionButton: JSX.Element; let actionButton: ReactNode = null;
if (message) { if (message) {
children = message; children = message;
} else if (onAction) { } else if (onAction) {

View file

@ -1,5 +1,6 @@
/* /*
Copyright 2022 Michael Telatynski <7t3chguy@gmail.com> Copyright 2022 Michael Telatynski <7t3chguy@gmail.com>
Copyright 2023 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@ -55,7 +56,7 @@ export const stateKeyField = (defaultValue?: string): IFieldDef => ({
const validateEventContent = withValidation<any, Error | undefined>({ const validateEventContent = withValidation<any, Error | undefined>({
deriveData({ value }) { deriveData({ value }) {
try { try {
JSON.parse(value); JSON.parse(value!);
} catch (e) { } catch (e) {
return e; return e;
} }
@ -75,7 +76,7 @@ const validateEventContent = withValidation<any, Error | undefined>({
export const EventEditor: React.FC<IEventEditorProps> = ({ fieldDefs, defaultContent = "{\n\n}", onSend, onBack }) => { export const EventEditor: React.FC<IEventEditorProps> = ({ fieldDefs, defaultContent = "{\n\n}", onSend, onBack }) => {
const [fieldData, setFieldData] = useState<string[]>(fieldDefs.map((def) => def.default ?? "")); const [fieldData, setFieldData] = useState<string[]>(fieldDefs.map((def) => def.default ?? ""));
const [content, setContent] = useState<string>(defaultContent); const [content, setContent] = useState<string>(defaultContent);
const contentField = useRef<Field>(); const contentField = useRef<Field>(null);
const fields = fieldDefs.map((def, i) => ( const fields = fieldDefs.map((def, i) => (
<Field <Field
@ -96,12 +97,12 @@ export const EventEditor: React.FC<IEventEditorProps> = ({ fieldDefs, defaultCon
/> />
)); ));
const onAction = async (): Promise<string> => { const onAction = async (): Promise<string | undefined> => {
const valid = await contentField.current.validate({}); const valid = contentField.current ? await contentField.current.validate({}) : false;
if (!valid) { if (!valid) {
contentField.current.focus(); contentField.current?.focus();
contentField.current.validate({ focused: true }); contentField.current?.validate({ focused: true });
return; return;
} }
@ -140,7 +141,7 @@ export interface IEditorProps extends Pick<IDevtoolsProps, "onBack"> {
} }
interface IViewerProps extends Required<IEditorProps> { interface IViewerProps extends Required<IEditorProps> {
Editor: React.FC<Required<IEditorProps>>; Editor: React.FC<IEditorProps>;
} }
export const EventViewer: React.FC<IViewerProps> = ({ mxEvent, onBack, Editor }) => { export const EventViewer: React.FC<IViewerProps> = ({ mxEvent, onBack, Editor }) => {
@ -168,7 +169,7 @@ export const EventViewer: React.FC<IViewerProps> = ({ mxEvent, onBack, Editor })
const getBaseEventId = (baseEvent: MatrixEvent): string => { const getBaseEventId = (baseEvent: MatrixEvent): string => {
// show the replacing event, not the original, if it is an edit // show the replacing event, not the original, if it is an edit
const mxEvent = baseEvent.replacingEvent() ?? baseEvent; const mxEvent = baseEvent.replacingEvent() ?? baseEvent;
return mxEvent.getWireContent()["m.relates_to"]?.event_id ?? baseEvent.getId(); return mxEvent.getWireContent()["m.relates_to"]?.event_id ?? baseEvent.getId()!;
}; };
export const TimelineEventEditor: React.FC<IEditorProps> = ({ mxEvent, onBack }) => { export const TimelineEventEditor: React.FC<IEditorProps> = ({ mxEvent, onBack }) => {
@ -178,10 +179,10 @@ export const TimelineEventEditor: React.FC<IEditorProps> = ({ mxEvent, onBack })
const fields = useMemo(() => [eventTypeField(mxEvent?.getType())], [mxEvent]); const fields = useMemo(() => [eventTypeField(mxEvent?.getType())], [mxEvent]);
const onSend = ([eventType]: string[], content?: IContent): Promise<unknown> => { const onSend = ([eventType]: string[], content?: IContent): Promise<unknown> => {
return cli.sendEvent(context.room.roomId, eventType, content); return cli.sendEvent(context.room.roomId, eventType, content || {});
}; };
let defaultContent: string; let defaultContent = "";
if (mxEvent) { if (mxEvent) {
const originalContent = mxEvent.getContent(); const originalContent = mxEvent.getContent();

View file

@ -1,5 +1,6 @@
/* /*
Copyright 2022 Michael Telatynski <7t3chguy@gmail.com> Copyright 2022 Michael Telatynski <7t3chguy@gmail.com>
Copyright 2023 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@ -37,7 +38,7 @@ const FilteredList: React.FC<IProps> = ({ children, query, onChange }) => {
let filteredChildren = children; let filteredChildren = children;
if (query) { if (query) {
const lcQuery = query.toLowerCase(); const lcQuery = query.toLowerCase();
filteredChildren = children.filter((child) => child.key.toString().toLowerCase().includes(lcQuery)); filteredChildren = children.filter((child) => child.key?.toString().toLowerCase().includes(lcQuery));
} }
setFilteredChildren(filteredChildren); setFilteredChildren(filteredChildren);
setTruncateAt(INITIAL_LOAD_TILES); setTruncateAt(INITIAL_LOAD_TILES);

View file

@ -1,5 +1,6 @@
/* /*
Copyright 2022 Michael Telatynski <7t3chguy@gmail.com> Copyright 2022 Michael Telatynski <7t3chguy@gmail.com>
Copyright 2023 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.

View file

@ -1,5 +1,6 @@
/* /*
Copyright 2022 Michael Telatynski <7t3chguy@gmail.com> Copyright 2022 Michael Telatynski <7t3chguy@gmail.com>
Copyright 2023 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@ -27,7 +28,7 @@ const ServersInRoom: React.FC<IDevtoolsProps> = ({ onBack }) => {
const servers: Record<string, number> = {}; const servers: Record<string, number> = {};
context.room.currentState.getStateEvents(EventType.RoomMember).forEach((ev) => { context.room.currentState.getStateEvents(EventType.RoomMember).forEach((ev) => {
if (ev.getContent().membership !== "join") return; // only count joined users if (ev.getContent().membership !== "join") return; // only count joined users
const server = ev.getSender().split(":")[1]; const server = ev.getSender()!.split(":")[1];
servers[server] = (servers[server] ?? 0) + 1; servers[server] = (servers[server] ?? 0) + 1;
}); });
return servers; return servers;

View file

@ -1,6 +1,6 @@
/* /*
Copyright 2022 Michael Telatynski <7t3chguy@gmail.com> Copyright 2022 Michael Telatynski <7t3chguy@gmail.com>
Copyright 2018-2021 The Matrix.org Foundation C.I.C. Copyright 2018-2023 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@ -27,7 +27,7 @@ import { SETTINGS } from "../../../../settings/Settings";
import Field from "../../elements/Field"; import Field from "../../elements/Field";
const SettingExplorer: React.FC<IDevtoolsProps> = ({ onBack }) => { const SettingExplorer: React.FC<IDevtoolsProps> = ({ onBack }) => {
const [setting, setSetting] = useState<string>(null); const [setting, setSetting] = useState<string | null>(null);
const [editing, setEditing] = useState(false); const [editing, setEditing] = useState(false);
if (setting && editing) { if (setting && editing) {
@ -73,7 +73,7 @@ const CanEditLevelField: React.FC<ICanEditLevelFieldProps> = ({ setting, roomId,
); );
}; };
function renderExplicitSettingValues(setting: string, roomId: string): string { function renderExplicitSettingValues(setting: string, roomId?: string): string {
const vals: Record<string, number | null> = {}; const vals: Record<string, number | null> = {};
for (const level of LEVEL_ORDER) { for (const level of LEVEL_ORDER) {
try { try {
@ -94,12 +94,12 @@ interface IEditSettingProps extends Pick<IDevtoolsProps, "onBack"> {
const EditSetting: React.FC<IEditSettingProps> = ({ setting, onBack }) => { const EditSetting: React.FC<IEditSettingProps> = ({ setting, onBack }) => {
const context = useContext(DevtoolsContext); const context = useContext(DevtoolsContext);
const [explicitValue, setExplicitValue] = useState(renderExplicitSettingValues(setting, null)); const [explicitValue, setExplicitValue] = useState(renderExplicitSettingValues(setting));
const [explicitRoomValue, setExplicitRoomValue] = useState( const [explicitRoomValue, setExplicitRoomValue] = useState(
renderExplicitSettingValues(setting, context.room.roomId), renderExplicitSettingValues(setting, context.room.roomId),
); );
const onSave = async (): Promise<string> => { const onSave = async (): Promise<string | undefined> => {
try { try {
const parsedExplicit = JSON.parse(explicitValue); const parsedExplicit = JSON.parse(explicitValue);
const parsedExplicitRoom = JSON.parse(explicitRoomValue); const parsedExplicitRoom = JSON.parse(explicitRoomValue);
@ -232,7 +232,7 @@ const ViewSetting: React.FC<IViewSettingProps> = ({ setting, onEdit, onBack }) =
<div> <div>
{_t("Values at explicit levels:")} {_t("Values at explicit levels:")}
<pre> <pre>
<code>{renderExplicitSettingValues(setting, null)}</code> <code>{renderExplicitSettingValues(setting)}</code>
</pre> </pre>
</div> </div>

View file

@ -1,5 +1,6 @@
/* /*
Copyright 2022 Michael Telatynski <7t3chguy@gmail.com> Copyright 2022 Michael Telatynski <7t3chguy@gmail.com>
Copyright 2023 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@ -87,7 +88,7 @@ const VerificationExplorer: Tool = ({ onBack }: IDevtoolsProps) => {
const requests = useTypedEventEmitterState(cli, CryptoEvent.VerificationRequest, () => { const requests = useTypedEventEmitterState(cli, CryptoEvent.VerificationRequest, () => {
return ( return (
cli.crypto.inRoomVerificationRequests["requestsByRoomId"]?.get(context.room.roomId) ?? cli.crypto?.inRoomVerificationRequests["requestsByRoomId"]?.get(context.room.roomId) ??
new Map<string, VerificationRequest>() new Map<string, VerificationRequest>()
); );
}); });

View file

@ -1,5 +1,6 @@
/* /*
Copyright 2022 Michael Telatynski <7t3chguy@gmail.com> Copyright 2022 Michael Telatynski <7t3chguy@gmail.com>
Copyright 2023 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@ -28,7 +29,7 @@ import { StateEventEditor } from "./RoomState";
const WidgetExplorer: React.FC<IDevtoolsProps> = ({ onBack }) => { const WidgetExplorer: React.FC<IDevtoolsProps> = ({ onBack }) => {
const context = useContext(DevtoolsContext); const context = useContext(DevtoolsContext);
const [query, setQuery] = useState(""); const [query, setQuery] = useState("");
const [widget, setWidget] = useState<IApp>(null); const [widget, setWidget] = useState<IApp | null>(null);
const widgets = useEventEmitterState(WidgetStore.instance, UPDATE_EVENT, () => { const widgets = useEventEmitterState(WidgetStore.instance, UPDATE_EVENT, () => {
return WidgetStore.instance.getApps(context.room.roomId); return WidgetStore.instance.getApps(context.room.roomId);
@ -46,7 +47,7 @@ const WidgetExplorer: React.FC<IDevtoolsProps> = ({ onBack }) => {
).reduce((p, c) => { ).reduce((p, c) => {
p.push(...c); p.push(...c);
return p; return p;
}, []); }, [] as MatrixEvent[]);
const event = allState.find((ev) => ev.getId() === widget.eventId); const event = allState.find((ev) => ev.getId() === widget.eventId);
if (!event) { if (!event) {
// "should never happen" // "should never happen"