ruleBuilder.tsx 6.8 KB

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