organizationSettingsForm.tsx 5.7 KB

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