projectGeneralSettings.tsx 11 KB

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