mergedItem.tsx 5.0 KB

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