durationAggregateSelector.tsx 1.8 KB

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