releaseContext.tsx 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. import {useEffect, useMemo} from 'react';
  2. import styled from '@emotion/styled';
  3. import AvatarList from 'sentry/components/avatar/avatarList';
  4. import {QuickContextCommitRow} from 'sentry/components/discover/quickContextCommitRow';
  5. import {DataSection} from 'sentry/components/events/styles';
  6. import Panel from 'sentry/components/panels/panel';
  7. import TimeSince from 'sentry/components/timeSince';
  8. import {IconNot} from 'sentry/icons';
  9. import {t, tct} from 'sentry/locale';
  10. import {space} from 'sentry/styles/space';
  11. import type {Actor} from 'sentry/types/core';
  12. import type {ReleaseWithHealth} from 'sentry/types/release';
  13. import type {User} from 'sentry/types/user';
  14. import {trackAnalytics} from 'sentry/utils/analytics';
  15. import {uniqueId} from 'sentry/utils/guid';
  16. import {useApiQuery} from 'sentry/utils/queryClient';
  17. import {useUser} from 'sentry/utils/useUser';
  18. import {NoContext} from './quickContextWrapper';
  19. import {
  20. ContextBody,
  21. ContextContainer,
  22. ContextHeader,
  23. ContextRow,
  24. ContextTitle,
  25. Wrapper,
  26. } from './styles';
  27. import type {BaseContextProps} from './utils';
  28. import {ContextType, tenSecondInMs} from './utils';
  29. function ReleaseContext(props: BaseContextProps) {
  30. const user = useUser();
  31. const {dataRow, organization} = props;
  32. const {isPending, isError, data} = useApiQuery<ReleaseWithHealth>(
  33. [
  34. `/organizations/${organization.slug}/releases/${encodeURIComponent(
  35. dataRow.release
  36. )}/`,
  37. ],
  38. {
  39. staleTime: tenSecondInMs,
  40. }
  41. );
  42. const authors = useMemo(
  43. () =>
  44. data?.authors.map<Actor | User>(author =>
  45. // Add a unique id if missing
  46. ({
  47. ...author,
  48. type: 'user',
  49. id: 'id' in author ? author.id : uniqueId(),
  50. })
  51. ),
  52. [data?.authors]
  53. );
  54. useEffect(() => {
  55. trackAnalytics('discover_v2.quick_context_hover_contexts', {
  56. organization,
  57. contextType: ContextType.RELEASE,
  58. });
  59. }, [organization]);
  60. const getCommitAuthorTitle = () => {
  61. const commitCount = data?.commitCount || 0;
  62. let authorsCount = data?.authors?.length || 0;
  63. const userInAuthors =
  64. data &&
  65. authorsCount >= 1 &&
  66. data.authors.find(author => 'id' in author && user.id && author.id === user.id);
  67. if (userInAuthors) {
  68. authorsCount = authorsCount - 1;
  69. return authorsCount !== 1 && commitCount !== 1
  70. ? tct('[commitCount] commits by you and [authorsCount] others', {
  71. commitCount,
  72. authorsCount,
  73. })
  74. : commitCount !== 1
  75. ? tct('[commitCount] commits by you and 1 other', {
  76. commitCount,
  77. })
  78. : authorsCount !== 1
  79. ? tct('1 commit by you and [authorsCount] others', {
  80. authorsCount,
  81. })
  82. : t('1 commit by you and 1 other');
  83. }
  84. return (
  85. data &&
  86. (authorsCount !== 1 && commitCount !== 1
  87. ? tct('[commitCount] commits by [authorsCount] authors', {
  88. commitCount,
  89. authorsCount,
  90. })
  91. : commitCount !== 1
  92. ? tct('[commitCount] commits by 1 author', {
  93. commitCount,
  94. })
  95. : authorsCount !== 1
  96. ? tct('1 commit by [authorsCount] authors', {
  97. authorsCount,
  98. })
  99. : t('1 commit by 1 author'))
  100. );
  101. };
  102. const renderReleaseAuthors = () => {
  103. return (
  104. data && (
  105. <ReleaseContextContainer data-test-id="quick-context-release-details-container">
  106. <ContextHeader data-test-id="quick-context-release-author-header">
  107. <ContextTitle>{getCommitAuthorTitle()}</ContextTitle>
  108. </ContextHeader>
  109. <ContextBody>
  110. {data.commitCount === 0 ? (
  111. <IconNot color="gray500" size="md" />
  112. ) : (
  113. <StyledAvatarList users={authors} maxVisibleAvatars={10} />
  114. )}
  115. </ContextBody>
  116. </ReleaseContextContainer>
  117. )
  118. );
  119. };
  120. const renderLastCommit = () =>
  121. data?.lastCommit && (
  122. <ReleaseContextContainer data-test-id="quick-context-release-last-commit-container">
  123. <ContextHeader>
  124. <ContextTitle>{t('Last Commit')}</ContextTitle>
  125. </ContextHeader>
  126. <DataSection>
  127. <Panel>
  128. <QuickContextCommitRow commit={data.lastCommit} />
  129. </Panel>
  130. </DataSection>
  131. </ReleaseContextContainer>
  132. );
  133. const renderReleaseDetails = () =>
  134. data && (
  135. <ReleaseContextContainer data-test-id="quick-context-release-issues-and-authors-container">
  136. <ContextRow>
  137. <div>
  138. <ContextHeader>
  139. <ContextTitle>{t('Created')}</ContextTitle>
  140. </ContextHeader>
  141. <ReleaseBody>
  142. <TimeSince date={data.dateCreated} />
  143. </ReleaseBody>
  144. </div>
  145. <div>
  146. <ContextHeader>
  147. <ContextTitle>{t('Last Event')}</ContextTitle>
  148. </ContextHeader>
  149. <ReleaseBody>
  150. {data.lastEvent ? <TimeSince date={data.lastEvent} /> : '\u2014'}
  151. </ReleaseBody>
  152. </div>
  153. <div>
  154. <ContextHeader>
  155. <ContextTitle>{t('New Issues')}</ContextTitle>
  156. </ContextHeader>
  157. <ContextBody>{data.newGroups}</ContextBody>
  158. </div>
  159. </ContextRow>
  160. </ReleaseContextContainer>
  161. );
  162. if (isPending || isError) {
  163. return <NoContext isLoading={isPending} />;
  164. }
  165. return (
  166. <Wrapper data-test-id="quick-context-hover-body">
  167. {renderReleaseDetails()}
  168. {renderReleaseAuthors()}
  169. {renderLastCommit()}
  170. </Wrapper>
  171. );
  172. }
  173. const StyledAvatarList = styled(AvatarList)`
  174. margin: 0 ${space(0.75)};
  175. `;
  176. const ReleaseContextContainer = styled(ContextContainer)`
  177. ${Panel} {
  178. margin: 0;
  179. border: none;
  180. box-shadow: none;
  181. }
  182. ${DataSection} {
  183. padding: 0;
  184. }
  185. & + & {
  186. margin-top: ${space(2)};
  187. }
  188. `;
  189. const ReleaseBody = styled(ContextBody)<{}>`
  190. font-size: 13px;
  191. color: ${p => p.theme.subText};
  192. `;
  193. export default ReleaseContext;