DataSourceDashboards.tsx 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import React, { PureComponent } from 'react';
  2. import { connect, ConnectedProps } from 'react-redux';
  3. import Page from 'app/core/components/Page/Page';
  4. import { GrafanaRouteComponentProps } from 'app/core/navigation/types';
  5. import { getNavModel } from 'app/core/selectors/navModel';
  6. import { PluginDashboard, StoreState } from 'app/types';
  7. import { importDashboard, removeDashboard } from '../dashboard/state/actions';
  8. import { loadPluginDashboards } from '../plugins/admin/state/actions';
  9. import DashboardTable from './DashboardsTable';
  10. import { loadDataSource } from './state/actions';
  11. import { getDataSource } from './state/selectors';
  12. export interface OwnProps extends GrafanaRouteComponentProps<{ uid: string }> {}
  13. function mapStateToProps(state: StoreState, props: OwnProps) {
  14. const dataSourceId = props.match.params.uid;
  15. return {
  16. navModel: getNavModel(state.navIndex, `datasource-dashboards-${dataSourceId}`),
  17. dashboards: state.plugins.dashboards,
  18. dataSource: getDataSource(state.dataSources, dataSourceId),
  19. isLoading: state.plugins.isLoadingPluginDashboards,
  20. dataSourceId,
  21. };
  22. }
  23. const mapDispatchToProps = {
  24. importDashboard,
  25. loadDataSource,
  26. loadPluginDashboards,
  27. removeDashboard,
  28. };
  29. const connector = connect(mapStateToProps, mapDispatchToProps);
  30. export type Props = OwnProps & ConnectedProps<typeof connector>;
  31. export class DataSourceDashboards extends PureComponent<Props> {
  32. async componentDidMount() {
  33. const { loadDataSource, dataSourceId } = this.props;
  34. await loadDataSource(dataSourceId);
  35. this.props.loadPluginDashboards();
  36. }
  37. onImport = (dashboard: PluginDashboard, overwrite: boolean) => {
  38. const { dataSource, importDashboard } = this.props;
  39. const data: any = {
  40. pluginId: dashboard.pluginId,
  41. path: dashboard.path,
  42. overwrite,
  43. inputs: [],
  44. };
  45. if (dataSource) {
  46. data.inputs.push({
  47. name: '*',
  48. type: 'datasource',
  49. pluginId: dataSource.type,
  50. value: dataSource.name,
  51. });
  52. }
  53. importDashboard(data, dashboard.title);
  54. };
  55. onRemove = (dashboard: PluginDashboard) => {
  56. this.props.removeDashboard(dashboard.uid);
  57. };
  58. render() {
  59. const { dashboards, navModel, isLoading } = this.props;
  60. return (
  61. <Page navModel={navModel}>
  62. <Page.Contents isLoading={isLoading}>
  63. <DashboardTable
  64. dashboards={dashboards}
  65. onImport={(dashboard, overwrite) => this.onImport(dashboard, overwrite)}
  66. onRemove={(dashboard) => this.onRemove(dashboard)}
  67. />
  68. </Page.Contents>
  69. </Page>
  70. );
  71. }
  72. }
  73. export default connector(DataSourceDashboards);