item.tsx 5.4 KB

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