index.tsx 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 {
  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. <FieldContainer hasTwoColumns={values.method === MethodType.REPLACE}>
  60. <FieldGroup
  61. label={t('Method')}
  62. help={t('What to do')}
  63. inline={false}
  64. flexibleControlStateSize
  65. stacked
  66. showHelpInTooltip
  67. >
  68. <SelectField
  69. placeholder={t('Select method')}
  70. name="method"
  71. options={sortBy(Object.values(MethodType)).map(value => ({
  72. ...getMethodLabel(value),
  73. value,
  74. }))}
  75. value={method}
  76. onChange={value => onChange('method', value?.value)}
  77. />
  78. </FieldGroup>
  79. {values.method === MethodType.REPLACE && (
  80. <FieldGroup
  81. label={t('Custom Placeholder (Optional)')}
  82. help={t('It will replace the default placeholder [Filtered]')}
  83. inline={false}
  84. flexibleControlStateSize
  85. stacked
  86. showHelpInTooltip
  87. >
  88. <Input
  89. type="text"
  90. name="placeholder"
  91. placeholder={`[${t('Filtered')}]`}
  92. onChange={this.handleChange('placeholder')}
  93. value={values.placeholder}
  94. />
  95. </FieldGroup>
  96. )}
  97. </FieldContainer>
  98. <FieldContainer hasTwoColumns={values.type === RuleType.PATTERN}>
  99. <FieldGroup
  100. data-test-id="type-field"
  101. label={t('Data Type')}
  102. help={t(
  103. 'What to look for. Use an existing pattern or define your own using regular expressions.'
  104. )}
  105. inline={false}
  106. flexibleControlStateSize
  107. stacked
  108. showHelpInTooltip
  109. >
  110. <SelectField
  111. placeholder={t('Select type')}
  112. name="type"
  113. options={sortBy(Object.values(RuleType)).map(value => ({
  114. label: getRuleLabel(value),
  115. value,
  116. }))}
  117. value={type}
  118. onChange={value => onChange('type', value?.value)}
  119. />
  120. </FieldGroup>
  121. {values.type === RuleType.PATTERN && (
  122. <FieldGroup
  123. label={t('Regex matches')}
  124. help={t('Custom regular expression (see documentation)')}
  125. inline={false}
  126. id="regex-matches"
  127. error={errors?.pattern}
  128. flexibleControlStateSize
  129. stacked
  130. required
  131. showHelpInTooltip
  132. >
  133. <RegularExpression
  134. type="text"
  135. name="pattern"
  136. placeholder={t('[a-zA-Z0-9]+')}
  137. onChange={this.handleChange('pattern')}
  138. value={values.pattern}
  139. onBlur={onValidate('pattern')}
  140. id="regex-matches"
  141. />
  142. </FieldGroup>
  143. )}
  144. </FieldContainer>
  145. <ToggleWrapper>
  146. {displayEventId ? (
  147. <Toggle priority="link" onClick={this.handleToggleEventId}>
  148. {t('Hide event ID field')}
  149. <IconChevron direction="up" size="xs" />
  150. </Toggle>
  151. ) : (
  152. <Toggle priority="link" onClick={this.handleToggleEventId}>
  153. {t('Use event ID for auto-completion')}
  154. <IconChevron direction="down" size="xs" />
  155. </Toggle>
  156. )}
  157. </ToggleWrapper>
  158. <SourceGroup isExpanded={displayEventId}>
  159. {displayEventId && (
  160. <EventIdField onUpdateEventId={onUpdateEventId} eventId={eventId} />
  161. )}
  162. <SourceField
  163. onChange={value => onChange('source', value)}
  164. value={source}
  165. error={errors?.source}
  166. onBlur={onValidate('source')}
  167. isRegExMatchesSelected={type === RuleType.PATTERN}
  168. suggestions={sourceSuggestions}
  169. />
  170. </SourceGroup>
  171. </Fragment>
  172. );
  173. }
  174. }
  175. export default Form;
  176. const FieldContainer = styled('div')<{hasTwoColumns: boolean}>`
  177. display: grid;
  178. margin-bottom: ${space(2)};
  179. @media (min-width: ${p => p.theme.breakpoints.small}) {
  180. gap: ${space(2)};
  181. ${p => p.hasTwoColumns && `grid-template-columns: 1fr 1fr;`}
  182. margin-bottom: ${p => (p.hasTwoColumns ? 0 : space(2))};
  183. }
  184. `;
  185. const SourceGroup = styled('div')<{isExpanded: boolean}>`
  186. height: 65px;
  187. transition: all 300ms cubic-bezier(0.4, 0, 0.2, 1) 0ms;
  188. transition-property: height;
  189. ${p =>
  190. p.isExpanded &&
  191. `
  192. border-radius: ${p.theme.borderRadius};
  193. border: 1px solid ${p.theme.border};
  194. box-shadow: ${p.theme.dropShadowMedium};
  195. margin: ${space(2)} 0 ${space(3)} 0;
  196. padding: ${space(2)};
  197. height: 180px;
  198. `}
  199. `;
  200. const RegularExpression = styled(Input)`
  201. font-family: ${p => p.theme.text.familyMono};
  202. `;
  203. const ToggleWrapper = styled('div')`
  204. display: flex;
  205. justify-content: flex-end;
  206. `;
  207. const Toggle = styled(Button)`
  208. font-weight: 700;
  209. color: ${p => p.theme.subText};
  210. &:hover,
  211. &:focus {
  212. color: ${p => p.theme.textColor};
  213. }
  214. > *:first-child {
  215. display: grid;
  216. gap: ${space(0.5)};
  217. grid-template-columns: repeat(2, max-content);
  218. align-items: center;
  219. }
  220. `;