projectLatestReleases.tsx 6.5 KB

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