item.tsx 6.7 KB

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