releasesSelectControl.tsx 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import {Fragment, useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import Badge from 'sentry/components/badge';
  4. import CompactSelect from 'sentry/components/forms/compactSelect';
  5. import TextOverflow from 'sentry/components/textOverflow';
  6. import {IconReleases} from 'sentry/icons';
  7. import {t} from 'sentry/locale';
  8. import {Release} from 'sentry/types';
  9. import {useReleases} from 'sentry/utils/releases/releasesProvider';
  10. function ReleasesSelectControl() {
  11. const {releases, loading} = useReleases();
  12. const [selectedReleases, setSelectedReleases] = useState<Release[]>([]);
  13. const triggerLabel = selectedReleases.length ? (
  14. <TextOverflow>{selectedReleases[0]}</TextOverflow>
  15. ) : (
  16. t('All Releases')
  17. );
  18. return (
  19. <CompactSelect
  20. multiple
  21. isClearable
  22. isSearchable
  23. isLoading={loading}
  24. menuTitle={t('Filter Releases')}
  25. options={
  26. releases.length
  27. ? releases.map(release => {
  28. return {
  29. label: release.shortVersion ?? release.version,
  30. value: release.version,
  31. };
  32. })
  33. : []
  34. }
  35. onChange={opts => setSelectedReleases(opts.map(opt => opt.value))}
  36. value={selectedReleases}
  37. triggerLabel={
  38. <Fragment>
  39. {triggerLabel}
  40. {selectedReleases.length > 1 && (
  41. <StyledBadge text={`+${selectedReleases.length - 1}`} />
  42. )}
  43. </Fragment>
  44. }
  45. triggerProps={{icon: <IconReleases />}}
  46. />
  47. );
  48. }
  49. export default ReleasesSelectControl;
  50. const StyledBadge = styled(Badge)`
  51. flex-shrink: 0;
  52. `;