eventCauseEmpty.tsx 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. import {Component} from 'react';
  2. import styled from '@emotion/styled';
  3. import moment from 'moment';
  4. import codesworth from 'sentry-images/spot/codesworth.svg';
  5. import {promptsCheck, promptsUpdate} from 'sentry/actionCreators/prompts';
  6. import {Client} from 'sentry/api';
  7. import Button from 'sentry/components/button';
  8. import {CommitRow} from 'sentry/components/commitRow';
  9. import {DataSection} from 'sentry/components/events/styles';
  10. import {Panel} from 'sentry/components/panels';
  11. import {t} from 'sentry/locale';
  12. import space from 'sentry/styles/space';
  13. import {Commit, Organization, Project, RepositoryStatus} from 'sentry/types';
  14. import {Event} from 'sentry/types/event';
  15. import {IssueEventKey} from 'sentry/utils/analytics/issueAnalyticsEvents';
  16. import trackAdvancedAnalyticsEvent from 'sentry/utils/analytics/trackAdvancedAnalyticsEvent';
  17. import getDynamicText from 'sentry/utils/getDynamicText';
  18. import {promptCanShow, promptIsDismissed} from 'sentry/utils/promptIsDismissed';
  19. import withApi from 'sentry/utils/withApi';
  20. const EXAMPLE_COMMITS = ['dec0de', 'de1e7e', '5ca1ed'];
  21. const DUMMY_COMMIT: Commit = {
  22. id: getDynamicText({
  23. value: EXAMPLE_COMMITS[Math.floor(Math.random() * EXAMPLE_COMMITS.length)],
  24. fixed: '5ca1ed',
  25. }),
  26. author: {
  27. id: '',
  28. name: 'codesworth',
  29. username: '',
  30. email: 'codesworth@example.com',
  31. ip_address: '',
  32. lastSeen: '',
  33. lastLogin: '',
  34. isSuperuser: false,
  35. isAuthenticated: false,
  36. emails: [],
  37. isManaged: false,
  38. lastActive: '',
  39. isStaff: false,
  40. identities: [],
  41. isActive: true,
  42. has2fa: false,
  43. canReset2fa: false,
  44. authenticators: [],
  45. dateJoined: '',
  46. options: {
  47. theme: 'system',
  48. timezone: '',
  49. stacktraceOrder: 1,
  50. language: '',
  51. clock24Hours: false,
  52. avatarType: 'letter_avatar',
  53. },
  54. flags: {newsletter_consent_prompt: false},
  55. hasPasswordAuth: true,
  56. permissions: new Set([]),
  57. experiments: {},
  58. },
  59. dateCreated: moment().subtract(3, 'day').format(),
  60. repository: {
  61. id: '',
  62. integrationId: '',
  63. name: '',
  64. externalSlug: '',
  65. url: '',
  66. provider: {
  67. id: 'integrations:github',
  68. name: 'GitHub',
  69. },
  70. dateCreated: '',
  71. status: RepositoryStatus.ACTIVE,
  72. },
  73. releases: [],
  74. message: t('This example commit broke something'),
  75. };
  76. const SUSPECT_COMMITS_FEATURE = 'suspect_commits';
  77. type ClickPayload = {
  78. action: 'snoozed' | 'dismissed';
  79. eventKey: IssueEventKey;
  80. };
  81. type Props = {
  82. api: Client;
  83. event: Event;
  84. organization: Organization;
  85. project: Project;
  86. };
  87. type State = {
  88. shouldShow: boolean | undefined;
  89. };
  90. class EventCauseEmpty extends Component<Props, State> {
  91. state: State = {
  92. shouldShow: undefined,
  93. };
  94. componentDidMount() {
  95. this.fetchData();
  96. }
  97. componentDidUpdate(_prevProps: Props, prevState: State) {
  98. const {shouldShow} = this.state;
  99. if (!prevState.shouldShow && shouldShow) {
  100. this.trackAnalytics('event_cause.viewed');
  101. }
  102. }
  103. async fetchData() {
  104. const {api, event, project, organization} = this.props;
  105. if (!promptCanShow(SUSPECT_COMMITS_FEATURE, event.eventID)) {
  106. this.setState({shouldShow: false});
  107. return;
  108. }
  109. const data = await promptsCheck(api, {
  110. projectId: project.id,
  111. organizationId: organization.id,
  112. feature: SUSPECT_COMMITS_FEATURE,
  113. });
  114. this.setState({shouldShow: !promptIsDismissed(data ?? {}, 7)});
  115. }
  116. handleClick({action, eventKey}: ClickPayload) {
  117. const {api, project, organization} = this.props;
  118. const data = {
  119. projectId: project.id,
  120. organizationId: organization.id,
  121. feature: SUSPECT_COMMITS_FEATURE,
  122. status: action,
  123. };
  124. promptsUpdate(api, data).then(() => this.setState({shouldShow: false}));
  125. this.trackAnalytics(eventKey);
  126. }
  127. trackAnalytics(eventKey: IssueEventKey) {
  128. const {project, organization} = this.props;
  129. trackAdvancedAnalyticsEvent(eventKey, {
  130. project_id: project.id,
  131. platform: project.platform,
  132. organization,
  133. });
  134. }
  135. render() {
  136. const {shouldShow} = this.state;
  137. if (!shouldShow) {
  138. return null;
  139. }
  140. return (
  141. <DataSection data-test-id="loaded-event-cause-empty">
  142. <StyledPanel dashedBorder>
  143. <BoxHeader>
  144. <Description>
  145. <h3>{t('Configure Suspect Commits')}</h3>
  146. <p>{t('To identify which commit caused this issue')}</p>
  147. </Description>
  148. <ButtonList>
  149. <DocsButton
  150. size="small"
  151. priority="primary"
  152. href="https://docs.sentry.io/product/releases/setup/"
  153. onClick={() => this.trackAnalytics('event_cause.docs_clicked')}
  154. >
  155. {t('Read the docs')}
  156. </DocsButton>
  157. <div>
  158. <SnoozeButton
  159. title={t('Remind me next week')}
  160. size="small"
  161. onClick={() =>
  162. this.handleClick({
  163. action: 'snoozed',
  164. eventKey: 'event_cause.snoozed',
  165. })
  166. }
  167. >
  168. {t('Snooze')}
  169. </SnoozeButton>
  170. <DismissButton
  171. title={t('Dismiss for this project')}
  172. size="small"
  173. onClick={() =>
  174. this.handleClick({
  175. action: 'dismissed',
  176. eventKey: 'event_cause.dismissed',
  177. })
  178. }
  179. >
  180. {t('Dismiss')}
  181. </DismissButton>
  182. </div>
  183. </ButtonList>
  184. </BoxHeader>
  185. <ExampleCommitPanel>
  186. <CommitRow
  187. key={DUMMY_COMMIT.id}
  188. commit={DUMMY_COMMIT}
  189. customAvatar={<CustomAvatar src={codesworth} />}
  190. />
  191. </ExampleCommitPanel>
  192. </StyledPanel>
  193. </DataSection>
  194. );
  195. }
  196. }
  197. const StyledPanel = styled(Panel)`
  198. padding: ${space(3)};
  199. padding-bottom: 0;
  200. background: none;
  201. `;
  202. const Description = styled('div')`
  203. h3 {
  204. font-size: 14px;
  205. text-transform: uppercase;
  206. margin-bottom: ${space(0.25)};
  207. color: ${p => p.theme.gray300};
  208. }
  209. p {
  210. font-size: 13px;
  211. font-weight: bold;
  212. color: ${p => p.theme.textColor};
  213. margin-bottom: ${space(1.5)};
  214. }
  215. `;
  216. const ButtonList = styled('div')`
  217. display: inline-grid;
  218. grid-auto-flow: column;
  219. gap: ${space(1)};
  220. align-items: center;
  221. justify-self: end;
  222. margin-bottom: 16px;
  223. `;
  224. const DocsButton = styled(Button)`
  225. &:focus {
  226. color: ${p => p.theme.white};
  227. }
  228. `;
  229. const SnoozeButton = styled(Button)`
  230. border-right: 0;
  231. border-top-right-radius: 0;
  232. border-bottom-right-radius: 0;
  233. `;
  234. const DismissButton = styled(Button)`
  235. border-top-left-radius: 0;
  236. border-bottom-left-radius: 0;
  237. `;
  238. const ExampleCommitPanel = styled(Panel)`
  239. overflow: hidden;
  240. pointer-events: none;
  241. position: relative;
  242. padding-right: ${space(3)};
  243. &:after {
  244. display: block;
  245. content: 'Example';
  246. position: absolute;
  247. top: 16px;
  248. right: -24px;
  249. text-transform: uppercase;
  250. background: #e46187;
  251. padding: 4px 26px;
  252. line-height: 11px;
  253. font-size: 11px;
  254. color: ${p => p.theme.white};
  255. transform: rotate(45deg);
  256. }
  257. `;
  258. const CustomAvatar = styled('img')`
  259. height: 48px;
  260. padding-right: 12px;
  261. margin: -6px 0px -6px -2px;
  262. `;
  263. const BoxHeader = styled('div')`
  264. display: grid;
  265. align-items: start;
  266. grid-template-columns: repeat(auto-fit, minmax(256px, 1fr));
  267. `;
  268. export default withApi(EventCauseEmpty);