index.tsx 13 KB

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