index.tsx 14 KB

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