adminSettings.tsx 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import isUndefined from 'lodash/isUndefined';
  2. import {Form} from 'sentry/components/forms';
  3. import {Panel, PanelHeader} from 'sentry/components/panels';
  4. import {t} from 'sentry/locale';
  5. import AsyncView from 'sentry/views/asyncView';
  6. import {getOption, getOptionField} from './options';
  7. const optionsAvailable = [
  8. 'system.url-prefix',
  9. 'system.admin-email',
  10. 'system.support-email',
  11. 'system.security-email',
  12. 'system.rate-limit',
  13. 'auth.allow-registration',
  14. 'auth.ip-rate-limit',
  15. 'auth.user-rate-limit',
  16. 'api.rate-limit.org-create',
  17. 'beacon.anonymous',
  18. ];
  19. type Field = ReturnType<typeof getOption>;
  20. type FieldDef = {
  21. field: Field;
  22. value: string | undefined;
  23. };
  24. type State = AsyncView['state'] & {
  25. data: Record<string, FieldDef>;
  26. };
  27. export default class AdminSettings extends AsyncView<{}, State> {
  28. get endpoint() {
  29. return '/internal/options/';
  30. }
  31. getEndpoints(): ReturnType<AsyncView['getEndpoints']> {
  32. return [['data', this.endpoint]];
  33. }
  34. renderBody() {
  35. const {data} = this.state;
  36. const initialData = {};
  37. const fields = {};
  38. for (const key of optionsAvailable) {
  39. // TODO(dcramer): we should not be mutating options
  40. const option = data[key] ?? {field: {}, value: undefined};
  41. if (isUndefined(option.value) || option.value === '') {
  42. const defn = getOption(key);
  43. initialData[key] = defn.defaultValue ? defn.defaultValue() : '';
  44. } else {
  45. initialData[key] = option.value;
  46. }
  47. fields[key] = getOptionField(key, option.field);
  48. }
  49. return (
  50. <div>
  51. <h3>{t('Settings')}</h3>
  52. <Form
  53. apiMethod="PUT"
  54. apiEndpoint={this.endpoint}
  55. initialData={initialData}
  56. saveOnBlur
  57. >
  58. <Panel>
  59. <PanelHeader>General</PanelHeader>
  60. {fields['system.url-prefix']}
  61. {fields['system.admin-email']}
  62. {fields['system.support-email']}
  63. {fields['system.security-email']}
  64. {fields['system.rate-limit']}
  65. </Panel>
  66. <Panel>
  67. <PanelHeader>Security & Abuse</PanelHeader>
  68. {fields['auth.allow-registration']}
  69. {fields['auth.ip-rate-limit']}
  70. {fields['auth.user-rate-limit']}
  71. {fields['api.rate-limit.org-create']}
  72. </Panel>
  73. <Panel>
  74. <PanelHeader>Beacon</PanelHeader>
  75. {fields['beacon.anonymous']}
  76. </Panel>
  77. </Form>
  78. </div>
  79. );
  80. }
  81. }