releaseSelector.tsx 1.5 KB

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