projectGeneralSettings.tsx 12 KB

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