index.tsx 12 KB

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