useManageDashboards.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import { useCallback, useMemo, useReducer } from 'react';
  2. import { useDebounce } from 'react-use';
  3. import { reportInteraction } from '@grafana/runtime/src';
  4. import { contextSrv } from 'app/core/services/context_srv';
  5. import { FolderDTO } from 'app/types';
  6. import { GENERAL_FOLDER_ID } from '../constants';
  7. import { DELETE_ITEMS, MOVE_ITEMS, TOGGLE_ALL_CHECKED, TOGGLE_CHECKED } from '../reducers/actionTypes';
  8. import { manageDashboardsReducer, manageDashboardsState, ManageDashboardsState } from '../reducers/manageDashboards';
  9. import { DashboardQuery, DashboardSection, OnDeleteItems, OnMoveItems, OnToggleChecked, SearchLayout } from '../types';
  10. import { useSearch } from './useSearch';
  11. import { useShowDashboardPreviews } from './useShowDashboardPreviews';
  12. const hasChecked = (section: DashboardSection) => {
  13. return section.checked || section.items.some((item) => item.checked);
  14. };
  15. export const reportDashboardListViewed = (
  16. dashboardListType: 'manage_dashboards' | 'dashboard_search',
  17. showPreviews: boolean,
  18. previewsEnabled: boolean,
  19. query: {
  20. layout?: SearchLayout;
  21. starred?: boolean;
  22. sortValue?: string;
  23. query?: string;
  24. tagCount?: number;
  25. }
  26. ) => {
  27. const previews = previewsEnabled ? (showPreviews ? 'on' : 'off') : 'feature_disabled';
  28. reportInteraction(`${dashboardListType}_viewed`, {
  29. previews,
  30. layout: query.layout,
  31. starredFilter: query.starred ?? false,
  32. sort: query.sortValue ?? '',
  33. tagCount: query.tagCount ?? 0,
  34. queryLength: query.query?.length ?? 0,
  35. });
  36. };
  37. export const useManageDashboards = (
  38. query: DashboardQuery,
  39. state: Partial<ManageDashboardsState> = {},
  40. folder?: FolderDTO
  41. ) => {
  42. const reducer = useReducer(manageDashboardsReducer, {
  43. ...manageDashboardsState,
  44. ...state,
  45. });
  46. const { showPreviews, setShowPreviews, previewFeatureEnabled } = useShowDashboardPreviews();
  47. useDebounce(
  48. () => {
  49. reportDashboardListViewed('manage_dashboards', showPreviews, previewFeatureEnabled, {
  50. layout: query.layout,
  51. starred: query.starred,
  52. sortValue: query.sort?.value,
  53. query: query.query,
  54. tagCount: query.tag?.length,
  55. });
  56. },
  57. 1000,
  58. [
  59. showPreviews,
  60. previewFeatureEnabled,
  61. query.layout,
  62. query.starred,
  63. query.sort?.value,
  64. query.query?.length,
  65. query.tag?.length,
  66. ]
  67. );
  68. const {
  69. state: { results, loading, initialLoading, allChecked },
  70. onToggleSection,
  71. dispatch,
  72. } = useSearch<ManageDashboardsState>(query, reducer, {});
  73. const onToggleChecked: OnToggleChecked = useCallback(
  74. (item) => {
  75. dispatch({ type: TOGGLE_CHECKED, payload: item });
  76. },
  77. [dispatch]
  78. );
  79. const onToggleAllChecked = () => {
  80. dispatch({ type: TOGGLE_ALL_CHECKED });
  81. };
  82. const onDeleteItems: OnDeleteItems = (folders, dashboards) => {
  83. dispatch({ type: DELETE_ITEMS, payload: { folders, dashboards } });
  84. };
  85. const onMoveItems: OnMoveItems = (selectedDashboards, folder) => {
  86. dispatch({ type: MOVE_ITEMS, payload: { dashboards: selectedDashboards, folder } });
  87. };
  88. const canMove = useMemo(() => results.some((result) => result.items.some((item) => item.checked)), [results]);
  89. const canDelete = useMemo(() => {
  90. const somethingChecked = results.some(hasChecked);
  91. const includesGeneralFolder = results.find((result) => result.checked && result.id === GENERAL_FOLDER_ID);
  92. return somethingChecked && !includesGeneralFolder;
  93. }, [results]);
  94. const canSave = folder?.canSave;
  95. const hasEditPermissionInFolders = folder ? canSave : contextSrv.hasEditPermissionInFolders;
  96. const noFolders = canSave && folder?.id && results.length === 0 && !loading && !initialLoading;
  97. return {
  98. results,
  99. loading,
  100. initialLoading,
  101. canSave,
  102. allChecked,
  103. hasEditPermissionInFolders,
  104. canMove,
  105. canDelete,
  106. onToggleSection,
  107. onToggleChecked,
  108. onToggleAllChecked,
  109. onDeleteItems,
  110. onMoveItems,
  111. noFolders,
  112. showPreviews,
  113. setShowPreviews,
  114. };
  115. };