DataSourcesListPage.tsx 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import React, { PureComponent } from 'react';
  2. import { connect, ConnectedProps } from 'react-redux';
  3. import { IconName } from '@grafana/ui';
  4. import EmptyListCTA from 'app/core/components/EmptyListCTA/EmptyListCTA';
  5. import Page from 'app/core/components/Page/Page';
  6. import PageActionBar from 'app/core/components/PageActionBar/PageActionBar';
  7. import { contextSrv } from 'app/core/core';
  8. import { getNavModel } from 'app/core/selectors/navModel';
  9. import { StoreState, AccessControlAction } from 'app/types';
  10. import DataSourcesList from './DataSourcesList';
  11. import { loadDataSources } from './state/actions';
  12. import { setDataSourcesLayoutMode, setDataSourcesSearchQuery } from './state/reducers';
  13. import {
  14. getDataSources,
  15. getDataSourcesCount,
  16. getDataSourcesLayoutMode,
  17. getDataSourcesSearchQuery,
  18. } from './state/selectors';
  19. function mapStateToProps(state: StoreState) {
  20. return {
  21. navModel: getNavModel(state.navIndex, 'datasources'),
  22. dataSources: getDataSources(state.dataSources),
  23. layoutMode: getDataSourcesLayoutMode(state.dataSources),
  24. dataSourcesCount: getDataSourcesCount(state.dataSources),
  25. searchQuery: getDataSourcesSearchQuery(state.dataSources),
  26. hasFetched: state.dataSources.hasFetched,
  27. };
  28. }
  29. const mapDispatchToProps = {
  30. loadDataSources,
  31. setDataSourcesSearchQuery,
  32. setDataSourcesLayoutMode,
  33. };
  34. const connector = connect(mapStateToProps, mapDispatchToProps);
  35. export type Props = ConnectedProps<typeof connector>;
  36. const emptyListModel = {
  37. title: 'No data sources defined',
  38. buttonIcon: 'database' as IconName,
  39. buttonLink: 'datasources/new',
  40. buttonTitle: 'Add data source',
  41. proTip: 'You can also define data sources through configuration files.',
  42. proTipLink: 'http://docs.grafana.org/administration/provisioning/#datasources?utm_source=grafana_ds_list',
  43. proTipLinkTitle: 'Learn more',
  44. proTipTarget: '_blank',
  45. };
  46. export class DataSourcesListPage extends PureComponent<Props> {
  47. componentDidMount() {
  48. this.props.loadDataSources();
  49. }
  50. render() {
  51. const { dataSources, dataSourcesCount, navModel, layoutMode, searchQuery, setDataSourcesSearchQuery, hasFetched } =
  52. this.props;
  53. const canCreateDataSource = contextSrv.hasPermission(AccessControlAction.DataSourcesCreate);
  54. const linkButton = {
  55. href: 'datasources/new',
  56. title: 'Add data source',
  57. disabled: !canCreateDataSource,
  58. };
  59. const emptyList = {
  60. ...emptyListModel,
  61. buttonDisabled: !canCreateDataSource,
  62. };
  63. return (
  64. <Page navModel={navModel}>
  65. <Page.Contents isLoading={!hasFetched}>
  66. <>
  67. {hasFetched && dataSourcesCount === 0 && <EmptyListCTA {...emptyList} />}
  68. {hasFetched &&
  69. dataSourcesCount > 0 && [
  70. <PageActionBar
  71. searchQuery={searchQuery}
  72. setSearchQuery={(query) => setDataSourcesSearchQuery(query)}
  73. linkButton={linkButton}
  74. key="action-bar"
  75. />,
  76. <DataSourcesList dataSources={dataSources} layoutMode={layoutMode} key="list" />,
  77. ]}
  78. </>
  79. </Page.Contents>
  80. </Page>
  81. );
  82. }
  83. }
  84. export default connector(DataSourcesListPage);