index.tsx 6.9 KB

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