settingsForm.tsx 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import {Fragment} from 'react';
  2. import type {RouteComponentProps} from 'react-router';
  3. import type {Location} from 'history';
  4. import {addErrorMessage} from 'sentry/actionCreators/indicator';
  5. import Form from 'sentry/components/forms/form';
  6. import JsonForm from 'sentry/components/forms/jsonForm';
  7. import type {JsonFormObject} from 'sentry/components/forms/types';
  8. import LoadingIndicator from 'sentry/components/loadingIndicator';
  9. import {t} from 'sentry/locale';
  10. import type {Organization, OrganizationAuthProvider, Scope} from 'sentry/types';
  11. import {useApiQuery} from 'sentry/utils/queryClient';
  12. import withOrganization from 'sentry/utils/withOrganization';
  13. interface Props extends RouteComponentProps<{}, {}> {
  14. access: Set<Scope>;
  15. location: Location;
  16. organization: Organization;
  17. }
  18. type FeatureFlags = Record<string, {description: string; value: boolean}>;
  19. function EarlyFeaturesSettingsForm({organization, access, location}: Props) {
  20. const {data: authProvider, isLoading: authProviderIsLoading} =
  21. useApiQuery<OrganizationAuthProvider>(
  22. [`/organizations/${organization.slug}/auth-provider/`],
  23. {staleTime: 0}
  24. );
  25. const {data: featureFlags, isLoading: featureFlagsIsLoading} =
  26. useApiQuery<FeatureFlags>(['/internal/feature-flags/'], {staleTime: 0});
  27. if (authProviderIsLoading || featureFlagsIsLoading) {
  28. return <LoadingIndicator />;
  29. }
  30. const initialData: Record<string, boolean> = {};
  31. for (const flag in featureFlags) {
  32. if (featureFlags.hasOwnProperty(flag)) {
  33. const obj = featureFlags[flag];
  34. initialData[flag] = obj.value;
  35. }
  36. }
  37. const jsonFormSettings = {
  38. additionalFieldProps: {hasSsoEnabled: !!authProvider},
  39. features: new Set(organization.features),
  40. access,
  41. location,
  42. disabled: !access.has('org:write'),
  43. };
  44. const featuresForm: JsonFormObject = {
  45. title: t('Early Adopter Features'),
  46. fields: Object.entries(featureFlags || {}).map(([flag, obj]) => ({
  47. label: obj.description,
  48. name: flag,
  49. type: 'boolean',
  50. })),
  51. };
  52. return (
  53. <Fragment>
  54. <Form
  55. data-test-id="organization-settings"
  56. apiMethod="PUT"
  57. apiEndpoint={`/internal/feature-flags/`}
  58. saveOnBlur
  59. allowUndo
  60. initialData={initialData}
  61. onSubmitError={() => addErrorMessage('Unable to save change')}
  62. onSubmitSuccess={() => {}}
  63. >
  64. <JsonForm {...jsonFormSettings} forms={[featuresForm]} />
  65. </Form>
  66. </Fragment>
  67. );
  68. }
  69. export default withOrganization(EarlyFeaturesSettingsForm);