index.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. import {Component} from 'react';
  2. import {browserHistory, RouteComponentProps} from 'react-router';
  3. import {addErrorMessage, addSuccessMessage} from 'sentry/actionCreators/indicator';
  4. import {
  5. changeProjectSlug,
  6. removeProject,
  7. transferProject,
  8. } from 'sentry/actionCreators/projects';
  9. import {hasEveryAccess} from 'sentry/components/acl/access';
  10. import {Button} from 'sentry/components/button';
  11. import Confirm from 'sentry/components/confirm';
  12. import FieldGroup from 'sentry/components/forms/fieldGroup';
  13. import TextField from 'sentry/components/forms/fields/textField';
  14. import Form, {FormProps} from 'sentry/components/forms/form';
  15. import JsonForm from 'sentry/components/forms/jsonForm';
  16. import {FieldValue} from 'sentry/components/forms/model';
  17. import Hook from 'sentry/components/hook';
  18. import ExternalLink from 'sentry/components/links/externalLink';
  19. import {removePageFiltersStorage} from 'sentry/components/organizations/pageFilters/persistence';
  20. import {Panel, PanelAlert, PanelHeader} from 'sentry/components/panels';
  21. import {fields} from 'sentry/data/forms/projectGeneralSettings';
  22. import {t, tct} from 'sentry/locale';
  23. import ProjectsStore from 'sentry/stores/projectsStore';
  24. import {Organization, Project} from 'sentry/types';
  25. import {handleXhrErrorResponse} from 'sentry/utils/handleXhrErrorResponse';
  26. import recreateRoute from 'sentry/utils/recreateRoute';
  27. import RequestError from 'sentry/utils/requestError/requestError';
  28. import routeTitleGen from 'sentry/utils/routeTitle';
  29. import withOrganization from 'sentry/utils/withOrganization';
  30. import AsyncView from 'sentry/views/asyncView';
  31. import SettingsPageHeader from 'sentry/views/settings/components/settingsPageHeader';
  32. import TextBlock from 'sentry/views/settings/components/text/textBlock';
  33. import PermissionAlert from 'sentry/views/settings/project/permissionAlert';
  34. type Props = AsyncView['props'] &
  35. RouteComponentProps<{projectId: string}, {}> & {
  36. onChangeSlug: (slug: string) => void;
  37. organization: Organization;
  38. };
  39. type State = AsyncView['state'] & {
  40. data: Project;
  41. };
  42. class ProjectGeneralSettings extends AsyncView<Props, State> {
  43. private _form: Record<string, FieldValue> = {};
  44. getTitle() {
  45. const {projectId} = this.props.params;
  46. return routeTitleGen(t('Project Settings'), projectId, false);
  47. }
  48. getEndpoints(): ReturnType<AsyncView['getEndpoints']> {
  49. const {organization} = this.props;
  50. const {projectId} = this.props.params;
  51. return [['data', `/projects/${organization.slug}/${projectId}/`]];
  52. }
  53. handleTransferFieldChange = (id: string, value: FieldValue) => {
  54. this._form[id] = value;
  55. };
  56. handleRemoveProject = () => {
  57. const {organization} = this.props;
  58. const project = this.state.data;
  59. removePageFiltersStorage(organization.slug);
  60. if (!project) {
  61. return;
  62. }
  63. removeProject({
  64. api: this.api,
  65. orgSlug: organization.slug,
  66. projectSlug: project.slug,
  67. origin: 'settings',
  68. })
  69. .then(
  70. () => {
  71. addSuccessMessage(
  72. tct('[project] was successfully removed', {project: project.slug})
  73. );
  74. },
  75. err => {
  76. addErrorMessage(tct('Error removing [project]', {project: project.slug}));
  77. throw err;
  78. }
  79. )
  80. .then(
  81. () => {
  82. // Need to hard reload because lots of components do not listen to Projects Store
  83. window.location.assign('/');
  84. },
  85. (err: RequestError) => handleXhrErrorResponse('Unable to remove project', err)
  86. );
  87. };
  88. handleTransferProject = async () => {
  89. const {organization} = this.props;
  90. const project = this.state.data;
  91. if (!project) {
  92. return;
  93. }
  94. if (typeof this._form.email !== 'string' || this._form.email.length < 1) {
  95. return;
  96. }
  97. try {
  98. await transferProject(this.api, organization.slug, project, this._form.email);
  99. // Need to hard reload because lots of components do not listen to Projects Store
  100. window.location.assign('/');
  101. } catch (err) {
  102. if (err.status >= 500) {
  103. handleXhrErrorResponse('Unable to transfer project', err);
  104. }
  105. }
  106. };
  107. isProjectAdmin = () =>
  108. hasEveryAccess(['project:admin'], {
  109. organization: this.props.organization,
  110. project: this.state.data,
  111. });
  112. renderRemoveProject() {
  113. const project = this.state.data;
  114. const isProjectAdmin = this.isProjectAdmin();
  115. const {isInternal} = project;
  116. return (
  117. <FieldGroup
  118. label={t('Remove Project')}
  119. help={tct(
  120. 'Remove the [project] project and all related data. [linebreak] Careful, this action cannot be undone.',
  121. {
  122. project: <strong>{project.slug}</strong>,
  123. linebreak: <br />,
  124. }
  125. )}
  126. >
  127. {!isProjectAdmin &&
  128. t('You do not have the required permission to remove this project.')}
  129. {isInternal &&
  130. t(
  131. 'This project cannot be removed. It is used internally by the Sentry server.'
  132. )}
  133. {isProjectAdmin && !isInternal && (
  134. <Confirm
  135. onConfirm={this.handleRemoveProject}
  136. priority="danger"
  137. confirmText={t('Remove Project')}
  138. message={
  139. <div>
  140. <TextBlock>
  141. <strong>
  142. {t('Removing this project is permanent and cannot be undone!')}
  143. </strong>
  144. </TextBlock>
  145. <TextBlock>
  146. {t('This will also remove all associated event data.')}
  147. </TextBlock>
  148. </div>
  149. }
  150. >
  151. <div>
  152. <Button priority="danger">{t('Remove Project')}</Button>
  153. </div>
  154. </Confirm>
  155. )}
  156. </FieldGroup>
  157. );
  158. }
  159. renderTransferProject() {
  160. const project = this.state.data;
  161. const isProjectAdmin = this.isProjectAdmin();
  162. const {isInternal} = project;
  163. return (
  164. <FieldGroup
  165. label={t('Transfer Project')}
  166. help={tct(
  167. 'Transfer the [project] project and all related data. [linebreak] Careful, this action cannot be undone.',
  168. {
  169. project: <strong>{project.slug}</strong>,
  170. linebreak: <br />,
  171. }
  172. )}
  173. >
  174. {!isProjectAdmin &&
  175. t('You do not have the required permission to transfer this project.')}
  176. {isInternal &&
  177. t(
  178. 'This project cannot be transferred. It is used internally by the Sentry server.'
  179. )}
  180. {isProjectAdmin && !isInternal && (
  181. <Confirm
  182. onConfirm={this.handleTransferProject}
  183. priority="danger"
  184. confirmText={t('Transfer project')}
  185. renderMessage={({confirm}) => (
  186. <div>
  187. <TextBlock>
  188. <strong>
  189. {t('Transferring this project is permanent and cannot be undone!')}
  190. </strong>
  191. </TextBlock>
  192. <TextBlock>
  193. {t(
  194. 'Please enter the email of an organization owner to whom you would like to transfer this project.'
  195. )}
  196. </TextBlock>
  197. <Panel>
  198. <Form
  199. hideFooter
  200. onFieldChange={this.handleTransferFieldChange}
  201. onSubmit={(_data, _onSuccess, _onError, e) => {
  202. e.stopPropagation();
  203. confirm();
  204. }}
  205. >
  206. <TextField
  207. name="email"
  208. label={t('Organization Owner')}
  209. placeholder="admin@example.com"
  210. required
  211. help={t(
  212. 'A request will be emailed to this address, asking the organization owner to accept the project transfer.'
  213. )}
  214. />
  215. </Form>
  216. </Panel>
  217. </div>
  218. )}
  219. >
  220. <div>
  221. <Button priority="danger">{t('Transfer Project')}</Button>
  222. </div>
  223. </Confirm>
  224. )}
  225. </FieldGroup>
  226. );
  227. }
  228. renderBody() {
  229. const {organization} = this.props;
  230. const project = this.state.data;
  231. const {projectId} = this.props.params;
  232. const endpoint = `/projects/${organization.slug}/${projectId}/`;
  233. const access = new Set(organization.access.concat(project.access));
  234. const jsonFormProps = {
  235. additionalFieldProps: {
  236. organization,
  237. },
  238. features: new Set(organization.features),
  239. access,
  240. disabled: !hasEveryAccess(['project:write'], {organization, project}),
  241. };
  242. const team = project.teams.length ? project.teams?.[0] : undefined;
  243. /*
  244. HACK: The <Form /> component applies its props to its children meaning the hooked component
  245. would need to conform to the form settings applied in a separate repository. This is
  246. not feasible to maintain and may introduce compatability errors if something changes
  247. in either repository. For that reason, the Form component is split in two, since the
  248. fields do not depend on one another, allowing for the Hook to manage it's own state.
  249. */
  250. const formProps: FormProps = {
  251. saveOnBlur: true,
  252. allowUndo: true,
  253. initialData: {
  254. ...project,
  255. team,
  256. },
  257. apiMethod: 'PUT' as const,
  258. apiEndpoint: endpoint,
  259. onSubmitSuccess: resp => {
  260. this.setState({data: resp});
  261. if (projectId !== resp.slug) {
  262. changeProjectSlug(projectId, resp.slug);
  263. // Container will redirect after stores get updated with new slug
  264. this.props.onChangeSlug(resp.slug);
  265. }
  266. // This will update our project context
  267. ProjectsStore.onUpdateSuccess(resp);
  268. },
  269. };
  270. return (
  271. <div>
  272. <SettingsPageHeader title={t('Project Settings')} />
  273. <PermissionAlert project={project} />
  274. <Form {...formProps}>
  275. <JsonForm
  276. {...jsonFormProps}
  277. title={t('Project Details')}
  278. fields={[fields.name, fields.platform]}
  279. />
  280. <JsonForm
  281. {...jsonFormProps}
  282. title={t('Email')}
  283. fields={[fields.subjectPrefix]}
  284. />
  285. </Form>
  286. <Hook
  287. name="spend-visibility:spike-protection-project-settings"
  288. project={project}
  289. />
  290. <Form {...formProps}>
  291. <JsonForm
  292. {...jsonFormProps}
  293. title={t('Event Settings')}
  294. fields={[fields.resolveAge]}
  295. />
  296. <JsonForm
  297. {...jsonFormProps}
  298. title={t('Client Security')}
  299. fields={[
  300. fields.allowedDomains,
  301. fields.scrapeJavaScript,
  302. fields.securityToken,
  303. fields.securityTokenHeader,
  304. fields.verifySSL,
  305. ]}
  306. renderHeader={() => (
  307. <PanelAlert type="info">
  308. <TextBlock noMargin>
  309. {tct(
  310. 'Configure origin URLs which Sentry should accept events from. This is used for communication with clients like [link].',
  311. {
  312. link: (
  313. <ExternalLink href="https://github.com/getsentry/sentry-javascript">
  314. sentry-javascript
  315. </ExternalLink>
  316. ),
  317. }
  318. )}{' '}
  319. {tct(
  320. 'This will restrict requests based on the [Origin] and [Referer] headers.',
  321. {
  322. Origin: <code>Origin</code>,
  323. Referer: <code>Referer</code>,
  324. }
  325. )}
  326. </TextBlock>
  327. </PanelAlert>
  328. )}
  329. />
  330. </Form>
  331. <Panel>
  332. <PanelHeader>{t('Project Administration')}</PanelHeader>
  333. {this.renderRemoveProject()}
  334. {this.renderTransferProject()}
  335. </Panel>
  336. </div>
  337. );
  338. }
  339. }
  340. type ContainerProps = {
  341. organization: Organization;
  342. } & RouteComponentProps<{projectId: string}, {}>;
  343. class ProjectGeneralSettingsContainer extends Component<ContainerProps> {
  344. componentWillUnmount() {
  345. this.unsubscribe();
  346. }
  347. changedSlug: string | undefined = undefined;
  348. unsubscribe = ProjectsStore.listen(() => this.onProjectsUpdate(), undefined);
  349. onProjectsUpdate() {
  350. if (!this.changedSlug) {
  351. return;
  352. }
  353. const project = ProjectsStore.getBySlug(this.changedSlug);
  354. if (!project) {
  355. return;
  356. }
  357. browserHistory.replace(
  358. recreateRoute('', {
  359. ...this.props,
  360. params: {
  361. ...this.props.params,
  362. projectId: this.changedSlug,
  363. },
  364. })
  365. );
  366. }
  367. render() {
  368. return (
  369. <ProjectGeneralSettings
  370. onChangeSlug={(newSlug: string) => (this.changedSlug = newSlug)}
  371. {...this.props}
  372. />
  373. );
  374. }
  375. }
  376. export default withOrganization(ProjectGeneralSettingsContainer);