mirror of
https://github.com/grafana/grafana.git
synced 2025-02-16 18:34:52 -06:00
* Search: add search wrapper * Search: add DashboardSearch.tsx * Search: enable search * Search: update types * Search: useReducer for saving search results * Search: use default query * Search: add toggle custom action * Search: add onQueryChange * Search: debounce search * Search: pas dispatch as a prop * Search: add tag filter * Search: Fix types * Search: revert changes * Search: close overlay on esc * Search: enable tag filtering * Search: clear query * Search: add autofocus to search field * Search: Rename close to closeSearch * Search: Add no results message * Search: Add loading state * Search: Remove Select from Forms namespace * Remove Add selectedIndex * Remove Add getFlattenedSections * Remove Enable selecting items * Search: add hasId * Search: preselect first item * Search: Add utils tests * Search: Fix moving selection down * Search: Add findSelected * Search: Add type to section * Search: Handle Enter key press on item highlight * Search: Move reducer et al. to separate files * Search: Remove redundant render check * Search: Close overlay on Esc and ArrowLeft press * Search: Add close button * Search: Document utils * Search: use Icon for remove icon * Search: Add DashboardSearch.test.tsx * Search: Move test data to a separate file * Search: Finalise DashboardSearch.test.tsx * Add search reducer tests * Search: Add search results loading indicator * Search: Remove inline function * Search: Do not mutate item * Search: Tweak utils * Search: Do not clear query on tag clear * Search: Fix folder:current search * Search: Fix results scroll * Search: Update tests * Search: Close overlay on cog icon click * Add mobile styles for close button * Search: Use CustomScrollbar * Search: Memoize TagList.tsx * Search: Fix type errors * Search: More strictNullChecks fixes * Search: Add ManageDashboards.tsx * Search: Add mergeReducers * Search: Use mergeReducers * Search: remove default state from reducers * Search: Fix recent and starred icons * Search: Enable search * Search: Add markup * Search: Separate manageDashboardsReducer * Search: Add DashboardActions.tsx * Use new Select for TagFilter * Search: Use TagFilter for search filters * Search: Use TagList * Search: Add toggleSection * Search: Add more actions * Search add manageDashboards.test.ts * Search: Add getCheckedUids * Search: Add modify and toggle checked actions * Search: Update tests * Search: Update component template * Search: Enable section toggle * Search: Derive canMove and canDelete * Search: Handle delete items * Search: Fix tests * Search: Enable toggle items * Search: Add confirm modal subtitle * Search: Use theme vars * Search: Add getCheckedDashboardsUids * Search: Add MoveToFolderModal * Search: Enable moving dashboards * Search: Fix strict null checks errors * Search: Fix strict null checks errors[2] * Search: Enable filters * Search: Add useSearchQuery.ts * Search: Toggle items when toggling all * Search: Update useSearchQuery to accept custom params * Search: Add useSearchQuery to dashboard search * Search: use SearchField for manage dashboards * Search: Remove event param from query change * Search: Add base search hooks * Search: refactor useSearch to accept reducer * Search: use useDashboardSearch hook * Search: Fix useSearchQuery params * Search: Enable folder search * Search: Update tests * Search: Pass the props to manage-dashboards * Search: Add search filters margin * Search: Remove search-field-wrapper class and hide logic for it * Search: Adjust SearchField styles * Search: Move search-results-container inside SearchResults * Search: Fix type errors * Search: Add EmptyListCTA * Search: Update move message * Search: Cleanup * Search: Add todo * Search: Fix action type * Search: Use React wrapper vs FolderDashboardsCtrl and DashboardListCtrl * Search: DashboardList => DashboardListPage * Search: Remove ManageDashboards from angular_wrappers * Minor style tweaks * Search: Use LinkButton Co-authored-by: Torkel Ödegaard <torkel@grafana.com>
187 lines
5.4 KiB
TypeScript
187 lines
5.4 KiB
TypeScript
import React, { PureComponent } from 'react';
|
|
import { AsyncSelect } from '@grafana/ui';
|
|
import { AppEvents, SelectableValue } from '@grafana/data';
|
|
import { debounce } from 'lodash';
|
|
import appEvents from '../../app_events';
|
|
import { getBackendSrv } from '@grafana/runtime';
|
|
import { contextSrv } from 'app/core/services/context_srv';
|
|
import { DashboardSearchHit } from '../../../types';
|
|
|
|
export interface Props {
|
|
onChange: ($folder: { title: string; id: number }) => void;
|
|
enableCreateNew?: boolean;
|
|
rootName?: string;
|
|
enableReset?: boolean;
|
|
dashboardId?: any;
|
|
initialTitle?: string;
|
|
initialFolderId?: number;
|
|
useNewForms?: boolean;
|
|
}
|
|
|
|
interface State {
|
|
folder: SelectableValue<number>;
|
|
}
|
|
|
|
export class FolderPicker extends PureComponent<Props, State> {
|
|
debouncedSearch: any;
|
|
|
|
constructor(props: Props) {
|
|
super(props);
|
|
|
|
this.state = {
|
|
folder: {},
|
|
};
|
|
|
|
this.debouncedSearch = debounce(this.getOptions, 300, {
|
|
leading: true,
|
|
trailing: true,
|
|
});
|
|
}
|
|
|
|
static defaultProps = {
|
|
rootName: 'General',
|
|
enableReset: false,
|
|
initialTitle: '',
|
|
enableCreateNew: false,
|
|
useNewForms: false,
|
|
};
|
|
|
|
componentDidMount = async () => {
|
|
await this.loadInitialValue();
|
|
};
|
|
|
|
getOptions = async (query: string) => {
|
|
const { rootName, enableReset, initialTitle } = this.props;
|
|
const params = {
|
|
query,
|
|
type: 'dash-folder',
|
|
permission: 'Edit',
|
|
};
|
|
|
|
// TODO: move search to BackendSrv interface
|
|
// @ts-ignore
|
|
const searchHits = (await getBackendSrv().search(params)) as DashboardSearchHit[];
|
|
const options: Array<SelectableValue<number>> = searchHits.map(hit => ({ label: hit.title, value: hit.id }));
|
|
if (contextSrv.isEditor && rootName?.toLowerCase().startsWith(query.toLowerCase())) {
|
|
options.unshift({ label: rootName, value: 0 });
|
|
}
|
|
|
|
if (enableReset && query === '' && initialTitle !== '') {
|
|
options.unshift({ label: initialTitle, value: undefined });
|
|
}
|
|
|
|
return options;
|
|
};
|
|
|
|
onFolderChange = (newFolder: SelectableValue<number>) => {
|
|
if (!newFolder) {
|
|
newFolder = { value: 0, label: this.props.rootName };
|
|
}
|
|
|
|
this.setState(
|
|
{
|
|
folder: newFolder,
|
|
},
|
|
() => this.props.onChange({ id: newFolder.value!, title: newFolder.label! })
|
|
);
|
|
};
|
|
|
|
createNewFolder = async (folderName: string) => {
|
|
// @ts-ignore
|
|
const newFolder = await getBackendSrv().createFolder({ title: folderName });
|
|
let folder = { value: -1, label: 'Not created' };
|
|
if (newFolder.id > -1) {
|
|
appEvents.emit(AppEvents.alertSuccess, ['Folder Created', 'OK']);
|
|
folder = { value: newFolder.id, label: newFolder.title };
|
|
await this.onFolderChange(folder);
|
|
} else {
|
|
appEvents.emit(AppEvents.alertError, ['Folder could not be created']);
|
|
}
|
|
|
|
return folder;
|
|
};
|
|
|
|
private loadInitialValue = async () => {
|
|
const { initialTitle, rootName, initialFolderId, enableReset, dashboardId } = this.props;
|
|
const resetFolder: SelectableValue<number> = { label: initialTitle, value: undefined };
|
|
const rootFolder: SelectableValue<number> = { label: rootName, value: 0 };
|
|
|
|
const options = await this.getOptions('');
|
|
|
|
let folder: SelectableValue<number> = { value: -1 };
|
|
|
|
if (initialFolderId !== undefined && initialFolderId !== null && initialFolderId > -1) {
|
|
folder = options.find(option => option.value === initialFolderId) || { value: -1 };
|
|
} else if (enableReset && initialTitle) {
|
|
folder = resetFolder;
|
|
}
|
|
|
|
if (folder.value === -1) {
|
|
if (contextSrv.isEditor) {
|
|
folder = rootFolder;
|
|
} else {
|
|
// We shouldn't assign a random folder without the user actively choosing it on a persisted dashboard
|
|
const isPersistedDashBoard = !!dashboardId;
|
|
if (isPersistedDashBoard) {
|
|
folder = resetFolder;
|
|
} else {
|
|
folder = options.length > 0 ? options[0] : resetFolder;
|
|
}
|
|
}
|
|
}
|
|
|
|
this.setState(
|
|
{
|
|
folder,
|
|
},
|
|
() => {
|
|
// if this is not the same as our initial value notify parent
|
|
if (folder.value !== initialFolderId) {
|
|
this.props.onChange({ id: folder.value!, title: folder.text });
|
|
}
|
|
}
|
|
);
|
|
};
|
|
|
|
render() {
|
|
const { folder } = this.state;
|
|
const { enableCreateNew, useNewForms } = this.props;
|
|
|
|
return (
|
|
<>
|
|
{useNewForms && (
|
|
<AsyncSelect
|
|
loadingMessage="Loading folders..."
|
|
defaultOptions
|
|
defaultValue={folder}
|
|
value={folder}
|
|
allowCustomValue={enableCreateNew}
|
|
loadOptions={this.debouncedSearch}
|
|
onChange={this.onFolderChange}
|
|
onCreateOption={this.createNewFolder}
|
|
menuPosition="fixed"
|
|
/>
|
|
)}
|
|
{!useNewForms && (
|
|
<div className="gf-form-inline">
|
|
<div className="gf-form">
|
|
<label className="gf-form-label width-7">Folder</label>
|
|
<AsyncSelect
|
|
loadingMessage="Loading folders..."
|
|
defaultOptions
|
|
defaultValue={folder}
|
|
value={folder}
|
|
className={'width-20'}
|
|
allowCustomValue={enableCreateNew}
|
|
loadOptions={this.debouncedSearch}
|
|
onChange={this.onFolderChange}
|
|
onCreateOption={this.createNewFolder}
|
|
/>
|
|
</div>
|
|
</div>
|
|
)}
|
|
</>
|
|
);
|
|
}
|
|
}
|