item.tsx 6.3 KB

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