projectLatestReleases.tsx 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. import {Fragment} from 'react';
  2. import styled from '@emotion/styled';
  3. import type {Location} from 'history';
  4. import pick from 'lodash/pick';
  5. import {fetchAnyReleaseExistence} from 'sentry/actionCreators/projects';
  6. import {SectionHeading} from 'sentry/components/charts/styles';
  7. import {DateTime} from 'sentry/components/dateTime';
  8. import DeprecatedAsyncComponent from 'sentry/components/deprecatedAsyncComponent';
  9. import EmptyStateWarning from 'sentry/components/emptyStateWarning';
  10. import Placeholder from 'sentry/components/placeholder';
  11. import TextOverflow from 'sentry/components/textOverflow';
  12. import Version from 'sentry/components/version';
  13. import {URL_PARAM} from 'sentry/constants/pageFilters';
  14. import {IconOpen} from 'sentry/icons';
  15. import {t} from 'sentry/locale';
  16. import {space} from 'sentry/styles/space';
  17. import type {Organization} from 'sentry/types/organization';
  18. import type {Project} from 'sentry/types/project';
  19. import type {Release} from 'sentry/types/release';
  20. import MissingReleasesButtons from './missingFeatureButtons/missingReleasesButtons';
  21. import {SectionHeadingLink, SectionHeadingWrapper, SidebarSection} from './styles';
  22. import {didProjectOrEnvironmentChange} from './utils';
  23. const PLACEHOLDER_AND_EMPTY_HEIGHT = '160px';
  24. type Props = DeprecatedAsyncComponent['props'] & {
  25. isProjectStabilized: boolean;
  26. location: Location;
  27. organization: Organization;
  28. projectSlug: string;
  29. project?: Project;
  30. };
  31. type State = {
  32. releases: Release[] | null;
  33. hasOlderReleases?: boolean;
  34. } & DeprecatedAsyncComponent['state'];
  35. class ProjectLatestReleases extends DeprecatedAsyncComponent<Props, State> {
  36. shouldComponentUpdate(nextProps: Props, nextState: State) {
  37. const {location, isProjectStabilized} = this.props;
  38. // TODO(project-detail): we temporarily removed refetching based on timeselector
  39. if (
  40. this.state !== nextState ||
  41. didProjectOrEnvironmentChange(location, nextProps.location) ||
  42. isProjectStabilized !== nextProps.isProjectStabilized
  43. ) {
  44. return true;
  45. }
  46. return false;
  47. }
  48. componentDidUpdate(prevProps: Props) {
  49. const {location, isProjectStabilized} = this.props;
  50. if (
  51. didProjectOrEnvironmentChange(prevProps.location, location) ||
  52. prevProps.isProjectStabilized !== isProjectStabilized
  53. ) {
  54. this.remountComponent();
  55. }
  56. }
  57. getEndpoints(): ReturnType<DeprecatedAsyncComponent['getEndpoints']> {
  58. const {location, organization, projectSlug, isProjectStabilized} = this.props;
  59. if (!isProjectStabilized) {
  60. return [];
  61. }
  62. const query = {
  63. ...pick(location.query, Object.values(URL_PARAM)),
  64. per_page: 5,
  65. };
  66. // TODO(project-detail): this does not filter releases for the given time
  67. return [
  68. ['releases', `/projects/${organization.slug}/${projectSlug}/releases/`, {query}],
  69. ];
  70. }
  71. /**
  72. * If our releases are empty, determine if we had a release in the last 90 days (empty message differs then)
  73. */
  74. async onLoadAllEndpointsSuccess() {
  75. const {releases} = this.state;
  76. const {organization, project, isProjectStabilized} = this.props;
  77. if (!isProjectStabilized) {
  78. return;
  79. }
  80. if ((releases ?? []).length !== 0 || !project?.id) {
  81. this.setState({hasOlderReleases: true});
  82. return;
  83. }
  84. this.setState({loading: true});
  85. const hasOlderReleases = await fetchAnyReleaseExistence(
  86. this.api,
  87. organization.slug,
  88. project.id
  89. );
  90. this.setState({hasOlderReleases, loading: false});
  91. }
  92. get releasesLink() {
  93. const {organization} = this.props;
  94. // as this is a link to latest releases, we want to only preserve project and environment
  95. return {
  96. pathname: `/organizations/${organization.slug}/releases/`,
  97. query: {
  98. statsPeriod: undefined,
  99. start: undefined,
  100. end: undefined,
  101. utc: undefined,
  102. },
  103. };
  104. }
  105. renderReleaseRow = (release: Release) => {
  106. const {project} = this.props;
  107. const {lastDeploy, dateCreated} = release;
  108. return (
  109. <Fragment key={release.version}>
  110. <DateTime date={lastDeploy?.dateFinished || dateCreated} seconds={false} />
  111. <TextOverflow>
  112. <StyledVersion
  113. version={release.version}
  114. tooltipRawVersion
  115. projectId={project?.id}
  116. />
  117. </TextOverflow>
  118. </Fragment>
  119. );
  120. };
  121. renderInnerBody() {
  122. const {organization, project, isProjectStabilized} = this.props;
  123. const {loading, releases, hasOlderReleases} = this.state;
  124. const checkingForOlderReleases =
  125. !(releases ?? []).length && hasOlderReleases === undefined;
  126. const showLoadingIndicator =
  127. loading || checkingForOlderReleases || !isProjectStabilized;
  128. if (showLoadingIndicator) {
  129. return <Placeholder height={PLACEHOLDER_AND_EMPTY_HEIGHT} />;
  130. }
  131. if (!hasOlderReleases) {
  132. return (
  133. <MissingReleasesButtons
  134. organization={organization}
  135. projectId={project?.id}
  136. platform={project?.platform}
  137. />
  138. );
  139. }
  140. if (!releases || releases.length === 0) {
  141. return (
  142. <StyledEmptyStateWarning small>{t('No releases found')}</StyledEmptyStateWarning>
  143. );
  144. }
  145. return <ReleasesTable>{releases.map(this.renderReleaseRow)}</ReleasesTable>;
  146. }
  147. renderLoading() {
  148. return this.renderBody();
  149. }
  150. renderBody() {
  151. return (
  152. <SidebarSection>
  153. <SectionHeadingWrapper>
  154. <SectionHeading>{t('Latest Releases')}</SectionHeading>
  155. <SectionHeadingLink to={this.releasesLink}>
  156. <IconOpen />
  157. </SectionHeadingLink>
  158. </SectionHeadingWrapper>
  159. <div>{this.renderInnerBody()}</div>
  160. </SidebarSection>
  161. );
  162. }
  163. }
  164. const ReleasesTable = styled('div')`
  165. display: grid;
  166. font-size: ${p => p.theme.fontSizeMedium};
  167. white-space: nowrap;
  168. grid-template-columns: 1fr auto;
  169. margin-bottom: ${space(2)};
  170. & > * {
  171. padding: ${space(0.5)} ${space(1)};
  172. height: 32px;
  173. }
  174. & > *:nth-child(2n + 2) {
  175. text-align: right;
  176. }
  177. & > *:nth-child(4n + 1),
  178. & > *:nth-child(4n + 2) {
  179. background-color: ${p => p.theme.rowBackground};
  180. }
  181. `;
  182. const StyledVersion = styled(Version)`
  183. ${p => p.theme.overflowEllipsis}
  184. line-height: 1.6;
  185. font-variant-numeric: tabular-nums;
  186. `;
  187. const StyledEmptyStateWarning = styled(EmptyStateWarning)`
  188. height: ${PLACEHOLDER_AND_EMPTY_HEIGHT};
  189. justify-content: center;
  190. `;
  191. export default ProjectLatestReleases;