item.tsx 6.4 KB

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