settingsForm.tsx 2.5 KB

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