Skip to content

Commit baaaa83

Browse files
fix(content-explorer): Validate selected item ids after item changes (#4268)
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
1 parent 4beb05d commit baaaa83

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

src/elements/content-explorer/ContentExplorer.tsx

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import cloneDeep from 'lodash/cloneDeep';
55
import debounce from 'lodash/debounce';
66
import flow from 'lodash/flow';
77
import getProp from 'lodash/get';
8+
import isEqual from 'lodash/isEqual';
89
import noop from 'lodash/noop';
910
import throttle from 'lodash/throttle';
1011
import uniqueid from 'lodash/uniqueId';
@@ -424,6 +425,8 @@ class ContentExplorer extends Component<ContentExplorerProps, State> {
424425
metadataTemplate,
425426
};
426427

428+
this.validateSelectedItemIds(metadataQueryCollection.items || []);
429+
427430
// if v2, fetch folder name and add to state
428431
if (metadataQuery?.ancestor_folder_id && isFeatureEnabled(features, 'contentExplorer.metadataViewV2')) {
429432
this.api.getFolderAPI().getFolderFields(
@@ -1010,6 +1013,35 @@ class ContentExplorer extends Component<ContentExplorerProps, State> {
10101013
this.setState({ currentCollection: newCollection, selected: newSelectedItem }, callback);
10111014
}
10121015

1016+
/**
1017+
* Validates selectedItemIds to ensure all selected IDs exist in current items
1018+
* This should be called whenever currentCollection changes
1019+
*
1020+
* @private
1021+
* @param {BoxItem[]} items - current items in the collection
1022+
* @return {void}
1023+
*/
1024+
validateSelectedItemIds = (items: BoxItem[]): void => {
1025+
const { selectedItemIds } = this.state;
1026+
1027+
if (selectedItemIds === 'all' || selectedItemIds.size === 0) {
1028+
// If all/none items are selected, no need to change anything
1029+
return;
1030+
}
1031+
1032+
const validSelectedIds = new Set<string>();
1033+
1034+
items.forEach(item => {
1035+
if (selectedItemIds.has(item.id)) {
1036+
validSelectedIds.add(item.id);
1037+
}
1038+
});
1039+
1040+
if (!isEqual(validSelectedIds, selectedItemIds)) {
1041+
this.setState({ selectedItemIds: validSelectedIds });
1042+
}
1043+
};
1044+
10131045
/**
10141046
* Attempts to generate a thumbnail for the given item and assigns the
10151047
* item its thumbnail url if successful
@@ -1046,6 +1078,9 @@ class ContentExplorer extends Component<ContentExplorerProps, State> {
10461078
const newCollection = { ...currentCollection } as const;
10471079

10481080
newCollection.items = items.map(item => (item.id === newItem.id ? newItem : item));
1081+
1082+
this.validateSelectedItemIds(newCollection.items);
1083+
10491084
this.setState({ currentCollection: newCollection });
10501085
};
10511086

@@ -1697,6 +1732,8 @@ class ContentExplorer extends Component<ContentExplorerProps, State> {
16971732
return clonedItem;
16981733
});
16991734

1735+
this.validateSelectedItemIds(updatedItems);
1736+
17001737
this.setState({
17011738
currentCollection: {
17021739
items: updatedItems,

0 commit comments

Comments
 (0)