organizationSettingsForm.tsx 4.9 KB

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