eventIdField.tsx 3.9 KB

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