@@ -68,13 +68,15 @@ class ThumbnailView extends Component {
6868 this . fileRefs . current = [ ] ;
6969 // Explicitly focus on the grid itself to announce changes to page etc.
7070 this . gridRef . current . focus ( ) ;
71+ const newState = { forceResetRefs : false } ;
7172 if ( this . state . focusedItem ) {
72- this . setState ( { focusedItem : null , forceResetRefs : false } ) ;
73+ newState . focusedItem = null ;
7374 }
75+ this . setState ( newState ) ;
7476 return ;
7577 }
7678 // If we removed a file or folder, we have some tidy-up to do.
77- if ( this . props . files . length < oldProps . files . length ) {
79+ if ( this . props . totalCount < oldProps . totalCount || this . props . files . length < oldProps . files . length ) {
7880 // If we have less files/folders than we used to, make sure to remove the extra refs
7981 // Note that all the refs are correct, there's just some extra old ones at the end of
8082 // the arrays.
@@ -127,20 +129,22 @@ class ThumbnailView extends Component {
127129 }
128130
129131 // If we added a folder or file, move focus to the first new item
130- if ( this . props . files . length > oldProps . files . length ) {
132+ if ( this . props . totalCount > oldProps . totalCount || this . props . files . length > oldProps . files . length ) {
131133 const newItems = this . props . files . filter ( ( item ) => ! oldProps . files . find ( ( oldItem ) => this . focusItemsAreIdentical ( item , oldItem ) ) ) ;
132134 const newItem = newItems [ 0 ] ;
133- this . setState ( {
134- allowedToSetFocus : true ,
135- focusedItem : this . getFocusDataFromItem ( newItem )
136- } ) ;
135+ if ( newItem ) {
136+ this . setState ( {
137+ allowedToSetFocus : true ,
138+ focusedItem : this . getFocusDataFromItem ( newItem )
139+ } ) ;
140+ }
137141 }
138142
139143 // For successful uploads, when the file gets assigned a new ID we need to capture that.
140144 // There's a brief period where the file has both a queuedID and a regular ID - and then it
141145 // drops the queuedID. We need to make sure we catch the ID so we can retain focus on the
142146 // item.
143- if ( this . state . focusedItem ?. queuedId && ! this . state . focusedItem . id && this . props . files . length === oldProps . files . length ) {
147+ if ( this . state . focusedItem ?. queuedId && ! this . state . focusedItem . id && this . props . totalCount === oldProps . totalCount ) {
144148 const current = this . props . files . find ( ( item ) => this . focusItemsAreIdentical ( this . state . focusedItem , item ) ) ;
145149 if ( current && current . id !== this . state . focusedItem . id ) {
146150 const newState = { focusedItem : this . getFocusDataFromItem ( current ) } ;
@@ -193,6 +197,9 @@ class ThumbnailView extends Component {
193197 * @param {object } item A file or folder from props.files
194198 */
195199 getFocusDataFromItem ( item ) {
200+ if ( ! item ) {
201+ return null ;
202+ }
196203 const itemRefs = item . type === 'folder' ? this . folderRefs . current : this . fileRefs . current ;
197204 const index = itemRefs . findIndex ( ( itemRef ) => this . focusItemsAreIdentical ( item , itemRef ) ) ;
198205 return { id : item . id , queuedId : item . queuedId , type : item . type , index } ;
0 commit comments