DashboardPicker.tsx 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import React, { PureComponent } from 'react';
  2. import { AsyncSelect } from '@grafana/ui';
  3. import { debounce } from 'lodash';
  4. import { getBackendSrv } from 'app/core/services/backend_srv';
  5. import { DashboardSearchHit, DashboardDTO } from 'app/types';
  6. export interface Props {
  7. className?: string;
  8. onSelected: (dashboard: DashboardDTO) => void;
  9. }
  10. export interface State {
  11. isLoading: boolean;
  12. }
  13. export class DashboardPicker extends PureComponent<Props, State> {
  14. debouncedSearch: any;
  15. constructor(props: Props) {
  16. super(props);
  17. this.state = {
  18. isLoading: false,
  19. };
  20. this.debouncedSearch = debounce(this.getDashboards, 300, {
  21. leading: true,
  22. trailing: true,
  23. });
  24. }
  25. getDashboards = (query = '') => {
  26. this.setState({ isLoading: true });
  27. return getBackendSrv()
  28. .search({ type: 'dash-db', query })
  29. .then((result: DashboardSearchHit[]) => {
  30. const dashboards = result.map((item: DashboardSearchHit) => {
  31. return {
  32. id: item.uid,
  33. value: item.id,
  34. label: `${item.folderTitle ? item.folderTitle : 'General'}/${item.title}`,
  35. };
  36. });
  37. this.setState({ isLoading: false });
  38. return dashboards;
  39. });
  40. };
  41. render() {
  42. const { className, onSelected } = this.props;
  43. const { isLoading } = this.state;
  44. return (
  45. <div className="gf-form-inline">
  46. <div className="gf-form">
  47. <AsyncSelect
  48. className={className}
  49. isLoading={isLoading}
  50. isClearable={true}
  51. defaultOptions={true}
  52. loadOptions={this.debouncedSearch}
  53. onChange={onSelected}
  54. placeholder="Select dashboard"
  55. noOptionsMessage={() => 'No dashboards found'}
  56. />
  57. </div>
  58. </div>
  59. );
  60. }
  61. }