index.tsx 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. import {Component, Fragment} from 'react';
  2. import styled from '@emotion/styled';
  3. import sortBy from 'lodash/sortBy';
  4. import {Button} from 'sentry/components/button';
  5. import FieldGroup from 'sentry/components/forms/fieldGroup';
  6. import Input from 'sentry/components/input';
  7. import {IconChevron} from 'sentry/icons';
  8. import {t} from 'sentry/locale';
  9. import {space} from 'sentry/styles/space';
  10. import type {EventId, KeysOfUnion, Rule, SourceSuggestion} from '../../types';
  11. import {MethodType, RuleType} from '../../types';
  12. import {getMethodLabel, getRuleLabel} from '../../utils';
  13. import EventIdField from './eventIdField';
  14. import SelectField from './selectField';
  15. import SourceField from './sourceField';
  16. type Values = Omit<Record<KeysOfUnion<Rule>, string>, 'id'>;
  17. type Props<V extends Values, K extends keyof V> = {
  18. errors: Partial<V>;
  19. eventId: EventId;
  20. onChange: (field: K, value: string) => void;
  21. onUpdateEventId: (eventId: string) => void;
  22. onValidate: (field: K) => () => void;
  23. sourceSuggestions: Array<SourceSuggestion>;
  24. values: V;
  25. };
  26. type State = {
  27. displayEventId: boolean;
  28. };
  29. class Form extends Component<Props<Values, KeysOfUnion<Values>>, State> {
  30. state: State = {displayEventId: !!this.props.eventId?.value};
  31. handleChange =
  32. <K extends keyof Values>(field: K) =>
  33. (event: React.ChangeEvent<HTMLInputElement>) => {
  34. this.props.onChange(field, event.target.value);
  35. };
  36. handleToggleEventId = () => {
  37. this.setState(prevState => ({displayEventId: !prevState.displayEventId}));
  38. };
  39. render() {
  40. const {
  41. values,
  42. onChange,
  43. errors,
  44. onValidate,
  45. sourceSuggestions,
  46. onUpdateEventId,
  47. eventId,
  48. } = this.props;
  49. const {method, type, source} = values;
  50. const {displayEventId} = this.state;
  51. return (
  52. <Fragment>
  53. <FieldContainer hasTwoColumns={values.method === MethodType.REPLACE}>
  54. <FieldGroup
  55. label={t('Method')}
  56. help={t('What to do')}
  57. inline={false}
  58. flexibleControlStateSize
  59. stacked
  60. showHelpInTooltip
  61. >
  62. <SelectField
  63. placeholder={t('Select method')}
  64. name="method"
  65. options={sortBy(Object.values(MethodType)).map(value => ({
  66. ...getMethodLabel(value),
  67. value,
  68. }))}
  69. value={method}
  70. onChange={value => onChange('method', value?.value)}
  71. />
  72. </FieldGroup>
  73. {values.method === MethodType.REPLACE && (
  74. <FieldGroup
  75. label={t('Custom Placeholder (Optional)')}
  76. help={t('It will replace the default placeholder [Filtered]')}
  77. inline={false}
  78. flexibleControlStateSize
  79. stacked
  80. showHelpInTooltip
  81. >
  82. <Input
  83. type="text"
  84. name="placeholder"
  85. placeholder={`[${t('Filtered')}]`}
  86. onChange={this.handleChange('placeholder')}
  87. value={values.placeholder}
  88. />
  89. </FieldGroup>
  90. )}
  91. </FieldContainer>
  92. <FieldContainer hasTwoColumns={values.type === RuleType.PATTERN}>
  93. <FieldGroup
  94. data-test-id="type-field"
  95. label={t('Data Type')}
  96. help={t(
  97. 'What to look for. Use an existing pattern or define your own using regular expressions.'
  98. )}
  99. inline={false}
  100. flexibleControlStateSize
  101. stacked
  102. showHelpInTooltip
  103. >
  104. <SelectField
  105. placeholder={t('Select type')}
  106. name="type"
  107. options={sortBy(Object.values(RuleType)).map(value => ({
  108. label: getRuleLabel(value),
  109. value,
  110. }))}
  111. value={type}
  112. onChange={value => onChange('type', value?.value)}
  113. />
  114. </FieldGroup>
  115. {values.type === RuleType.PATTERN && (
  116. <FieldGroup
  117. label={t('Regex matches')}
  118. help={t('Custom regular expression (see documentation)')}
  119. inline={false}
  120. id="regex-matches"
  121. error={errors?.pattern}
  122. flexibleControlStateSize
  123. stacked
  124. required
  125. showHelpInTooltip
  126. >
  127. <RegularExpression
  128. type="text"
  129. name="pattern"
  130. placeholder={t('[a-zA-Z0-9]+')}
  131. onChange={this.handleChange('pattern')}
  132. value={values.pattern}
  133. onBlur={onValidate('pattern')}
  134. id="regex-matches"
  135. />
  136. </FieldGroup>
  137. )}
  138. </FieldContainer>
  139. <ToggleWrapper>
  140. {displayEventId ? (
  141. <Toggle priority="link" onClick={this.handleToggleEventId}>
  142. {t('Hide event ID field')}
  143. <IconChevron direction="up" size="xs" />
  144. </Toggle>
  145. ) : (
  146. <Toggle priority="link" onClick={this.handleToggleEventId}>
  147. {t('Use event ID for auto-completion')}
  148. <IconChevron direction="down" size="xs" />
  149. </Toggle>
  150. )}
  151. </ToggleWrapper>
  152. <SourceGroup isExpanded={displayEventId}>
  153. {displayEventId && (
  154. <EventIdField onUpdateEventId={onUpdateEventId} eventId={eventId} />
  155. )}
  156. <SourceField
  157. onChange={value => onChange('source', value)}
  158. value={source}
  159. error={errors?.source}
  160. onBlur={onValidate('source')}
  161. isRegExMatchesSelected={type === RuleType.PATTERN}
  162. suggestions={sourceSuggestions}
  163. />
  164. </SourceGroup>
  165. </Fragment>
  166. );
  167. }
  168. }
  169. export default Form;
  170. const FieldContainer = styled('div')<{hasTwoColumns: boolean}>`
  171. display: grid;
  172. margin-bottom: ${space(2)};
  173. @media (min-width: ${p => p.theme.breakpoints.small}) {
  174. gap: ${space(2)};
  175. ${p => p.hasTwoColumns && `grid-template-columns: 1fr 1fr;`}
  176. margin-bottom: ${p => (p.hasTwoColumns ? 0 : space(2))};
  177. }
  178. `;
  179. const SourceGroup = styled('div')<{isExpanded: boolean}>`
  180. height: 65px;
  181. transition: all 300ms cubic-bezier(0.4, 0, 0.2, 1) 0ms;
  182. transition-property: height;
  183. ${p =>
  184. p.isExpanded &&
  185. `
  186. border-radius: ${p.theme.borderRadius};
  187. border: 1px solid ${p.theme.border};
  188. box-shadow: ${p.theme.dropShadowMedium};
  189. margin: ${space(2)} 0 ${space(3)} 0;
  190. padding: ${space(2)};
  191. height: 180px;
  192. `}
  193. `;
  194. const RegularExpression = styled(Input)`
  195. font-family: ${p => p.theme.text.familyMono};
  196. `;
  197. const ToggleWrapper = styled('div')`
  198. display: flex;
  199. justify-content: flex-end;
  200. `;
  201. const Toggle = styled(Button)`
  202. font-weight: 700;
  203. color: ${p => p.theme.subText};
  204. &:hover,
  205. &:focus {
  206. color: ${p => p.theme.textColor};
  207. }
  208. > *:first-child {
  209. display: grid;
  210. gap: ${space(0.5)};
  211. grid-template-columns: repeat(2, max-content);
  212. align-items: center;
  213. }
  214. `;