ruleBuilder.tsx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. import {Component, Fragment} from 'react';
  2. import styled from '@emotion/styled';
  3. import {addErrorMessage} from 'sentry/actionCreators/indicator';
  4. import Button from 'sentry/components/button';
  5. import SelectControl from 'sentry/components/forms/controls/selectControl';
  6. import Input from 'sentry/components/input';
  7. import Tag from 'sentry/components/tag';
  8. import TextOverflow from 'sentry/components/textOverflow';
  9. import {IconAdd, IconChevron} from 'sentry/icons';
  10. import {t} from 'sentry/locale';
  11. import MemberListStore from 'sentry/stores/memberListStore';
  12. import space from 'sentry/styles/space';
  13. import {Organization, Project} from 'sentry/types';
  14. import SelectOwners, {
  15. Owner,
  16. } from 'sentry/views/settings/project/projectOwnership/selectOwners';
  17. const initialState = {
  18. text: '',
  19. tagName: '',
  20. type: 'path',
  21. owners: [],
  22. isValid: false,
  23. };
  24. function getMatchPlaceholder(type: string): string {
  25. switch (type) {
  26. case 'path':
  27. return 'src/example/*';
  28. case 'module':
  29. return 'com.module.name.example';
  30. case 'url':
  31. return 'https://example.com/settings/*';
  32. case 'tag':
  33. return 'tag-value';
  34. default:
  35. return '';
  36. }
  37. }
  38. type Props = {
  39. disabled: boolean;
  40. onAddRule: (rule: string) => void;
  41. organization: Organization;
  42. paths: string[];
  43. project: Project;
  44. urls: string[];
  45. };
  46. type State = {
  47. isValid: boolean;
  48. owners: Owner[];
  49. tagName: string;
  50. text: string;
  51. type: string;
  52. };
  53. class RuleBuilder extends Component<Props, State> {
  54. state: State = initialState;
  55. checkIsValid = () => {
  56. this.setState(state => ({
  57. isValid: !!state.text && state.owners && !!state.owners.length,
  58. }));
  59. };
  60. handleTypeChange = (val: string | number | boolean) => {
  61. this.setState({type: val as string}); // TODO(ts): Add select value type as generic to select controls
  62. this.checkIsValid();
  63. };
  64. handleTagNameChangeValue = (e: React.ChangeEvent<HTMLInputElement>) => {
  65. this.setState({tagName: e.target.value}, this.checkIsValid);
  66. };
  67. handleChangeValue = (e: React.ChangeEvent<HTMLInputElement>) => {
  68. this.setState({text: e.target.value});
  69. this.checkIsValid();
  70. };
  71. handleChangeOwners = (owners: Owner[]) => {
  72. this.setState({owners});
  73. this.checkIsValid();
  74. };
  75. handleAddRule = () => {
  76. const {type, text, tagName, owners, isValid} = this.state;
  77. if (!isValid) {
  78. addErrorMessage('A rule needs a type, a value, and one or more issue owners.');
  79. return;
  80. }
  81. const ownerText = owners
  82. .map(owner =>
  83. owner.actor.type === 'team'
  84. ? `#${owner.actor.name}`
  85. : MemberListStore.getById(owner.actor.id)?.email
  86. )
  87. .join(' ');
  88. const quotedText = text.match(/\s/) ? `"${text}"` : text;
  89. const rule = `${
  90. type === 'tag' ? `tags.${tagName}` : type
  91. }:${quotedText} ${ownerText}`;
  92. this.props.onAddRule(rule);
  93. this.setState(initialState);
  94. };
  95. handleSelectCandidate = (text: string, type: string) => {
  96. this.setState({text, type});
  97. this.checkIsValid();
  98. };
  99. render() {
  100. const {urls, paths, disabled, project, organization} = this.props;
  101. const {type, text, tagName, owners, isValid} = this.state;
  102. const hasCandidates = paths || urls;
  103. return (
  104. <Fragment>
  105. {hasCandidates && (
  106. <Candidates>
  107. {paths.map(v => (
  108. <RuleCandidate
  109. key={v}
  110. role="button"
  111. aria-label={t('Path rule candidate')}
  112. onClick={() => this.handleSelectCandidate(v, 'path')}
  113. >
  114. <IconAdd color="border" isCircled />
  115. <TextOverflow>{v}</TextOverflow>
  116. <Tag>{t('Path')}</Tag>
  117. </RuleCandidate>
  118. ))}
  119. {urls.map(v => (
  120. <RuleCandidate
  121. key={v}
  122. role="button"
  123. aria-label={t('URL rule candidate')}
  124. onClick={() => this.handleSelectCandidate(v, 'url')}
  125. >
  126. <IconAdd color="border" isCircled />
  127. <TextOverflow>{v}</TextOverflow>
  128. <Tag>{t('URL')}</Tag>
  129. </RuleCandidate>
  130. ))}
  131. </Candidates>
  132. )}
  133. <BuilderBar>
  134. <BuilderSelect
  135. aria-label={t('Rule type')}
  136. name="select-type"
  137. value={type}
  138. onChange={this.handleTypeChange}
  139. options={[
  140. {value: 'path', label: t('Path')},
  141. {value: 'module', label: t('Module')},
  142. {value: 'tag', label: t('Tag')},
  143. {value: 'url', label: t('URL')},
  144. ]}
  145. clearable={false}
  146. disabled={disabled}
  147. />
  148. {type === 'tag' && (
  149. <BuilderTagNameInput
  150. value={tagName}
  151. onChange={this.handleTagNameChangeValue}
  152. disabled={disabled}
  153. placeholder="tag-name"
  154. />
  155. )}
  156. <Input
  157. value={text}
  158. onChange={this.handleChangeValue}
  159. disabled={disabled}
  160. placeholder={getMatchPlaceholder(type)}
  161. aria-label={t('Rule pattern')}
  162. />
  163. <IconChevron color="border" direction="right" />
  164. <SelectOwners
  165. organization={organization}
  166. project={project}
  167. value={owners}
  168. onChange={this.handleChangeOwners}
  169. disabled={disabled}
  170. />
  171. <Button
  172. priority="primary"
  173. disabled={!isValid}
  174. onClick={this.handleAddRule}
  175. icon={<IconAdd isCircled />}
  176. aria-label={t('Add rule')}
  177. />
  178. </BuilderBar>
  179. </Fragment>
  180. );
  181. }
  182. }
  183. const Candidates = styled('div')`
  184. margin-bottom: 10px;
  185. `;
  186. const RuleCandidate = styled('div')`
  187. font-family: ${p => p.theme.text.familyMono};
  188. border: 1px solid ${p => p.theme.border};
  189. border-radius: ${p => p.theme.borderRadius};
  190. background-color: ${p => p.theme.background};
  191. padding: ${space(0.25)} ${space(0.5)};
  192. margin-bottom: ${space(0.5)};
  193. cursor: pointer;
  194. overflow: hidden;
  195. display: flex;
  196. gap: ${space(0.5)};
  197. align-items: center;
  198. `;
  199. const BuilderBar = styled('div')`
  200. display: flex;
  201. gap: ${space(1)};
  202. align-items: center;
  203. margin-bottom: ${space(2)};
  204. `;
  205. const BuilderSelect = styled(SelectControl)`
  206. width: 140px;
  207. flex-shrink: 0;
  208. `;
  209. const BuilderTagNameInput = styled(Input)`
  210. width: 200px;
  211. `;
  212. export default RuleBuilder;