Skip to content

Commit f078a0b

Browse files
NestedFolderPicker: Allow for excluding certain uids (grafana#72019)
* exclude some uids from nested folder picker so you can't move a folder to itself * rename excludedUids to excludeUIDs
1 parent 8f464ac commit f078a0b

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

public/app/core/components/NestedFolderPicker/NestedFolderPicker.tsx

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,12 @@ interface NestedFolderPickerProps {
3333
// TODO: think properly (and pragmatically) about how to communicate moving to general folder,
3434
// vs removing selection (if possible?)
3535
onChange?: (folder: FolderChange) => void;
36+
excludeUIDs?: string[];
3637
}
3738

3839
const EXCLUDED_KINDS = ['empty-folder' as const, 'dashboard' as const];
3940

40-
export function NestedFolderPicker({ value, onChange }: NestedFolderPickerProps) {
41+
export function NestedFolderPicker({ value, onChange, excludeUIDs = [] }: NestedFolderPickerProps) {
4142
const styles = useStyles2(getStyles);
4243
const dispatch = useDispatch();
4344
const selectedFolder = useGetFolderQuery(value || skipToken);
@@ -136,10 +137,18 @@ export function NestedFolderPicker({ value, onChange }: NestedFolderPickerProps)
136137
items: searchResults.items ?? [],
137138
};
138139

139-
return createFlatTree(undefined, searchCollection, childrenCollections, {}, 0, EXCLUDED_KINDS);
140+
return createFlatTree(undefined, searchCollection, childrenCollections, {}, 0, EXCLUDED_KINDS, excludeUIDs);
140141
}
141142

142-
let flatTree = createFlatTree(undefined, rootCollection, childrenCollections, folderOpenState, 0, EXCLUDED_KINDS);
143+
let flatTree = createFlatTree(
144+
undefined,
145+
rootCollection,
146+
childrenCollections,
147+
folderOpenState,
148+
0,
149+
EXCLUDED_KINDS,
150+
excludeUIDs
151+
);
143152

144153
// Increase the level of each item to 'make way' for the fake root Dashboards item
145154
for (const item of flatTree) {
@@ -162,7 +171,7 @@ export function NestedFolderPicker({ value, onChange }: NestedFolderPickerProps)
162171
}
163172

164173
return flatTree;
165-
}, [search, searchState.value, rootCollection, childrenCollections, folderOpenState]);
174+
}, [search, searchState.value, rootCollection, childrenCollections, folderOpenState, excludeUIDs]);
166175

167176
const isItemLoaded = useCallback(
168177
(itemIndex: number) => {

public/app/features/browse-dashboards/components/BrowseActions/MoveModal.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export const MoveModal = ({ onConfirm, onDismiss, selectedItems, ...props }: Pro
6161

6262
<Field label={t('browse-dashboards.action.move-modal-field-label', 'Folder name')}>
6363
{config.featureToggles.nestedFolderPicker ? (
64-
<NestedFolderPicker value={moveTarget} onChange={handleFolderChange} />
64+
<NestedFolderPicker value={moveTarget} onChange={handleFolderChange} excludeUIDs={selectedFolders} />
6565
) : (
6666
<FolderPicker allowEmpty onChange={handleFolderChange} />
6767
)}

public/app/features/browse-dashboards/state/hooks.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,11 @@ export function createFlatTree(
144144
childrenByUID: BrowseDashboardsState['childrenByParentUID'],
145145
openFolders: Record<string, boolean>,
146146
level = 0,
147-
excludeKinds: Array<DashboardViewItemWithUIItems['kind'] | UIDashboardViewItem['uiKind']> = []
147+
excludeKinds: Array<DashboardViewItemWithUIItems['kind'] | UIDashboardViewItem['uiKind']> = [],
148+
excludeUIDs: string[] = []
148149
): DashboardsTreeItem[] {
149150
function mapItem(item: DashboardViewItem, parentUID: string | undefined, level: number): DashboardsTreeItem[] {
150-
if (excludeKinds.includes(item.kind)) {
151+
if (excludeKinds.includes(item.kind) || excludeUIDs.includes(item.uid)) {
151152
return [];
152153
}
153154

@@ -157,7 +158,8 @@ export function createFlatTree(
157158
childrenByUID,
158159
openFolders,
159160
level + 1,
160-
excludeKinds
161+
excludeKinds,
162+
excludeUIDs
161163
);
162164

163165
const isOpen = Boolean(openFolders[item.uid]);

0 commit comments

Comments
 (0)