settingsForm.tsx 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 {OrganizationAuthProvider, Scope} from 'sentry/types';
  11. import {useApiQuery} from 'sentry/utils/queryClient';
  12. import useOrganization from 'sentry/utils/useOrganization';
  13. interface Props extends RouteComponentProps<{}, {}> {
  14. access: Set<Scope>;
  15. location: Location;
  16. }
  17. type FeatureFlags = Record<string, {description: string; value: boolean}>;
  18. export default function EarlyFeaturesSettingsForm({access, location}: Props) {
  19. const organization = useOrganization();
  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. }