eventCauseEmpty.tsx 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. if (!this.state.shouldShow) {
  137. return null;
  138. }
  139. return (
  140. <DataSection data-test-id="loaded-event-cause-empty">
  141. <StyledPanel dashedBorder>
  142. <BoxHeader>
  143. <Description>
  144. <h3>{t('Configure Suspect Commits')}</h3>
  145. <p>{t('To identify which commit caused this issue')}</p>
  146. </Description>
  147. <ButtonList>
  148. <DocsButton
  149. size="sm"
  150. priority="primary"
  151. href="https://docs.sentry.io/product/releases/setup/"
  152. onClick={() => this.trackAnalytics('event_cause.docs_clicked')}
  153. >
  154. {t('Read the docs')}
  155. </DocsButton>
  156. <div>
  157. <SnoozeButton
  158. title={t('Remind me next week')}
  159. size="sm"
  160. onClick={() =>
  161. this.handleClick({
  162. action: 'snoozed',
  163. eventKey: 'event_cause.snoozed',
  164. })
  165. }
  166. >
  167. {t('Snooze')}
  168. </SnoozeButton>
  169. <DismissButton
  170. title={t('Dismiss for this project')}
  171. size="sm"
  172. onClick={() =>
  173. this.handleClick({
  174. action: 'dismissed',
  175. eventKey: 'event_cause.dismissed',
  176. })
  177. }
  178. >
  179. {t('Dismiss')}
  180. </DismissButton>
  181. </div>
  182. </ButtonList>
  183. </BoxHeader>
  184. <ExampleCommitPanel>
  185. <CommitRow
  186. key={DUMMY_COMMIT.id}
  187. commit={DUMMY_COMMIT}
  188. customAvatar={<CustomAvatar src={codesworth} />}
  189. />
  190. </ExampleCommitPanel>
  191. </StyledPanel>
  192. </DataSection>
  193. );
  194. }
  195. }
  196. const StyledPanel = styled(Panel)`
  197. padding: ${space(3)};
  198. padding-bottom: 0;
  199. background: none;
  200. `;
  201. const Description = styled('div')`
  202. h3 {
  203. font-size: 14px;
  204. text-transform: uppercase;
  205. margin-bottom: ${space(0.25)};
  206. color: ${p => p.theme.gray300};
  207. }
  208. p {
  209. font-size: 13px;
  210. font-weight: bold;
  211. color: ${p => p.theme.textColor};
  212. margin-bottom: ${space(1.5)};
  213. }
  214. `;
  215. const ButtonList = styled('div')`
  216. display: inline-grid;
  217. grid-auto-flow: column;
  218. gap: ${space(1)};
  219. align-items: center;
  220. justify-self: end;
  221. margin-bottom: 16px;
  222. `;
  223. const DocsButton = styled(Button)`
  224. &:focus {
  225. color: ${p => p.theme.white};
  226. }
  227. `;
  228. const SnoozeButton = styled(Button)`
  229. border-right: 0;
  230. border-top-right-radius: 0;
  231. border-bottom-right-radius: 0;
  232. `;
  233. const DismissButton = styled(Button)`
  234. border-top-left-radius: 0;
  235. border-bottom-left-radius: 0;
  236. `;
  237. const ExampleCommitPanel = styled(Panel)`
  238. overflow: hidden;
  239. pointer-events: none;
  240. position: relative;
  241. padding-right: ${space(3)};
  242. &:after {
  243. display: block;
  244. content: 'Example';
  245. position: absolute;
  246. top: 16px;
  247. right: -24px;
  248. text-transform: uppercase;
  249. background: #e46187;
  250. padding: 4px 26px;
  251. line-height: 11px;
  252. font-size: 11px;
  253. color: ${p => p.theme.white};
  254. transform: rotate(45deg);
  255. }
  256. `;
  257. const CustomAvatar = styled('img')`
  258. height: 48px;
  259. padding-right: 12px;
  260. margin: -6px 0px -6px -2px;
  261. `;
  262. const BoxHeader = styled('div')`
  263. display: grid;
  264. align-items: start;
  265. grid-template-columns: repeat(auto-fit, minmax(256px, 1fr));
  266. `;
  267. export default withApi(EventCauseEmpty);