item.tsx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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
  105. data={issue}
  106. includeLink
  107. size="normal"
  108. source="similar-issues"
  109. />
  110. <EventOrGroupExtraDetails data={{...issue, lastSeen: ''}} showAssignee />
  111. </EventDetails>
  112. <Diff>
  113. <Button onClick={this.handleShowDiff} size="sm">
  114. {t('Diff')}
  115. </Button>
  116. </Diff>
  117. </Details>
  118. <Columns>
  119. <StyledCount value={issue.count} />
  120. {similarInterfaces.map(interfaceName => {
  121. const avgScore = aggregate?.[interfaceName];
  122. const scoreList = scoresByInterface?.[interfaceName] || [];
  123. // Check for valid number (and not NaN)
  124. const scoreValue =
  125. typeof avgScore === 'number' && !Number.isNaN(avgScore) ? avgScore : 0;
  126. return (
  127. <Column key={interfaceName}>
  128. <Hovercard
  129. body={scoreList.length && <SimilarScoreCard scoreList={scoreList} />}
  130. >
  131. <ScoreBar vertical score={Math.round(scoreValue * 5)} />
  132. </Hovercard>
  133. </Column>
  134. );
  135. })}
  136. </Columns>
  137. </StyledPanelItem>
  138. );
  139. }
  140. }
  141. const Details = styled('div')`
  142. ${p => p.theme.overflowEllipsis};
  143. display: grid;
  144. gap: ${space(1)};
  145. grid-template-columns: max-content auto max-content;
  146. margin-left: ${space(2)};
  147. input[type='checkbox'] {
  148. margin: 0;
  149. }
  150. `;
  151. const StyledPanelItem = styled(PanelItem)`
  152. padding: ${space(1)} 0;
  153. `;
  154. const Columns = styled('div')`
  155. display: flex;
  156. align-items: center;
  157. flex-shrink: 0;
  158. min-width: 300px;
  159. width: 300px;
  160. `;
  161. const columnStyle = css`
  162. flex: 1;
  163. flex-shrink: 0;
  164. display: flex;
  165. justify-content: center;
  166. padding: ${space(0.5)} 0;
  167. `;
  168. const Column = styled('div')`
  169. ${columnStyle}
  170. `;
  171. const StyledCount = styled(Count)`
  172. ${columnStyle}
  173. font-variant-numeric: tabular-nums;
  174. `;
  175. const Diff = styled('div')`
  176. display: flex;
  177. align-items: center;
  178. margin-right: ${space(0.25)};
  179. `;
  180. const EventDetails = styled('div')`
  181. flex: 1;
  182. ${p => p.theme.overflowEllipsis};
  183. `;
  184. export default Item;