index.tsx 13 KB

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