durationAggregateSelector.tsx 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import {browserHistory} from 'react-router';
  2. import styled from '@emotion/styled';
  3. import {CompactSelect} from 'sentry/components/compactSelect';
  4. import {t} from 'sentry/locale';
  5. import {space} from 'sentry/styles/space';
  6. import {useLocation} from 'sentry/utils/useLocation';
  7. import {useAvailableDurationAggregates} from 'sentry/views/performance/database/useAvailableDurationAggregates';
  8. export function DurationAggregateSelector() {
  9. const location = useLocation();
  10. const {selectedAggregate, availableAggregates} = useAvailableDurationAggregates();
  11. // If only one aggregate is available, render a plain string
  12. if (availableAggregates.length === 1) {
  13. return DURATION_AGGREGATE_LABELS[selectedAggregate];
  14. }
  15. const handleDurationFunctionChange = option => {
  16. browserHistory.push({
  17. ...location,
  18. query: {
  19. ...location.query,
  20. aggregate: option.value,
  21. },
  22. });
  23. };
  24. // If multiple aggregates are available, render a dropdown list
  25. return (
  26. <StyledCompactSelect
  27. size="md"
  28. options={availableAggregates.map(availableAggregate => ({
  29. value: availableAggregate,
  30. label: DURATION_AGGREGATE_LABELS[availableAggregate],
  31. }))}
  32. value={selectedAggregate}
  33. onChange={handleDurationFunctionChange}
  34. triggerProps={{borderless: true, size: 'zero'}}
  35. />
  36. );
  37. }
  38. const DURATION_AGGREGATE_LABELS = {
  39. avg: t('Average Duration'),
  40. max: t('Max Duration'),
  41. p50: t('Duration p50'),
  42. p75: t('Duration p75'),
  43. p95: t('Duration p95'),
  44. p99: t('Duration p99'),
  45. };
  46. // TODO: Talk to UI folks about making this a built-in dropdown size, we use this in a few places
  47. const StyledCompactSelect = styled(CompactSelect)`
  48. text-align: left;
  49. font-weight: normal;
  50. margin: -${space(0.5)} -${space(1)} -${space(0.25)};
  51. min-width: 0;
  52. button {
  53. padding: ${space(0.5)} ${space(1)};
  54. font-size: ${p => p.theme.fontSizeLarge};
  55. }
  56. `;