organizationSettingsForm.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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:
  50. !organization.features.includes('codecov-integration') ||
  51. !access.has('org:write'),
  52. label: (
  53. <PoweredByCodecov>
  54. {t('Enable Code Coverage Insights')}{' '}
  55. <Feature
  56. hookName="feature-disabled:codecov-integration-setting"
  57. renderDisabled={p => (
  58. <Hovercard
  59. body={
  60. <FeatureDisabled
  61. features={p.features}
  62. hideHelpToggle
  63. featureName={t('Codecov Coverage')}
  64. />
  65. }
  66. >
  67. <Tag role="status" icon={<IconLock isSolid />}>
  68. {t('disabled')}
  69. </Tag>
  70. </Hovercard>
  71. )}
  72. features="organizations:codecov-integration"
  73. >
  74. {() => null}
  75. </Feature>
  76. </PoweredByCodecov>
  77. ),
  78. formatMessageValue: (value: boolean) => {
  79. const onOff = value ? t('on') : t('off');
  80. return t('Codecov access was turned %s', onOff);
  81. },
  82. help: (
  83. <PoweredByCodecov>
  84. {t('powered by')} <IconCodecov /> Codecov{' '}
  85. <HookCodecovSettingsLink organization={organization} />
  86. </PoweredByCodecov>
  87. ),
  88. },
  89. ];
  90. return formsConfig;
  91. }, [access, organization]);
  92. return (
  93. <Form
  94. data-test-id="organization-settings"
  95. apiMethod="PUT"
  96. apiEndpoint={endpoint}
  97. saveOnBlur
  98. allowUndo
  99. initialData={initialData}
  100. onSubmitSuccess={(updated, _model) => {
  101. // Special case for slug, need to forward to new slug
  102. if (typeof onSave === 'function') {
  103. onSave(initialData, updated);
  104. }
  105. }}
  106. onSubmitError={() => addErrorMessage('Unable to save change')}
  107. >
  108. <JsonForm {...jsonFormSettings} forms={forms} />
  109. <AvatarChooser
  110. type="organization"
  111. allowGravatar={false}
  112. endpoint={`${endpoint}avatar/`}
  113. model={initialData}
  114. uploadDomain={initialData.links.regionUrl}
  115. onSave={updateOrganization}
  116. disabled={!access.has('org:write')}
  117. />
  118. </Form>
  119. );
  120. }
  121. export default OrganizationSettingsForm;
  122. const PoweredByCodecov = styled('div')`
  123. display: flex;
  124. align-items: center;
  125. gap: ${space(0.5)};
  126. & > span {
  127. display: flex;
  128. align-items: center;
  129. }
  130. `;