modalManager.tsx 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. import {Component} from 'react';
  2. import isEqual from 'lodash/isEqual';
  3. import omit from 'lodash/omit';
  4. import {addErrorMessage} from 'sentry/actionCreators/indicator';
  5. import type {ModalRenderProps} from 'sentry/actionCreators/modal';
  6. import type {Client} from 'sentry/api';
  7. import {t} from 'sentry/locale';
  8. import type {Organization} from 'sentry/types/organization';
  9. import type {Project} from 'sentry/types/project';
  10. import submitRules from '../submitRules';
  11. import type {KeysOfUnion, Rule} from '../types';
  12. import {EventIdStatus, MethodType, RuleType} from '../types';
  13. import {valueSuggestions} from '../utils';
  14. import Form from './form';
  15. import handleError, {ErrorType} from './handleError';
  16. import Modal from './modal';
  17. import {fetchSourceGroupData, saveToSourceGroupData} from './utils';
  18. type FormProps = React.ComponentProps<typeof Form>;
  19. type Values = FormProps['values'];
  20. type EventId = NonNullable<FormProps['eventId']>;
  21. type SourceSuggestions = FormProps['sourceSuggestions'];
  22. type Props = ModalRenderProps & {
  23. api: Client;
  24. endpoint: string;
  25. onGetNewRules: (values: Values) => Rule[];
  26. onSubmitSuccess: (data: {relayPiiConfig: string}) => void;
  27. orgSlug: Organization['slug'];
  28. savedRules: Rule[];
  29. title: string;
  30. initialState?: Partial<Values>;
  31. projectId?: Project['id'];
  32. };
  33. type State = {
  34. errors: FormProps['errors'];
  35. eventId: EventId;
  36. isFormValid: boolean;
  37. requiredValues: Array<keyof Values>;
  38. sourceSuggestions: SourceSuggestions;
  39. values: Values;
  40. };
  41. class ModalManager extends Component<Props, State> {
  42. state = this.getDefaultState();
  43. componentDidMount() {
  44. this.handleValidateForm();
  45. }
  46. componentDidUpdate(_prevProps: Props, prevState: State) {
  47. if (!isEqual(prevState.values, this.state.values)) {
  48. this.handleValidateForm();
  49. }
  50. if (prevState.eventId.value !== this.state.eventId.value) {
  51. this.loadSourceSuggestions();
  52. }
  53. if (prevState.eventId.status !== this.state.eventId.status) {
  54. saveToSourceGroupData(this.state.eventId, this.state.sourceSuggestions);
  55. }
  56. }
  57. getDefaultState(): Readonly<State> {
  58. const {eventId, sourceSuggestions} = fetchSourceGroupData();
  59. const values = this.getInitialValues();
  60. return {
  61. values,
  62. requiredValues: this.getRequiredValues(values),
  63. errors: {},
  64. isFormValid: false,
  65. eventId: {
  66. value: eventId,
  67. status: !eventId ? EventIdStatus.UNDEFINED : EventIdStatus.LOADED,
  68. },
  69. sourceSuggestions,
  70. } as Readonly<State>;
  71. }
  72. getInitialValues() {
  73. const {initialState} = this.props;
  74. return {
  75. type: initialState?.type ?? RuleType.CREDITCARD,
  76. method: initialState?.method ?? MethodType.MASK,
  77. source: initialState?.source ?? '',
  78. placeholder: initialState?.placeholder ?? '',
  79. pattern: initialState?.pattern ?? '',
  80. };
  81. }
  82. getRequiredValues(values: Values) {
  83. const {type} = values;
  84. const requiredValues: Array<KeysOfUnion<Values>> = ['type', 'method', 'source'];
  85. if (type === RuleType.PATTERN) {
  86. requiredValues.push('pattern');
  87. }
  88. return requiredValues;
  89. }
  90. clearError<F extends keyof Values>(field: F) {
  91. this.setState(prevState => ({
  92. errors: omit(prevState.errors, field),
  93. }));
  94. }
  95. async loadSourceSuggestions() {
  96. const {orgSlug, projectId, api} = this.props;
  97. const {eventId} = this.state;
  98. if (!eventId.value) {
  99. this.setState(prevState => ({
  100. sourceSuggestions: valueSuggestions,
  101. eventId: {
  102. ...prevState.eventId,
  103. status: EventIdStatus.UNDEFINED,
  104. },
  105. }));
  106. return;
  107. }
  108. this.setState(prevState => ({
  109. sourceSuggestions: valueSuggestions,
  110. eventId: {
  111. ...prevState.eventId,
  112. status: EventIdStatus.LOADING,
  113. },
  114. }));
  115. try {
  116. const query: {eventId: string; projectId?: string} = {eventId: eventId.value};
  117. if (projectId) {
  118. query.projectId = projectId;
  119. }
  120. const rawSuggestions = await api.requestPromise(
  121. `/organizations/${orgSlug}/data-scrubbing-selector-suggestions/`,
  122. {query}
  123. );
  124. const sourceSuggestions: SourceSuggestions = rawSuggestions.suggestions;
  125. if (sourceSuggestions && sourceSuggestions.length > 0) {
  126. this.setState(prevState => ({
  127. sourceSuggestions,
  128. eventId: {
  129. ...prevState.eventId,
  130. status: EventIdStatus.LOADED,
  131. },
  132. }));
  133. return;
  134. }
  135. this.setState(prevState => ({
  136. sourceSuggestions: valueSuggestions,
  137. eventId: {
  138. ...prevState.eventId,
  139. status: EventIdStatus.NOT_FOUND,
  140. },
  141. }));
  142. } catch {
  143. this.setState(prevState => ({
  144. eventId: {
  145. ...prevState.eventId,
  146. status: EventIdStatus.ERROR,
  147. },
  148. }));
  149. }
  150. }
  151. convertRequestError(error: ReturnType<typeof handleError>) {
  152. switch (error.type) {
  153. case ErrorType.INVALID_SELECTOR:
  154. this.setState(prevState => ({
  155. errors: {
  156. ...prevState.errors,
  157. source: error.message,
  158. },
  159. }));
  160. break;
  161. case ErrorType.REGEX_PARSE:
  162. this.setState(prevState => ({
  163. errors: {
  164. ...prevState.errors,
  165. pattern: error.message,
  166. },
  167. }));
  168. break;
  169. default:
  170. addErrorMessage(error.message);
  171. }
  172. }
  173. handleChange = <R extends Rule, K extends KeysOfUnion<R>>(field: K, value: R[K]) => {
  174. const values = {
  175. ...this.state.values,
  176. [field]: value,
  177. };
  178. if (values.type !== RuleType.PATTERN && values.pattern) {
  179. values.pattern = '';
  180. }
  181. if (values.method !== MethodType.REPLACE && values.placeholder) {
  182. values.placeholder = '';
  183. }
  184. this.setState(prevState => ({
  185. values,
  186. requiredValues: this.getRequiredValues(values),
  187. errors: omit(prevState.errors, field),
  188. }));
  189. };
  190. handleSave = async () => {
  191. const {endpoint, api, onSubmitSuccess, closeModal, onGetNewRules} = this.props;
  192. const newRules = onGetNewRules(this.state.values);
  193. try {
  194. const data = await submitRules(api, endpoint, newRules);
  195. onSubmitSuccess(data);
  196. closeModal();
  197. } catch (error) {
  198. this.convertRequestError(handleError(error));
  199. }
  200. };
  201. handleValidateForm() {
  202. const {values, requiredValues} = this.state;
  203. const isFormValid = requiredValues.every(requiredValue => !!values[requiredValue]);
  204. this.setState({isFormValid});
  205. }
  206. handleValidate =
  207. <K extends keyof Values>(field: K) =>
  208. () => {
  209. const isFieldValueEmpty = !this.state.values[field].trim();
  210. const fieldErrorAlreadyExist = this.state.errors[field];
  211. if (isFieldValueEmpty && fieldErrorAlreadyExist) {
  212. return;
  213. }
  214. if (isFieldValueEmpty && !fieldErrorAlreadyExist) {
  215. this.setState(prevState => ({
  216. errors: {
  217. ...prevState.errors,
  218. [field]: t('Field Required'),
  219. },
  220. }));
  221. return;
  222. }
  223. if (!isFieldValueEmpty && fieldErrorAlreadyExist) {
  224. this.clearError(field);
  225. }
  226. };
  227. handleUpdateEventId = (eventId: string) => {
  228. if (eventId === this.state.eventId.value) {
  229. return;
  230. }
  231. this.setState({
  232. eventId: {value: eventId, status: EventIdStatus.UNDEFINED},
  233. });
  234. };
  235. render() {
  236. const {values, errors, isFormValid, eventId, sourceSuggestions} = this.state;
  237. const {title} = this.props;
  238. return (
  239. <Modal
  240. {...this.props}
  241. title={title}
  242. onSave={this.handleSave}
  243. disabled={!isFormValid}
  244. content={
  245. <Form
  246. onChange={this.handleChange}
  247. onValidate={this.handleValidate}
  248. onUpdateEventId={this.handleUpdateEventId}
  249. eventId={eventId}
  250. errors={errors}
  251. values={values}
  252. sourceSuggestions={sourceSuggestions}
  253. />
  254. }
  255. />
  256. );
  257. }
  258. }
  259. export default ModalManager;