item.tsx 5.3 KB

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