organizationSettingsForm.tsx 4.8 KB

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