item.tsx 6.3 KB

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