useAddToDashboard.tsx 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import {useCallback} from 'react';
  2. import {t} from 'sentry/locale';
  3. import type {NewQuery} from 'sentry/types/organization';
  4. import EventView from 'sentry/utils/discover/eventView';
  5. import {MutableSearch} from 'sentry/utils/tokenizeSearch';
  6. import {useLocation} from 'sentry/utils/useLocation';
  7. import useOrganization from 'sentry/utils/useOrganization';
  8. import usePageFilters from 'sentry/utils/usePageFilters';
  9. import useRouter from 'sentry/utils/useRouter';
  10. import {WidgetType} from 'sentry/views/dashboards/types';
  11. import {MAX_NUM_Y_AXES} from 'sentry/views/dashboards/widgetBuilder/buildSteps/yAxisStep/yAxisSelector';
  12. import {handleAddQueryToDashboard} from 'sentry/views/discover/utils';
  13. import {useDataset} from 'sentry/views/explore/hooks/useDataset';
  14. import {useGroupBys} from 'sentry/views/explore/hooks/useGroupBys';
  15. import {useResultMode} from 'sentry/views/explore/hooks/useResultsMode';
  16. import {useSampleFields} from 'sentry/views/explore/hooks/useSampleFields';
  17. import {getSorts} from 'sentry/views/explore/hooks/useSorts';
  18. import {useUserQuery} from 'sentry/views/explore/hooks/useUserQuery';
  19. import {useVisualizes} from 'sentry/views/explore/hooks/useVisualizes';
  20. import {formatSort} from 'sentry/views/explore/tables/aggregatesTable';
  21. export function useAddToDashboard() {
  22. const location = useLocation();
  23. const router = useRouter();
  24. const {selection} = usePageFilters();
  25. const organization = useOrganization();
  26. const [resultMode] = useResultMode();
  27. const [dataset] = useDataset();
  28. const {groupBys} = useGroupBys();
  29. const [visualizes] = useVisualizes();
  30. const [sampleFields] = useSampleFields();
  31. const [query] = useUserQuery();
  32. const getEventView = useCallback(
  33. (visualizeIndex: number) => {
  34. const yAxes = visualizes[visualizeIndex].yAxes.slice(0, MAX_NUM_Y_AXES);
  35. let fields;
  36. if (resultMode === 'samples') {
  37. fields = sampleFields.filter(Boolean);
  38. } else {
  39. fields = [...groupBys, ...yAxes].filter(Boolean);
  40. }
  41. const search = new MutableSearch(query);
  42. const sorts = getSorts(fields, location);
  43. const discoverQuery: NewQuery = {
  44. name: t('Custom Explore Widget'),
  45. fields,
  46. orderby: sorts.map(formatSort),
  47. query: search.formatString(),
  48. version: 2,
  49. dataset,
  50. yAxis: yAxes,
  51. };
  52. const newEventView = EventView.fromNewQueryWithPageFilters(
  53. discoverQuery,
  54. selection
  55. );
  56. newEventView.dataset = dataset;
  57. return newEventView;
  58. },
  59. [visualizes, resultMode, sampleFields, groupBys, query, location, dataset, selection]
  60. );
  61. const addToDashboard = useCallback(
  62. (visualizeIndex: number) => {
  63. const eventView = getEventView(visualizeIndex);
  64. handleAddQueryToDashboard({
  65. organization,
  66. location,
  67. eventView,
  68. router,
  69. yAxis: eventView.yAxis,
  70. widgetType: WidgetType.SPANS,
  71. });
  72. },
  73. [organization, location, getEventView, router]
  74. );
  75. return {
  76. addToDashboard,
  77. };
  78. }