useTopEvents.tsx 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import {useMemo} from 'react';
  2. import {
  3. useExploreGroupBys,
  4. useExploreMode,
  5. useExploreVisualizes,
  6. } from 'sentry/views/explore/contexts/pageParamsContext';
  7. import {Mode} from 'sentry/views/explore/contexts/pageParamsContext/mode';
  8. export const TOP_EVENTS_LIMIT = 5;
  9. // TODO: There's a limitation with this hook when a top n query < 5 series.
  10. // This hook always returns 5, which can be misleading, but there's no simple way
  11. // to get the series count without adding more complexity to this hook.
  12. export function useTopEvents(): number | undefined {
  13. const visualizes = useExploreVisualizes();
  14. const groupBys = useExploreGroupBys();
  15. const mode = useExploreMode();
  16. const hasChartWithMultipleYaxes = useMemo(() => {
  17. return visualizes.some(visualize => visualize.yAxes.length > 1);
  18. }, [visualizes]);
  19. const topEvents: number | undefined = useMemo(() => {
  20. if (mode === Mode.SAMPLES) {
  21. return undefined;
  22. }
  23. // We only support top events for when there are no multiple y-axes chart
  24. // and there is at least one group by.
  25. return hasChartWithMultipleYaxes || (groupBys.length === 1 && groupBys[0] === '')
  26. ? undefined
  27. : TOP_EVENTS_LIMIT;
  28. }, [hasChartWithMultipleYaxes, groupBys, mode]);
  29. return topEvents;
  30. }