item.tsx 6.4 KB

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