releaseSelector.tsx 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import {browserHistory} from 'react-router';
  2. import styled from '@emotion/styled';
  3. import {CompactSelect} from 'sentry/components/compactSelect';
  4. import PageFilterBar from 'sentry/components/organizations/pageFilterBar';
  5. import {t} from 'sentry/locale';
  6. import {defined} from 'sentry/utils';
  7. import {useLocation} from 'sentry/utils/useLocation';
  8. import {
  9. useReleases,
  10. useReleaseSelection,
  11. } from 'sentry/views/starfish/queries/useReleases';
  12. const ALL_RELEASES = {
  13. value: '',
  14. label: t('All Releases'),
  15. };
  16. type Props = {
  17. selectorKey: string;
  18. selectorName: string;
  19. selectorValue?: string;
  20. };
  21. export function ReleaseSelector({selectorName, selectorKey, selectorValue}: Props) {
  22. const {data, isLoading} = useReleases();
  23. const location = useLocation();
  24. let value = selectorValue;
  25. if (!isLoading && !defined(value)) {
  26. value = ALL_RELEASES.value;
  27. }
  28. return (
  29. <StyledCompactSelect
  30. triggerProps={{
  31. prefix: selectorName,
  32. }}
  33. value={selectorValue}
  34. options={[
  35. ...(data ?? [ALL_RELEASES]).map(release => ({
  36. value: release.version,
  37. label: release.shortVersion ?? release.version,
  38. })),
  39. ]}
  40. onChange={newValue => {
  41. browserHistory.push({
  42. ...location,
  43. query: {
  44. ...location.query,
  45. [selectorKey]: newValue.value,
  46. },
  47. });
  48. }}
  49. />
  50. );
  51. }
  52. export function ReleaseComparisonSelector() {
  53. const {primaryRelease, secondaryRelease} = useReleaseSelection();
  54. return (
  55. <PageFilterBar condensed>
  56. <ReleaseSelector
  57. selectorKey="primaryRelease"
  58. selectorName={t('Primary Release')}
  59. selectorValue={primaryRelease}
  60. />
  61. <ReleaseSelector
  62. selectorKey="secondaryRelease"
  63. selectorName={t('Secondary Release')}
  64. selectorValue={secondaryRelease}
  65. />
  66. </PageFilterBar>
  67. );
  68. }
  69. const StyledCompactSelect = styled(CompactSelect)`
  70. @media (min-width: ${p => p.theme.breakpoints.small}) {
  71. max-width: 300px;
  72. }
  73. `;