releaseSelector.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import {useState} from 'react';
  2. import {browserHistory} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import debounce from 'lodash/debounce';
  5. import {CompactSelect, SelectOption} from 'sentry/components/compactSelect';
  6. import PageFilterBar from 'sentry/components/organizations/pageFilterBar';
  7. import {DEFAULT_DEBOUNCE_DURATION} from 'sentry/constants';
  8. import {IconReleases} from 'sentry/icons/iconReleases';
  9. import {t, tn} from 'sentry/locale';
  10. import {space} from 'sentry/styles/space';
  11. import {defined} from 'sentry/utils';
  12. import {getFormattedDate} from 'sentry/utils/dates';
  13. import {useLocation} from 'sentry/utils/useLocation';
  14. import {
  15. useReleases,
  16. useReleaseSelection,
  17. } from 'sentry/views/starfish/queries/useReleases';
  18. import {centerTruncate} from 'sentry/views/starfish/utils/centerTruncate';
  19. type Props = {
  20. selectorKey: string;
  21. selectorName?: string;
  22. selectorValue?: string;
  23. };
  24. export function ReleaseSelector({selectorKey, selectorValue}: Props) {
  25. const [searchTerm, setSearchTerm] = useState<string | undefined>(undefined);
  26. const {data, isLoading} = useReleases(searchTerm);
  27. const {primaryRelease, secondaryRelease} = useReleaseSelection();
  28. const location = useLocation();
  29. const options: SelectOption<string>[] = [];
  30. if (defined(selectorValue)) {
  31. const index = data?.findIndex(({version}) => version === selectorValue);
  32. const selectedRelease = defined(index) ? data?.[index] : undefined;
  33. let selectedReleaseSessionCount: number | undefined = undefined;
  34. let selectedReleaseDateCreated: string | undefined = undefined;
  35. if (defined(selectedRelease)) {
  36. selectedReleaseSessionCount = selectedRelease.count;
  37. selectedReleaseDateCreated = selectedRelease.dateCreated;
  38. }
  39. options.push({
  40. value: selectorValue,
  41. label: selectorValue,
  42. details: (
  43. <LabelDetails
  44. screenCount={selectedReleaseSessionCount}
  45. dateCreated={selectedReleaseDateCreated}
  46. />
  47. ),
  48. });
  49. }
  50. data
  51. ?.filter(({version}) => ![primaryRelease, secondaryRelease].includes(version))
  52. .forEach(release => {
  53. const option = {
  54. value: release.version,
  55. label: release.version,
  56. details: (
  57. <LabelDetails screenCount={release.count} dateCreated={release.dateCreated} />
  58. ),
  59. };
  60. options.push(option);
  61. });
  62. return (
  63. <StyledCompactSelect
  64. triggerProps={{
  65. icon: <IconReleases />,
  66. title: selectorValue,
  67. }}
  68. triggerLabel={selectorValue ? centerTruncate(selectorValue, 16) : selectorValue}
  69. menuTitle={t('Filter Release')}
  70. loading={isLoading}
  71. searchable
  72. value={selectorValue}
  73. options={[
  74. {
  75. value: '_releases',
  76. label: t('Sorted by date created'),
  77. options,
  78. },
  79. ]}
  80. onSearch={debounce(val => {
  81. setSearchTerm(val);
  82. }, DEFAULT_DEBOUNCE_DURATION)}
  83. onChange={newValue => {
  84. browserHistory.push({
  85. ...location,
  86. query: {
  87. ...location.query,
  88. [selectorKey]: newValue.value,
  89. },
  90. });
  91. }}
  92. onClose={() => {
  93. setSearchTerm(undefined);
  94. }}
  95. />
  96. );
  97. }
  98. type LabelDetailsProps = {
  99. dateCreated?: string;
  100. screenCount?: number;
  101. };
  102. function LabelDetails(props: LabelDetailsProps) {
  103. return (
  104. <DetailsContainer>
  105. <div>
  106. {defined(props.screenCount)
  107. ? tn('%s event', '%s events', props.screenCount)
  108. : t('No screens')}
  109. </div>
  110. <div>
  111. {defined(props.dateCreated)
  112. ? getFormattedDate(props.dateCreated, 'MMM D, YYYY')
  113. : null}
  114. </div>
  115. </DetailsContainer>
  116. );
  117. }
  118. export function ReleaseComparisonSelector() {
  119. const {primaryRelease, secondaryRelease} = useReleaseSelection();
  120. return (
  121. <PageFilterBar condensed>
  122. <ReleaseSelector
  123. selectorKey="primaryRelease"
  124. selectorValue={primaryRelease}
  125. selectorName={t('Release 1')}
  126. key="primaryRelease"
  127. />
  128. <ReleaseSelector
  129. selectorKey="secondaryRelease"
  130. selectorName={t('Release 2')}
  131. selectorValue={secondaryRelease}
  132. key="secondaryRelease"
  133. />
  134. </PageFilterBar>
  135. );
  136. }
  137. const StyledCompactSelect = styled(CompactSelect)`
  138. @media (min-width: ${p => p.theme.breakpoints.medium}) {
  139. max-width: 275px;
  140. }
  141. `;
  142. const DetailsContainer = styled('div')`
  143. display: flex;
  144. flex-direction: row;
  145. justify-content: space-between;
  146. gap: ${space(1)};
  147. min-width: 200px;
  148. `;