From 217e5a4011e248afffa7ca4eb598b601cad890b1 Mon Sep 17 00:00:00 2001 From: jfox-box Date: Thu, 4 Sep 2025 14:54:31 -0700 Subject: [PATCH] fix(content-explorer): Validate selected item ids after item changes --- .../content-explorer/ContentExplorer.tsx | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/elements/content-explorer/ContentExplorer.tsx b/src/elements/content-explorer/ContentExplorer.tsx index 067b1b61c2..d326176754 100644 --- a/src/elements/content-explorer/ContentExplorer.tsx +++ b/src/elements/content-explorer/ContentExplorer.tsx @@ -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'; @@ -424,6 +425,8 @@ class ContentExplorer extends Component { 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( @@ -1010,6 +1013,35 @@ class ContentExplorer extends Component { 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(); + + 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 @@ -1046,6 +1078,9 @@ class ContentExplorer extends Component { const newCollection = { ...currentCollection } as const; newCollection.items = items.map(item => (item.id === newItem.id ? newItem : item)); + + this.validateSelectedItemIds(newCollection.items); + this.setState({ currentCollection: newCollection }); }; @@ -1701,6 +1736,8 @@ class ContentExplorer extends Component { return clonedItem; }); + this.validateSelectedItemIds(updatedItems); + this.setState({ currentCollection: { items: updatedItems,