releaseSelector.tsx 1.9 KB

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