DashboardListPage.tsx 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import { css } from '@emotion/css';
  2. import React, { FC, memo } from 'react';
  3. import { connect, MapStateToProps } from 'react-redux';
  4. import { useAsync } from 'react-use';
  5. import { NavModel, locationUtil } from '@grafana/data';
  6. import { config, locationService } from '@grafana/runtime';
  7. import Page from 'app/core/components/Page/Page';
  8. import { getNavModel } from 'app/core/selectors/navModel';
  9. import { FolderDTO, StoreState } from 'app/types';
  10. import { GrafanaRouteComponentProps } from '../../../core/navigation/types';
  11. import { loadFolderPage } from '../loaders';
  12. import ManageDashboards from './ManageDashboards';
  13. import ManageDashboardsNew from './ManageDashboardsNew';
  14. export interface DashboardListPageRouteParams {
  15. uid?: string;
  16. slug?: string;
  17. }
  18. interface DashboardListPageConnectedProps {
  19. navModel: NavModel;
  20. }
  21. interface Props extends GrafanaRouteComponentProps<DashboardListPageRouteParams>, DashboardListPageConnectedProps {}
  22. export const DashboardListPage: FC<Props> = memo(({ navModel, match, location }) => {
  23. const { loading, value } = useAsync<() => Promise<{ folder?: FolderDTO; pageNavModel: NavModel }>>(() => {
  24. const uid = match.params.uid;
  25. const url = location.pathname;
  26. if (!uid || !url.startsWith('/dashboards')) {
  27. return Promise.resolve({ pageNavModel: navModel });
  28. }
  29. return loadFolderPage(uid!).then(({ folder, folderNav }) => {
  30. const path = locationUtil.stripBaseFromUrl(folder.url);
  31. if (path !== location.pathname) {
  32. locationService.push(path);
  33. }
  34. return { folder, pageNavModel: { ...navModel, main: folderNav } };
  35. });
  36. }, [match.params.uid]);
  37. return (
  38. <Page navModel={value?.pageNavModel ?? navModel}>
  39. {Boolean(config.featureToggles.panelTitleSearch) ? (
  40. <Page.Contents
  41. isLoading={loading}
  42. className={css`
  43. display: flex;
  44. flex-direction: column;
  45. overflow: hidden;
  46. `}
  47. >
  48. <ManageDashboardsNew folder={value?.folder} />
  49. </Page.Contents>
  50. ) : (
  51. <Page.Contents isLoading={loading}>
  52. <ManageDashboards folder={value?.folder} />
  53. </Page.Contents>
  54. )}
  55. </Page>
  56. );
  57. });
  58. DashboardListPage.displayName = 'DashboardListPage';
  59. const mapStateToProps: MapStateToProps<DashboardListPageConnectedProps, {}, StoreState> = (state) => {
  60. return {
  61. navModel: getNavModel(state.navIndex, 'manage-dashboards'),
  62. };
  63. };
  64. export default connect(mapStateToProps)(DashboardListPage);