organizationSettingsForm.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import {RouteComponentProps} from 'react-router';
  2. import styled from '@emotion/styled';
  3. import {Location} from 'history';
  4. import cloneDeep from 'lodash/cloneDeep';
  5. import {addErrorMessage} from 'sentry/actionCreators/indicator';
  6. import {updateOrganization} from 'sentry/actionCreators/organizations';
  7. import Feature from 'sentry/components/acl/feature';
  8. import FeatureDisabled from 'sentry/components/acl/featureDisabled';
  9. import AsyncComponent from 'sentry/components/asyncComponent';
  10. import AvatarChooser from 'sentry/components/avatarChooser';
  11. import Form from 'sentry/components/forms/form';
  12. import JsonForm from 'sentry/components/forms/jsonForm';
  13. import {Hovercard} from 'sentry/components/hovercard';
  14. import ExternalLink from 'sentry/components/links/externalLink';
  15. import Tag from 'sentry/components/tag';
  16. import organizationSettingsFields from 'sentry/data/forms/organizationGeneralSettings';
  17. import {IconCodecov, IconLock} from 'sentry/icons';
  18. import {t} from 'sentry/locale';
  19. import {space} from 'sentry/styles/space';
  20. import {Organization, Scope} from 'sentry/types';
  21. import withOrganization from 'sentry/utils/withOrganization';
  22. type Props = {
  23. access: Set<Scope>;
  24. initialData: Organization;
  25. location: Location;
  26. onSave: (previous: Organization, updated: Organization) => void;
  27. organization: Organization;
  28. } & RouteComponentProps<{}, {}>;
  29. type State = AsyncComponent['state'] & {
  30. authProvider: object;
  31. };
  32. class OrganizationSettingsForm extends AsyncComponent<Props, State> {
  33. getEndpoints(): ReturnType<AsyncComponent['getEndpoints']> {
  34. const {organization} = this.props;
  35. return [['authProvider', `/organizations/${organization.slug}/auth-provider/`]];
  36. }
  37. render() {
  38. const {initialData, organization, onSave, access} = this.props;
  39. const {authProvider} = this.state;
  40. const endpoint = `/organizations/${organization.slug}/`;
  41. const jsonFormSettings = {
  42. additionalFieldProps: {hasSsoEnabled: !!authProvider},
  43. features: new Set(organization.features),
  44. access,
  45. location: this.props.location,
  46. disabled: !access.has('org:write'),
  47. };
  48. const forms = cloneDeep(organizationSettingsFields);
  49. if (organization.features.includes('codecov-stacktrace-integration')) {
  50. forms[0].fields = [
  51. ...forms[0].fields,
  52. {
  53. name: 'codecovAccess',
  54. type: 'boolean',
  55. disabled: !organization.features.includes('codecov-integration'),
  56. label: (
  57. <PoweredByCodecov>
  58. {t('Enable Code Coverage Insights')}{' '}
  59. <Feature
  60. hookName="feature-disabled:codecov-integration-setting"
  61. renderDisabled={p => (
  62. <Hovercard
  63. body={
  64. <FeatureDisabled
  65. features={p.features}
  66. hideHelpToggle
  67. featureName={t('Codecov Coverage')}
  68. />
  69. }
  70. >
  71. <Tag role="status" icon={<IconLock isSolid />}>
  72. {t('disabled')}
  73. </Tag>
  74. </Hovercard>
  75. )}
  76. features={['organizations:codecov-integration']}
  77. >
  78. {() => null}
  79. </Feature>
  80. </PoweredByCodecov>
  81. ),
  82. formatMessageValue: (value: boolean) => {
  83. const onOff = value ? t('on') : t('off');
  84. return t('Codecov access was turned %s', onOff);
  85. },
  86. help: (
  87. <PoweredByCodecov>
  88. {t('powered by')} <IconCodecov /> Codecov{' '}
  89. <ExternalLink href="https://about.codecov.io/sign-up-sentry-codecov/">
  90. {t('Learn More')}
  91. </ExternalLink>
  92. </PoweredByCodecov>
  93. ),
  94. },
  95. ];
  96. }
  97. return (
  98. <Form
  99. data-test-id="organization-settings"
  100. apiMethod="PUT"
  101. apiEndpoint={endpoint}
  102. saveOnBlur
  103. allowUndo
  104. initialData={initialData}
  105. onSubmitSuccess={(updated, _model) => {
  106. // Special case for slug, need to forward to new slug
  107. if (typeof onSave === 'function') {
  108. onSave(initialData, updated);
  109. }
  110. }}
  111. onSubmitError={() => addErrorMessage('Unable to save change')}
  112. >
  113. <JsonForm {...jsonFormSettings} forms={forms} />
  114. <AvatarChooser
  115. type="organization"
  116. allowGravatar={false}
  117. endpoint={`${endpoint}avatar/`}
  118. model={initialData}
  119. onSave={updateOrganization}
  120. disabled={!access.has('org:write')}
  121. />
  122. </Form>
  123. );
  124. }
  125. }
  126. export default withOrganization(OrganizationSettingsForm);
  127. const PoweredByCodecov = styled('div')`
  128. display: flex;
  129. align-items: center;
  130. gap: ${space(0.5)};
  131. & > span {
  132. display: flex;
  133. align-items: center;
  134. }
  135. `;