organizationSettingsForm.tsx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. ...(organization.features.includes('sentry-rollback-settings')
  114. ? [
  115. {
  116. name: 'rollbackEnabled',
  117. type: 'boolean',
  118. label: t('2024 Sentry Rollback'),
  119. help: t('Allow organization members to view their year in review'),
  120. } as FieldObject,
  121. ]
  122. : []),
  123. ];
  124. return formsConfig;
  125. }, [access, organization]);
  126. return (
  127. <Form
  128. data-test-id="organization-settings"
  129. apiMethod="PUT"
  130. apiEndpoint={endpoint}
  131. saveOnBlur
  132. allowUndo
  133. initialData={initialData}
  134. onSubmitSuccess={(updated, _model) => {
  135. // Special case for slug, need to forward to new slug
  136. if (typeof onSave === 'function') {
  137. onSave(initialData, updated);
  138. }
  139. }}
  140. onSubmitError={() => addErrorMessage('Unable to save change')}
  141. >
  142. <JsonForm {...jsonFormSettings} forms={generalForms} />
  143. <HookOrganizationMembershipSettings
  144. jsonFormSettings={jsonFormSettings}
  145. forms={organizationMembershipSettingsFields}
  146. />
  147. <AvatarChooser
  148. type="organization"
  149. allowGravatar={false}
  150. endpoint={`${endpoint}avatar/`}
  151. model={initialData}
  152. uploadDomain={initialData.links.regionUrl}
  153. onSave={updateOrganization}
  154. disabled={!access.has('org:write')}
  155. />
  156. </Form>
  157. );
  158. }
  159. export default OrganizationSettingsForm;
  160. const PoweredByCodecov = styled('div')`
  161. display: flex;
  162. align-items: center;
  163. gap: ${space(0.5)};
  164. & > span {
  165. display: flex;
  166. align-items: center;
  167. }
  168. `;