index.tsx 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import {useMemo} from 'react';
  2. import {useDataset} from 'sentry/views/explore/hooks/useDataset';
  3. import {useGroupBys} from 'sentry/views/explore/hooks/useGroupBys';
  4. import {useResultMode} from 'sentry/views/explore/hooks/useResultsMode';
  5. import {useSampleFields} from 'sentry/views/explore/hooks/useSampleFields';
  6. import {useSorts} from 'sentry/views/explore/hooks/useSorts';
  7. import {useVisualizes} from 'sentry/views/explore/hooks/useVisualizes';
  8. import {ToolbarDataset} from 'sentry/views/explore/toolbar/toolbarDataset';
  9. import {ToolbarGroupBy} from 'sentry/views/explore/toolbar/toolbarGroupBy';
  10. import {ToolbarResults} from 'sentry/views/explore/toolbar/toolbarResults';
  11. import {ToolbarSortBy} from 'sentry/views/explore/toolbar/toolbarSortBy';
  12. import {ToolbarVisualize} from 'sentry/views/explore/toolbar/toolbarVisualize';
  13. type Extras = 'dataset toggle';
  14. interface ExploreToolbarProps {
  15. extras?: Extras[];
  16. }
  17. export function ExploreToolbar({extras}: ExploreToolbarProps) {
  18. const [dataset, setDataset] = useDataset({allowRPC: true});
  19. const [resultMode, setResultMode] = useResultMode();
  20. const [sampleFields] = useSampleFields();
  21. const {groupBys} = useGroupBys();
  22. const [visualizes] = useVisualizes();
  23. const fields = useMemo(() => {
  24. if (resultMode === 'samples') {
  25. return sampleFields;
  26. }
  27. const allFields: string[] = [];
  28. for (const visualize of visualizes) {
  29. for (const yAxis of visualize.yAxes) {
  30. if (allFields.includes(yAxis)) {
  31. continue;
  32. }
  33. allFields.push(yAxis);
  34. }
  35. }
  36. for (const groupBy of groupBys) {
  37. if (allFields.includes(groupBy)) {
  38. continue;
  39. }
  40. allFields.push(groupBy);
  41. }
  42. return allFields.filter(Boolean);
  43. }, [resultMode, sampleFields, groupBys, visualizes]);
  44. const [sorts, setSorts] = useSorts({fields});
  45. return (
  46. <div>
  47. {extras?.includes('dataset toggle') && (
  48. <ToolbarDataset dataset={dataset} setDataset={setDataset} />
  49. )}
  50. <ToolbarResults resultMode={resultMode} setResultMode={setResultMode} />
  51. <ToolbarVisualize />
  52. <ToolbarGroupBy disabled={resultMode !== 'aggregate'} />
  53. <ToolbarSortBy fields={fields} sorts={sorts} setSorts={setSorts} />
  54. </div>
  55. );
  56. }