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