@@ -18,7 +18,7 @@ limitations under the License.
1818*/
1919
2020import { IImageInfo , Room } from 'matrix-js-sdk/src/matrix' ;
21- import React , { useCallback , useContext , useEffect , useState } from 'react' ;
21+ import React , { useContext , useState } from 'react' ;
2222
2323import MatrixClientContext from '../../../contexts/MatrixClientContext' ;
2424import { mediaFromMxc } from '../../../customisations/Media' ;
@@ -57,8 +57,9 @@ const PackImage: React.FC<{
5757 image : I2545Image ,
5858 roomId : string ,
5959 threadId : string ,
60- setStickerPickerOpen : ( isStickerPickerOpen : boolean ) => void ;
61- } > = ( { image, roomId, threadId, setStickerPickerOpen } ) => {
60+ setStickerPickerOpen : ( isStickerPickerOpen : boolean ) => void ,
61+ innerRef : React . RefObject < HTMLImageElement > | null
62+ } > = ( { image, roomId, threadId, setStickerPickerOpen, innerRef } ) => {
6263 const cli = useContext ( MatrixClientContext ) ;
6364 const media = mediaFromMxc ( image . url , cli ) ;
6465 // noinspection JSIgnoredPromiseFromCall
@@ -74,7 +75,7 @@ const PackImage: React.FC<{
7475
7576 // eslint-disable-next-line new-cap
7677 return < div className = { cc ( "imageContainer" ) } >
77- < img src = { media . srcHttp } onClick = { onImageClick } alt = { image . body } />
78+ < img ref = { innerRef } src = { media . srcHttp } onClick = { onImageClick } alt = { image . body } />
7879 </ div > ;
7980} ;
8081
@@ -104,13 +105,18 @@ export const MSC2545StickerPicker: React.FC<{
104105 } ) ;
105106 } ) . flat ( 1 ) ;
106107
107- const finished = ( ) => {
108+ const topStickerRef = React . createRef < HTMLImageElement > ( ) ;
109+
110+ const finished = ( send : boolean ) => ( ) => {
108111 if ( isStickerPickerOpen ) {
112+ if ( send ) topStickerRef . current . click ( ) ;
109113 setStickerPickerOpen ( false ) ;
114+ setSearchFilter ( "" ) ;
110115 }
111116 } ;
112117
113- const renderedPacks = packs . map ( ( { pack, packName } ) => {
118+
119+ const renderedPacks = packs . map ( ( { pack, packName } , packIdx ) => {
114120 const lcFilter = searchFilter . toLowerCase ( ) . trim ( ) ; // filter is case insensitive
115121 const images = Object . values ( pack . images )
116122 . filter ( im => im . body . toLowerCase ( ) . includes ( lcFilter ) ) ;
@@ -121,6 +127,7 @@ export const MSC2545StickerPicker: React.FC<{
121127 < h3 className = { cc ( "label" ) } > { pack . pack . display_name } </ h3 >
122128 < div className = { cc ( "grid" ) } >
123129 { images . map ( ( im , idx ) => < PackImage
130+ innerRef = { ( ! packIdx && ! idx ) ? topStickerRef : null }
124131 key = { idx }
125132 image = { im }
126133 roomId = { room . roomId }
@@ -135,7 +142,7 @@ export const MSC2545StickerPicker: React.FC<{
135142 chevronFace = { ChevronFace . Bottom }
136143 menuWidth = { 300 }
137144 menuHeight = { 500 }
138- onFinished = { finished }
145+ onFinished = { finished ( false ) }
139146 menuPaddingTop = { 0 }
140147 menuPaddingLeft = { 0 }
141148 menuPaddingRight = { 0 }
@@ -144,10 +151,10 @@ export const MSC2545StickerPicker: React.FC<{
144151 >
145152 < GenericElementContextMenu element = {
146153 < ScrollPanel startAtBottom = { false } className = { cc ( "root" ) } >
147- < Search query = { searchFilter } onChange = { setSearchFilter } onEnter = { ( ) => { } } />
154+ < Search query = { searchFilter } onChange = { setSearchFilter } onEnter = { finished ( true ) } />
148155 { renderedPacks }
149156 </ ScrollPanel > }
150- onResize = { finished } />
157+ onResize = { finished ( false ) } />
151158 </ ContextMenu > ;
152159} ;
153160
0 commit comments