addToDashboardButton.tsx 3.4 KB

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