mergedItem.tsx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. import {Component} from 'react';
  2. import styled from '@emotion/styled';
  3. import {Button} from 'sentry/components/button';
  4. import Checkbox from 'sentry/components/checkbox';
  5. import EventOrGroupHeader from 'sentry/components/eventOrGroupHeader';
  6. import {Tooltip} from 'sentry/components/tooltip';
  7. import {IconChevron} from 'sentry/icons';
  8. import {t} from 'sentry/locale';
  9. import GroupingStore, {Fingerprint} from 'sentry/stores/groupingStore';
  10. import {space} from 'sentry/styles/space';
  11. import {Organization} from 'sentry/types';
  12. type Props = {
  13. fingerprint: Fingerprint;
  14. organization: Organization;
  15. totalFingerprint: number;
  16. };
  17. type State = {
  18. busy: boolean;
  19. checked: boolean;
  20. collapsed: boolean;
  21. };
  22. class MergedItem extends Component<Props, State> {
  23. state: State = {
  24. collapsed: false,
  25. checked: false,
  26. busy: false,
  27. };
  28. listener = GroupingStore.listen(data => this.onGroupChange(data), undefined);
  29. onGroupChange = ({unmergeState}) => {
  30. if (!unmergeState) {
  31. return;
  32. }
  33. const {fingerprint} = this.props;
  34. const stateForId = unmergeState.has(fingerprint.id)
  35. ? unmergeState.get(fingerprint.id)
  36. : undefined;
  37. if (!stateForId) {
  38. return;
  39. }
  40. Object.keys(stateForId).forEach(key => {
  41. if (stateForId[key] === this.state[key]) {
  42. return;
  43. }
  44. this.setState(prevState => ({...prevState, [key]: stateForId[key]}));
  45. });
  46. };
  47. handleToggleEvents = () => {
  48. const {fingerprint} = this.props;
  49. GroupingStore.onToggleCollapseFingerprint(fingerprint.id);
  50. };
  51. // Disable default behavior of toggling checkbox
  52. handleLabelClick(event: React.MouseEvent) {
  53. event.preventDefault();
  54. }
  55. handleToggle = () => {
  56. const {fingerprint} = this.props;
  57. const {latestEvent} = fingerprint;
  58. if (this.state.busy) {
  59. return;
  60. }
  61. // clicking anywhere in the row will toggle the checkbox
  62. GroupingStore.onToggleUnmerge([fingerprint.id, latestEvent.id]);
  63. };
  64. handleCheckClick() {
  65. // noop because of react warning about being a controlled input without `onChange`
  66. // we handle change via row click
  67. }
  68. renderFingerprint(id: string, label?: string) {
  69. if (!label) {
  70. return id;
  71. }
  72. return (
  73. <Tooltip title={id}>
  74. <code>{label}</code>
  75. </Tooltip>
  76. );
  77. }
  78. render() {
  79. const {fingerprint, organization, totalFingerprint} = this.props;
  80. const {latestEvent, id, label} = fingerprint;
  81. const {collapsed, busy, checked} = this.state;
  82. const checkboxDisabled = busy || totalFingerprint === 1;
  83. // `latestEvent` can be null if last event w/ fingerprint is not within retention period
  84. return (
  85. <MergedGroup busy={busy}>
  86. <Controls expanded={!collapsed}>
  87. <FingerprintLabel onClick={this.handleToggle}>
  88. <Tooltip
  89. containerDisplayMode="flex"
  90. disabled={!checkboxDisabled}
  91. title={
  92. checkboxDisabled && totalFingerprint === 1
  93. ? t('To check, the list must contain 2 or more items')
  94. : undefined
  95. }
  96. >
  97. <Checkbox
  98. value={id}
  99. checked={checked}
  100. disabled={checkboxDisabled}
  101. onChange={this.handleCheckClick}
  102. size="xs"
  103. />
  104. </Tooltip>
  105. {this.renderFingerprint(id, label)}
  106. </FingerprintLabel>
  107. <Button
  108. aria-label={
  109. collapsed
  110. ? t('Show %s fingerprints', id)
  111. : t('Collapse %s fingerprints', id)
  112. }
  113. size="zero"
  114. borderless
  115. icon={<IconChevron direction={collapsed ? 'down' : 'up'} size="xs" />}
  116. onClick={this.handleToggleEvents}
  117. />
  118. </Controls>
  119. {!collapsed && (
  120. <MergedEventList className="event-list">
  121. {latestEvent && (
  122. <EventDetails className="event-details">
  123. <EventOrGroupHeader
  124. data={latestEvent}
  125. organization={organization}
  126. hideIcons
  127. hideLevel
  128. source="merged-item"
  129. />
  130. </EventDetails>
  131. )}
  132. </MergedEventList>
  133. )}
  134. </MergedGroup>
  135. );
  136. }
  137. }
  138. const MergedGroup = styled('div')<{busy: boolean}>`
  139. ${p => p.busy && 'opacity: 0.2'};
  140. `;
  141. const Controls = styled('div')<{expanded: boolean}>`
  142. display: flex;
  143. justify-content: space-between;
  144. background-color: ${p => p.theme.backgroundSecondary};
  145. ${p => p.expanded && `border-bottom: 1px solid ${p.theme.innerBorder}`};
  146. padding: ${space(0.5)} ${space(1)};
  147. ${MergedGroup}:not(:first-child) & {
  148. border-top: 1px solid ${p => p.theme.innerBorder};
  149. }
  150. ${MergedGroup}:last-child & {
  151. ${p => !p.expanded && `border-bottom: none`};
  152. ${p =>
  153. !p.expanded &&
  154. `border-radius: 0 0 ${p.theme.borderRadius} ${p.theme.borderRadius}`};
  155. }
  156. `;
  157. const FingerprintLabel = styled('label')`
  158. display: flex;
  159. align-items: center;
  160. gap: ${space(1)};
  161. font-family: ${p => p.theme.text.familyMono};
  162. line-height: 1;
  163. font-weight: 400;
  164. margin: 0;
  165. `;
  166. const MergedEventList = styled('div')`
  167. overflow: hidden;
  168. border: none;
  169. background-color: ${p => p.theme.background};
  170. `;
  171. const EventDetails = styled('div')`
  172. display: flex;
  173. justify-content: space-between;
  174. .event-list & {
  175. padding: ${space(1)};
  176. }
  177. `;
  178. export default MergedItem;