item.tsx 6.1 KB

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