organizationSettingsForm.tsx 4.3 KB

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