projectGeneralSettings.tsx 12 KB

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