item.tsx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. import {Component} from 'react';
  2. import {css} from '@emotion/react';
  3. import styled from '@emotion/styled';
  4. import classNames from 'classnames';
  5. import {openDiffModal} from 'sentry/actionCreators/modal';
  6. import Button from 'sentry/components/button';
  7. import Checkbox from 'sentry/components/checkbox';
  8. import Count from 'sentry/components/count';
  9. import EventOrGroupExtraDetails from 'sentry/components/eventOrGroupExtraDetails';
  10. import EventOrGroupHeader from 'sentry/components/eventOrGroupHeader';
  11. import {Hovercard} from 'sentry/components/hovercard';
  12. import {PanelItem} from 'sentry/components/panels';
  13. import ScoreBar from 'sentry/components/scoreBar';
  14. import SimilarScoreCard from 'sentry/components/similarScoreCard';
  15. import {t} from 'sentry/locale';
  16. import GroupingStore from 'sentry/stores/groupingStore';
  17. import space from 'sentry/styles/space';
  18. import {Group, Organization, Project} from 'sentry/types';
  19. type Props = {
  20. groupId: Group['id'];
  21. issue: Group;
  22. orgId: Organization['id'];
  23. project: Project;
  24. v2: boolean;
  25. aggregate?: {
  26. exception: number;
  27. message: number;
  28. };
  29. score?: Record<string, any>;
  30. scoresByInterface?: {
  31. exception: Array<[string, number | null]>;
  32. message: Array<[string, any | null]>;
  33. };
  34. };
  35. const initialState = {visible: true, checked: false, busy: false};
  36. type State = typeof initialState;
  37. class Item extends Component<Props, State> {
  38. state: State = initialState;
  39. componentWillUnmount() {
  40. this.listener?.();
  41. }
  42. listener = GroupingStore.listen(data => this.onGroupChange(data), undefined);
  43. handleToggle = () => {
  44. const {issue} = this.props;
  45. // clicking anywhere in the row will toggle the checkbox
  46. if (!this.state.busy) {
  47. GroupingStore.onToggleMerge(issue.id);
  48. }
  49. };
  50. handleShowDiff = (event: React.MouseEvent) => {
  51. const {orgId, groupId: baseIssueId, issue, project} = this.props;
  52. const {id: targetIssueId} = issue;
  53. openDiffModal({baseIssueId, targetIssueId, project, orgId});
  54. event.stopPropagation();
  55. };
  56. handleCheckClick = () => {
  57. // noop to appease React warnings
  58. // This is controlled via row click instead of only Checkbox
  59. };
  60. onGroupChange = ({mergeState}) => {
  61. if (!mergeState) {
  62. return;
  63. }
  64. const {issue} = this.props;
  65. const stateForId = mergeState.has(issue.id) && mergeState.get(issue.id);
  66. if (!stateForId) {
  67. return;
  68. }
  69. Object.keys(stateForId).forEach(key => {
  70. if (stateForId[key] === this.state[key]) {
  71. return;
  72. }
  73. this.setState(prevState => ({
  74. ...prevState,
  75. [key]: stateForId[key],
  76. }));
  77. });
  78. };
  79. render() {
  80. const {aggregate, scoresByInterface, issue, v2} = this.props;
  81. const {visible, busy} = this.state;
  82. const similarInterfaces = v2 ? ['similarity'] : ['exception', 'message'];
  83. if (!visible) {
  84. return null;
  85. }
  86. const cx = classNames('group', {
  87. isResolved: issue.status === 'resolved',
  88. busy,
  89. });
  90. return (
  91. <StyledPanelItem
  92. data-test-id="similar-item-row"
  93. className={cx}
  94. onClick={this.handleToggle}
  95. >
  96. <Details>
  97. <Checkbox
  98. id={issue.id}
  99. value={issue.id}
  100. checked={this.state.checked}
  101. onChange={this.handleCheckClick}
  102. />
  103. <EventDetails>
  104. <EventOrGroupHeader data={issue} includeLink size="normal" />
  105. <EventOrGroupExtraDetails data={{...issue, lastSeen: ''}} showAssignee />
  106. </EventDetails>
  107. <Diff>
  108. <Button onClick={this.handleShowDiff} size="sm">
  109. {t('Diff')}
  110. </Button>
  111. </Diff>
  112. </Details>
  113. <Columns>
  114. <StyledCount value={issue.count} />
  115. {similarInterfaces.map(interfaceName => {
  116. const avgScore = aggregate?.[interfaceName];
  117. const scoreList = scoresByInterface?.[interfaceName] || [];
  118. // Check for valid number (and not NaN)
  119. const scoreValue =
  120. typeof avgScore === 'number' && !Number.isNaN(avgScore) ? avgScore : 0;
  121. return (
  122. <Column key={interfaceName}>
  123. <Hovercard
  124. body={scoreList.length && <SimilarScoreCard scoreList={scoreList} />}
  125. >
  126. <ScoreBar vertical score={Math.round(scoreValue * 5)} />
  127. </Hovercard>
  128. </Column>
  129. );
  130. })}
  131. </Columns>
  132. </StyledPanelItem>
  133. );
  134. }
  135. }
  136. const Details = styled('div')`
  137. ${p => p.theme.overflowEllipsis};
  138. display: grid;
  139. gap: ${space(1)};
  140. grid-template-columns: max-content auto max-content;
  141. margin-left: ${space(2)};
  142. input[type='checkbox'] {
  143. margin: 0;
  144. }
  145. `;
  146. const StyledPanelItem = styled(PanelItem)`
  147. padding: ${space(1)} 0;
  148. `;
  149. const Columns = styled('div')`
  150. display: flex;
  151. align-items: center;
  152. flex-shrink: 0;
  153. min-width: 300px;
  154. width: 300px;
  155. `;
  156. const columnStyle = css`
  157. flex: 1;
  158. flex-shrink: 0;
  159. display: flex;
  160. justify-content: center;
  161. padding: ${space(0.5)} 0;
  162. `;
  163. const Column = styled('div')`
  164. ${columnStyle}
  165. `;
  166. const StyledCount = styled(Count)`
  167. ${columnStyle}
  168. font-variant-numeric: tabular-nums;
  169. `;
  170. const Diff = styled('div')`
  171. display: flex;
  172. align-items: center;
  173. margin-right: ${space(0.25)};
  174. `;
  175. const EventDetails = styled('div')`
  176. flex: 1;
  177. ${p => p.theme.overflowEllipsis};
  178. `;
  179. export default Item;