11/*
22Copyright 2022 Michael Telatynski <[email protected] > 3+ Copyright 2023 The Matrix.org Foundation C.I.C.
34
45Licensed under the Apache License, Version 2.0 (the "License");
56you may not use this file except in compliance with the License.
@@ -55,7 +56,7 @@ export const stateKeyField = (defaultValue?: string): IFieldDef => ({
5556const 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>({
7576export 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
142143interface IViewerProps extends Required < IEditorProps > {
143- Editor : React . FC < Required < IEditorProps > > ;
144+ Editor : React . FC < IEditorProps > ;
144145}
145146
146147export const EventViewer : React . FC < IViewerProps > = ( { mxEvent, onBack, Editor } ) => {
@@ -168,7 +169,7 @@ export const EventViewer: React.FC<IViewerProps> = ({ mxEvent, onBack, Editor })
168169const 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
174175export 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 ( ) ;
0 commit comments