item.tsx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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: any) => 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}: any) => {
  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. // @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
  82. if (stateForId[key] === this.state[key]) {
  83. return;
  84. }
  85. this.setState(prevState => ({
  86. ...prevState,
  87. [key]: stateForId[key],
  88. }));
  89. });
  90. };
  91. render() {
  92. const {aggregate, scoresByInterface, issue, hasSimilarityEmbeddingsFeature} =
  93. this.props;
  94. const {visible, busy} = this.state;
  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. // @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
  132. const avgScore = aggregate?.[interfaceName];
  133. // @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
  134. const scoreList = scoresByInterface?.[interfaceName] || [];
  135. // Check for valid number (and not NaN)
  136. let scoreValue =
  137. typeof avgScore === 'number' && !Number.isNaN(avgScore) ? avgScore : 0;
  138. // If hasSimilarityEmbeddingsFeature is on, translate similarity score in range 0.9-1 to score between 1-5
  139. if (hasSimilarityEmbeddingsFeature) {
  140. for (let i = 0; i <= similarityEmbeddingScoreValues.length; i++) {
  141. if (scoreValue <= similarityEmbeddingScoreValues[i]!) {
  142. scoreValue = i;
  143. break;
  144. }
  145. }
  146. }
  147. return (
  148. <Column key={interfaceName}>
  149. {!hasSimilarityEmbeddingsFeature && (
  150. <Hovercard
  151. body={scoreList.length && <SimilarScoreCard scoreList={scoreList} />}
  152. >
  153. <ScoreBar vertical score={Math.round(scoreValue * 5)} />
  154. </Hovercard>
  155. )}
  156. {hasSimilarityEmbeddingsFeature && (
  157. <ScoreBar vertical score={scoreValue} />
  158. )}
  159. </Column>
  160. );
  161. })}
  162. </Columns>
  163. </StyledPanelItem>
  164. );
  165. }
  166. }
  167. const Details = styled('div')`
  168. ${p => p.theme.overflowEllipsis};
  169. display: grid;
  170. align-items: start;
  171. gap: ${space(1)};
  172. grid-template-columns: max-content auto max-content;
  173. margin-left: ${space(2)};
  174. `;
  175. const StyledPanelItem = styled(PanelItem)`
  176. padding: ${space(1)} 0;
  177. `;
  178. const Columns = styled('div')`
  179. display: flex;
  180. align-items: center;
  181. flex-shrink: 0;
  182. min-width: 350px;
  183. width: 350px;
  184. `;
  185. const columnStyle = css`
  186. flex: 1;
  187. flex-shrink: 0;
  188. display: flex;
  189. justify-content: center;
  190. padding: ${space(0.5)} 0;
  191. `;
  192. const Column = styled('div')`
  193. ${columnStyle}
  194. `;
  195. const StyledCount = styled(Count)`
  196. ${columnStyle}
  197. font-variant-numeric: tabular-nums;
  198. `;
  199. const Diff = styled('div')`
  200. height: 100%;
  201. display: flex;
  202. align-items: center;
  203. margin-right: ${space(0.25)};
  204. `;
  205. const EventDetails = styled('div')`
  206. flex: 1;
  207. ${p => p.theme.overflowEllipsis};
  208. `;
  209. export default Item;