Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/elements/content-explorer/ContentExplorer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import cloneDeep from 'lodash/cloneDeep';
import debounce from 'lodash/debounce';
import flow from 'lodash/flow';
import getProp from 'lodash/get';
import isEqual from 'lodash/isEqual';
import noop from 'lodash/noop';
import throttle from 'lodash/throttle';
import uniqueid from 'lodash/uniqueId';
Expand Down Expand Up @@ -424,6 +425,8 @@ class ContentExplorer extends Component<ContentExplorerProps, State> {
metadataTemplate,
};

this.validateSelectedItemIds(metadataQueryCollection.items || []);

// if v2, fetch folder name and add to state
if (metadataQuery?.ancestor_folder_id && isFeatureEnabled(features, 'contentExplorer.metadataViewV2')) {
this.api.getFolderAPI().getFolderFields(
Expand Down Expand Up @@ -1010,6 +1013,35 @@ class ContentExplorer extends Component<ContentExplorerProps, State> {
this.setState({ currentCollection: newCollection, selected: newSelectedItem }, callback);
}

/**
* Validates selectedItemIds to ensure all selected IDs exist in current items
* This should be called whenever currentCollection changes
*
* @private
* @param {BoxItem[]} items - current items in the collection
* @return {void}
*/
validateSelectedItemIds = (items: BoxItem[]): void => {
const { selectedItemIds } = this.state;

if (selectedItemIds === 'all' || selectedItemIds.size === 0) {
// If all/none items are selected, no need to change anything
return;
}

const validSelectedIds = new Set<string>();

items.forEach(item => {
if (selectedItemIds.has(item.id)) {
validSelectedIds.add(item.id);
}
});

if (!isEqual(validSelectedIds, selectedItemIds)) {
this.setState({ selectedItemIds: validSelectedIds });
}
};

/**
* Attempts to generate a thumbnail for the given item and assigns the
* item its thumbnail url if successful
Expand Down Expand Up @@ -1046,6 +1078,9 @@ class ContentExplorer extends Component<ContentExplorerProps, State> {
const newCollection = { ...currentCollection } as const;

newCollection.items = items.map(item => (item.id === newItem.id ? newItem : item));

this.validateSelectedItemIds(newCollection.items);

this.setState({ currentCollection: newCollection });
};

Expand Down Expand Up @@ -1697,6 +1732,8 @@ class ContentExplorer extends Component<ContentExplorerProps, State> {
return clonedItem;
});

this.validateSelectedItemIds(updatedItems);

this.setState({
currentCollection: {
items: updatedItems,
Expand Down