releaseSelector.tsx 4.3 KB

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