eventIdField.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import {Component} from 'react';
  2. import styled from '@emotion/styled';
  3. import isEqual from 'lodash/isEqual';
  4. import FieldGroup from 'sentry/components/forms/fieldGroup';
  5. import Input from 'sentry/components/input';
  6. import {t} from 'sentry/locale';
  7. import {space} from 'sentry/styles/space';
  8. import type {EventId} from '../../types';
  9. import {EventIdStatus} from '../../types';
  10. import {saveToSourceGroupData} from '../utils';
  11. import EventIdFieldStatusIcon from './eventIdFieldStatusIcon';
  12. type Props = {
  13. eventId: EventId;
  14. onUpdateEventId: (eventId: string) => void;
  15. disabled?: boolean;
  16. };
  17. type State = {
  18. status: EventIdStatus;
  19. value: string;
  20. };
  21. class EventIdField extends Component<Props, State> {
  22. state: State = {...this.props.eventId};
  23. componentDidUpdate(prevProps: Props) {
  24. if (!isEqual(prevProps.eventId, this.props.eventId)) {
  25. this.loadState();
  26. }
  27. }
  28. loadState() {
  29. this.setState({
  30. ...this.props.eventId,
  31. });
  32. }
  33. getErrorMessage(): string | undefined {
  34. const {status} = this.state;
  35. switch (status) {
  36. case EventIdStatus.INVALID:
  37. return t('This event ID is invalid');
  38. case EventIdStatus.ERROR:
  39. return t(
  40. 'An error occurred while fetching the suggestions based on this event ID'
  41. );
  42. case EventIdStatus.NOT_FOUND:
  43. return t('The chosen event ID was not found in projects you have access to');
  44. default:
  45. return undefined;
  46. }
  47. }
  48. isEventIdValid(): boolean {
  49. const {value, status} = this.state;
  50. if (value && value.length !== 32) {
  51. if (status !== EventIdStatus.INVALID) {
  52. saveToSourceGroupData({value, status});
  53. this.setState({status: EventIdStatus.INVALID});
  54. }
  55. return false;
  56. }
  57. return true;
  58. }
  59. handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
  60. const eventId = event.target.value.replace(/-/g, '').trim();
  61. if (eventId !== this.state.value) {
  62. this.setState({
  63. value: eventId,
  64. status: EventIdStatus.UNDEFINED,
  65. });
  66. }
  67. };
  68. handleBlur = (event: React.FocusEvent<HTMLInputElement>) => {
  69. event.preventDefault();
  70. if (this.isEventIdValid()) {
  71. this.props.onUpdateEventId(this.state.value);
  72. }
  73. };
  74. handleKeyDown = (event: React.KeyboardEvent<HTMLInputElement>) => {
  75. const {key} = event;
  76. if (key === 'Enter' && this.isEventIdValid()) {
  77. this.props.onUpdateEventId(this.state.value);
  78. }
  79. };
  80. handleClickIconClose = () => {
  81. this.setState({
  82. value: '',
  83. status: EventIdStatus.UNDEFINED,
  84. });
  85. };
  86. render() {
  87. const {disabled} = this.props;
  88. const {value, status} = this.state;
  89. return (
  90. <FieldGroup
  91. data-test-id="event-id-field"
  92. label={t('Event ID (Optional)')}
  93. help={t(
  94. 'Providing an event ID will automatically provide you a list of suggested sources'
  95. )}
  96. inline={false}
  97. error={this.getErrorMessage()}
  98. flexibleControlStateSize
  99. stacked
  100. showHelpInTooltip
  101. >
  102. <FieldWrapper>
  103. <StyledInput
  104. type="text"
  105. name="eventId"
  106. disabled={disabled}
  107. value={value}
  108. placeholder={t('XXXXXXXXXXXXXX')}
  109. onChange={this.handleChange}
  110. onKeyDown={this.handleKeyDown}
  111. onBlur={this.handleBlur}
  112. />
  113. <Status>
  114. <EventIdFieldStatusIcon
  115. onClickIconClose={this.handleClickIconClose}
  116. status={status}
  117. />
  118. </Status>
  119. </FieldWrapper>
  120. </FieldGroup>
  121. );
  122. }
  123. }
  124. export default EventIdField;
  125. const StyledInput = styled(Input)`
  126. flex: 1;
  127. font-weight: ${p => p.theme.fontWeightNormal};
  128. input {
  129. padding-right: ${space(1.5)};
  130. }
  131. margin-bottom: 0;
  132. `;
  133. const Status = styled('div')`
  134. height: 100%;
  135. position: absolute;
  136. right: ${space(1.5)};
  137. top: 0;
  138. display: flex;
  139. align-items: center;
  140. `;
  141. const FieldWrapper = styled('div')`
  142. position: relative;
  143. display: flex;
  144. align-items: center;
  145. `;