ownerInput.tsx 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. import React from 'react';
  2. import TextareaAutosize from 'react-autosize-textarea';
  3. import styled from '@emotion/styled';
  4. import {addErrorMessage, addSuccessMessage} from 'app/actionCreators/indicator';
  5. import {Client} from 'app/api';
  6. import Button from 'app/components/button';
  7. import {t} from 'app/locale';
  8. import MemberListStore from 'app/stores/memberListStore';
  9. import ProjectsStore from 'app/stores/projectsStore';
  10. import {inputStyles} from 'app/styles/input';
  11. import {Organization, Project, Team} from 'app/types';
  12. import {defined} from 'app/utils';
  13. import RuleBuilder from './ruleBuilder';
  14. const defaultProps = {
  15. urls: [] as string[],
  16. paths: [] as string[],
  17. disabled: false,
  18. };
  19. type Props = {
  20. organization: Organization;
  21. project: Project;
  22. initialText: string;
  23. onSave?: (text: string | null) => void;
  24. } & typeof defaultProps;
  25. type State = {
  26. hasChanges: boolean;
  27. text: string | null;
  28. error: null | {
  29. raw: string[];
  30. };
  31. };
  32. class OwnerInput extends React.Component<Props, State> {
  33. static defaultProps = defaultProps;
  34. state: State = {
  35. hasChanges: false,
  36. text: null,
  37. error: null,
  38. };
  39. parseError(error: State['error']) {
  40. const text = error?.raw?.[0];
  41. if (!text) {
  42. return null;
  43. }
  44. if (text.startsWith('Invalid rule owners:')) {
  45. return <InvalidOwners>{text}</InvalidOwners>;
  46. } else {
  47. return (
  48. <SyntaxOverlay line={parseInt(text.match(/line (\d*),/)?.[1] ?? '', 10) - 1} />
  49. );
  50. }
  51. }
  52. handleUpdateOwnership = () => {
  53. const {organization, project, onSave} = this.props;
  54. const {text} = this.state;
  55. this.setState({error: null});
  56. const api = new Client();
  57. const request = api.requestPromise(
  58. `/projects/${organization.slug}/${project.slug}/ownership/`,
  59. {
  60. method: 'PUT',
  61. data: {raw: text || ''},
  62. }
  63. );
  64. request
  65. .then(() => {
  66. addSuccessMessage(t('Updated issue ownership rules'));
  67. this.setState(
  68. {
  69. hasChanges: false,
  70. text,
  71. },
  72. () => onSave && onSave(text)
  73. );
  74. })
  75. .catch(error => {
  76. this.setState({error: error.responseJSON});
  77. if (error.status === 403) {
  78. addErrorMessage(
  79. t(
  80. "You don't have permission to modify issue ownership rules for this project"
  81. )
  82. );
  83. } else if (
  84. error.status === 400 &&
  85. error.responseJSON.raw &&
  86. error.responseJSON.raw[0].startsWith('Invalid rule owners:')
  87. ) {
  88. addErrorMessage(
  89. t('Unable to save issue ownership rule changes: ' + error.responseJSON.raw[0])
  90. );
  91. } else {
  92. addErrorMessage(t('Unable to save issue ownership rule changes'));
  93. }
  94. });
  95. return request;
  96. };
  97. mentionableUsers() {
  98. return MemberListStore.getAll().map(member => ({
  99. id: member.id,
  100. display: member.email,
  101. email: member.email,
  102. }));
  103. }
  104. mentionableTeams() {
  105. const {project} = this.props;
  106. const projectWithTeams = ProjectsStore.getBySlug(project.slug);
  107. if (!projectWithTeams) {
  108. return [];
  109. }
  110. return projectWithTeams.teams.map((team: Team) => ({
  111. id: team.id,
  112. display: `#${team.slug}`,
  113. email: team.id,
  114. }));
  115. }
  116. handleChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
  117. this.setState({
  118. hasChanges: true,
  119. text: e.target.value,
  120. });
  121. };
  122. handleAddRule = (rule: string) => {
  123. const {initialText} = this.props;
  124. this.setState(
  125. ({text}) => ({
  126. text: (text || initialText) + '\n' + rule,
  127. }),
  128. this.handleUpdateOwnership
  129. );
  130. };
  131. render() {
  132. const {project, organization, disabled, urls, paths, initialText} = this.props;
  133. const {hasChanges, text, error} = this.state;
  134. return (
  135. <React.Fragment>
  136. <RuleBuilder
  137. urls={urls}
  138. paths={paths}
  139. organization={organization}
  140. project={project}
  141. onAddRule={this.handleAddRule.bind(this)}
  142. disabled={disabled}
  143. />
  144. <div
  145. style={{position: 'relative'}}
  146. onKeyDown={e => {
  147. if (e.metaKey && e.key === 'Enter') {
  148. this.handleUpdateOwnership();
  149. }
  150. }}
  151. >
  152. <StyledTextArea
  153. placeholder={
  154. '#example usage\n' +
  155. 'path:src/example/pipeline/* person@sentry.io #infra\n' +
  156. 'url:http://example.com/settings/* #product\n' +
  157. 'tags.sku_class:enterprise #enterprise'
  158. }
  159. onChange={this.handleChange}
  160. disabled={disabled}
  161. value={defined(text) ? text : initialText}
  162. spellCheck="false"
  163. autoComplete="off"
  164. autoCorrect="off"
  165. autoCapitalize="off"
  166. />
  167. <ActionBar>
  168. <div>{this.parseError(error)}</div>
  169. <SaveButton>
  170. <Button
  171. size="small"
  172. priority="primary"
  173. onClick={this.handleUpdateOwnership}
  174. disabled={disabled || !hasChanges}
  175. >
  176. {t('Save Changes')}
  177. </Button>
  178. </SaveButton>
  179. </ActionBar>
  180. </div>
  181. </React.Fragment>
  182. );
  183. }
  184. }
  185. const TEXTAREA_PADDING = 4;
  186. const TEXTAREA_LINE_HEIGHT = 24;
  187. const ActionBar = styled('div')`
  188. display: flex;
  189. align-items: center;
  190. justify-content: space-between;
  191. `;
  192. const SyntaxOverlay = styled('div')<{line: number}>`
  193. ${inputStyles};
  194. width: 100%;
  195. height: ${TEXTAREA_LINE_HEIGHT}px;
  196. background-color: red;
  197. opacity: 0.1;
  198. pointer-events: none;
  199. position: absolute;
  200. top: ${({line}) => TEXTAREA_PADDING + line * 24}px;
  201. `;
  202. const SaveButton = styled('div')`
  203. text-align: end;
  204. padding-top: 10px;
  205. `;
  206. const StyledTextArea = styled(TextareaAutosize)`
  207. ${p => inputStyles(p)};
  208. min-height: 140px;
  209. overflow: auto;
  210. outline: 0;
  211. width: 100%;
  212. resize: none;
  213. margin: 0;
  214. font-family: ${p => p.theme.text.familyMono};
  215. word-break: break-all;
  216. white-space: pre-wrap;
  217. padding-top: ${TEXTAREA_PADDING}px;
  218. line-height: ${TEXTAREA_LINE_HEIGHT}px;
  219. `;
  220. const InvalidOwners = styled('div')`
  221. color: ${p => p.theme.error};
  222. font-weight: bold;
  223. margin-top: 12px;
  224. `;
  225. export default OwnerInput;