NestedFolderPicker: Allow for excluding certain uids (#72019)

* exclude some uids from nested folder picker so you can't move a folder to itself

* rename excludedUids to excludeUIDs
This commit is contained in:
Ashley Harrison 2023-07-21 13:10:23 +01:00 committed by GitHub
parent 8f464ac66e
commit f078a0bb40
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 8 deletions

View File

@ -33,11 +33,12 @@ interface NestedFolderPickerProps {
// TODO: think properly (and pragmatically) about how to communicate moving to general folder,
// vs removing selection (if possible?)
onChange?: (folder: FolderChange) => void;
excludeUIDs?: string[];
}
const EXCLUDED_KINDS = ['empty-folder' as const, 'dashboard' as const];
export function NestedFolderPicker({ value, onChange }: NestedFolderPickerProps) {
export function NestedFolderPicker({ value, onChange, excludeUIDs = [] }: NestedFolderPickerProps) {
const styles = useStyles2(getStyles);
const dispatch = useDispatch();
const selectedFolder = useGetFolderQuery(value || skipToken);
@ -136,10 +137,18 @@ export function NestedFolderPicker({ value, onChange }: NestedFolderPickerProps)
items: searchResults.items ?? [],
};
return createFlatTree(undefined, searchCollection, childrenCollections, {}, 0, EXCLUDED_KINDS);
return createFlatTree(undefined, searchCollection, childrenCollections, {}, 0, EXCLUDED_KINDS, excludeUIDs);
}
let flatTree = createFlatTree(undefined, rootCollection, childrenCollections, folderOpenState, 0, EXCLUDED_KINDS);
let flatTree = createFlatTree(
undefined,
rootCollection,
childrenCollections,
folderOpenState,
0,
EXCLUDED_KINDS,
excludeUIDs
);
// Increase the level of each item to 'make way' for the fake root Dashboards item
for (const item of flatTree) {
@ -162,7 +171,7 @@ export function NestedFolderPicker({ value, onChange }: NestedFolderPickerProps)
}
return flatTree;
}, [search, searchState.value, rootCollection, childrenCollections, folderOpenState]);
}, [search, searchState.value, rootCollection, childrenCollections, folderOpenState, excludeUIDs]);
const isItemLoaded = useCallback(
(itemIndex: number) => {

View File

@ -61,7 +61,7 @@ export const MoveModal = ({ onConfirm, onDismiss, selectedItems, ...props }: Pro
<Field label={t('browse-dashboards.action.move-modal-field-label', 'Folder name')}>
{config.featureToggles.nestedFolderPicker ? (
<NestedFolderPicker value={moveTarget} onChange={handleFolderChange} />
<NestedFolderPicker value={moveTarget} onChange={handleFolderChange} excludeUIDs={selectedFolders} />
) : (
<FolderPicker allowEmpty onChange={handleFolderChange} />
)}

View File

@ -144,10 +144,11 @@ export function createFlatTree(
childrenByUID: BrowseDashboardsState['childrenByParentUID'],
openFolders: Record<string, boolean>,
level = 0,
excludeKinds: Array<DashboardViewItemWithUIItems['kind'] | UIDashboardViewItem['uiKind']> = []
excludeKinds: Array<DashboardViewItemWithUIItems['kind'] | UIDashboardViewItem['uiKind']> = [],
excludeUIDs: string[] = []
): DashboardsTreeItem[] {
function mapItem(item: DashboardViewItem, parentUID: string | undefined, level: number): DashboardsTreeItem[] {
if (excludeKinds.includes(item.kind)) {
if (excludeKinds.includes(item.kind) || excludeUIDs.includes(item.uid)) {
return [];
}
@ -157,7 +158,8 @@ export function createFlatTree(
childrenByUID,
openFolders,
level + 1,
excludeKinds
excludeKinds,
excludeUIDs
);
const isOpen = Boolean(openFolders[item.uid]);