item.tsx 5.6 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/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. organization: Organization;
  24. project: Project;
  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, organization} = this.props;
  81. const {visible, busy} = this.state;
  82. const similarInterfaces = ['exception', 'message'];
  83. const hasSimilarityEmbeddingsFeature = organization?.features?.includes(
  84. 'issues-similarity-embeddings'
  85. );
  86. if (!visible) {
  87. return null;
  88. }
  89. const cx = classNames('group', {
  90. isResolved: issue.status === 'resolved',
  91. busy,
  92. });
  93. return (
  94. <StyledPanelItem
  95. data-test-id="similar-item-row"
  96. className={cx}
  97. onClick={this.handleToggle}
  98. >
  99. <Details>
  100. <Checkbox
  101. id={issue.id}
  102. value={issue.id}
  103. checked={this.state.checked}
  104. onChange={this.handleCheckClick}
  105. />
  106. <EventDetails>
  107. <EventOrGroupHeader data={issue} size="normal" source="similar-issues" />
  108. <EventOrGroupExtraDetails data={{...issue, lastSeen: ''}} showAssignee />
  109. </EventDetails>
  110. <Diff>
  111. <Button onClick={this.handleShowDiff} size="sm">
  112. {t('Diff')}
  113. </Button>
  114. </Diff>
  115. </Details>
  116. <Columns>
  117. <StyledCount value={issue.count} />
  118. {similarInterfaces.map(interfaceName => {
  119. const avgScore = aggregate?.[interfaceName];
  120. const scoreList = scoresByInterface?.[interfaceName] || [];
  121. // Check for valid number (and not NaN)
  122. const scoreValue =
  123. typeof avgScore === 'number' && !Number.isNaN(avgScore) ? avgScore : 0;
  124. return (
  125. <Column key={interfaceName}>
  126. {!hasSimilarityEmbeddingsFeature && (
  127. <Hovercard
  128. body={scoreList.length && <SimilarScoreCard scoreList={scoreList} />}
  129. >
  130. <ScoreBar vertical score={Math.round(scoreValue * 5)} />
  131. </Hovercard>
  132. )}
  133. {hasSimilarityEmbeddingsFeature && <div>{scoreValue.toFixed(4)}</div>}
  134. </Column>
  135. );
  136. })}
  137. </Columns>
  138. </StyledPanelItem>
  139. );
  140. }
  141. }
  142. const Details = styled('div')`
  143. ${p => p.theme.overflowEllipsis};
  144. display: grid;
  145. gap: ${space(1)};
  146. grid-template-columns: max-content auto max-content;
  147. margin-left: ${space(2)};
  148. `;
  149. const StyledPanelItem = styled(PanelItem)`
  150. padding: ${space(1)} 0;
  151. `;
  152. const Columns = styled('div')`
  153. display: flex;
  154. align-items: center;
  155. flex-shrink: 0;
  156. min-width: 300px;
  157. width: 300px;
  158. `;
  159. const columnStyle = css`
  160. flex: 1;
  161. flex-shrink: 0;
  162. display: flex;
  163. justify-content: center;
  164. padding: ${space(0.5)} 0;
  165. `;
  166. const Column = styled('div')`
  167. ${columnStyle}
  168. `;
  169. const StyledCount = styled(Count)`
  170. ${columnStyle}
  171. font-variant-numeric: tabular-nums;
  172. `;
  173. const Diff = styled('div')`
  174. display: flex;
  175. align-items: center;
  176. margin-right: ${space(0.25)};
  177. `;
  178. const EventDetails = styled('div')`
  179. flex: 1;
  180. ${p => p.theme.overflowEllipsis};
  181. `;
  182. export default Item;