settingsForm.tsx 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. interface State {
  19. authProvider: OrganizationAuthProvider;
  20. featureFlags: {[key: string]: {description: string; value: boolean}};
  21. }
  22. function EarlyFeaturesSettingsForm({organization, access, location}: Props) {
  23. const {data: authProvider, isLoading: authProviderIsLoading} = useApiQuery<
  24. Pick<State, 'authProvider'>
  25. >([`/organizations/${organization.slug}/auth-provider/`], {
  26. staleTime: 0,
  27. });
  28. const {data: featureFlags, isLoading: featureFlagsIsLoading} = useApiQuery<
  29. Pick<State, 'featureFlags'>
  30. >(['/internal/feature-flags/'], {
  31. staleTime: 0,
  32. });
  33. const endpoint = `/internal/feature-flags/`;
  34. if (authProviderIsLoading || featureFlagsIsLoading) {
  35. return <LoadingIndicator />;
  36. }
  37. const initialData = {};
  38. for (const flag in featureFlags) {
  39. if (featureFlags.hasOwnProperty(flag)) {
  40. const obj = featureFlags[flag];
  41. initialData[flag] = obj.value;
  42. }
  43. }
  44. const jsonFormSettings = {
  45. additionalFieldProps: {hasSsoEnabled: !!authProvider},
  46. features: new Set(organization.features),
  47. access,
  48. location,
  49. disabled: !access.has('org:write'),
  50. };
  51. const featuresForm: JsonFormObject = {
  52. title: t('Early Adopter Features'),
  53. fields: Object.entries(featureFlags || {}).map(([flag, obj]) => ({
  54. label: obj.description,
  55. name: flag,
  56. type: 'boolean',
  57. })),
  58. };
  59. return (
  60. <Fragment>
  61. <Form
  62. data-test-id="organization-settings"
  63. apiMethod="PUT"
  64. apiEndpoint={endpoint}
  65. saveOnBlur
  66. allowUndo
  67. initialData={initialData}
  68. onSubmitError={() => addErrorMessage('Unable to save change')}
  69. onSubmitSuccess={() => {}}
  70. >
  71. <JsonForm {...jsonFormSettings} forms={[featuresForm]} />
  72. </Form>
  73. </Fragment>
  74. );
  75. }
  76. export default withOrganization(EarlyFeaturesSettingsForm);