SettingsSensitiveWords.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import React, { useEffect, useState, useRef } from 'react';
  2. import { Button, Col, Form, Row, Spin, Tag } from '@douyinfe/semi-ui';
  3. import {
  4. compareObjects,
  5. API,
  6. showError,
  7. showSuccess,
  8. showWarning,
  9. } from '../../../helpers';
  10. export default function SettingsSensitiveWords(props) {
  11. const [loading, setLoading] = useState(false);
  12. const [inputs, setInputs] = useState({
  13. CheckSensitiveEnabled: false,
  14. CheckSensitiveOnPromptEnabled: false,
  15. SensitiveWords: '',
  16. });
  17. const refForm = useRef();
  18. const [inputsRow, setInputsRow] = useState(inputs);
  19. function onSubmit() {
  20. const updateArray = compareObjects(inputs, inputsRow);
  21. if (!updateArray.length) return showWarning('你似乎并没有修改什么');
  22. const requestQueue = updateArray.map((item) => {
  23. let value = '';
  24. if (typeof inputs[item.key] === 'boolean') {
  25. value = String(inputs[item.key]);
  26. } else {
  27. value = inputs[item.key];
  28. }
  29. return API.put('/api/option/', {
  30. key: item.key,
  31. value,
  32. });
  33. });
  34. setLoading(true);
  35. Promise.all(requestQueue)
  36. .then((res) => {
  37. if (requestQueue.length === 1) {
  38. if (res.includes(undefined)) return;
  39. } else if (requestQueue.length > 1) {
  40. if (res.includes(undefined)) return showError('部分更新失败');
  41. }
  42. showSuccess('更新成功');
  43. })
  44. .catch(() => {
  45. showError('更新失败');
  46. })
  47. .finally(() => {
  48. setLoading(false);
  49. setInputsRow(structuredClone(inputs));
  50. });
  51. }
  52. useEffect(() => {
  53. const currentInputs = {};
  54. for (let key in props.options) {
  55. if (Object.keys(inputs).includes(key)) {
  56. currentInputs[key] = props.options[key];
  57. }
  58. }
  59. setInputs(currentInputs);
  60. setInputsRow(structuredClone(currentInputs));
  61. refForm.current.setValues(currentInputs);
  62. }, [props.options]);
  63. return (
  64. <>
  65. <Spin spinning={loading}>
  66. <Form
  67. values={inputs}
  68. getFormApi={(formAPI) => (refForm.current = formAPI)}
  69. style={{ marginBottom: 15 }}
  70. >
  71. <Form.Section text={'屏蔽词过滤设置'}>
  72. <Row gutter={16}>
  73. <Col span={8}>
  74. <Form.Switch
  75. field={'CheckSensitiveEnabled'}
  76. label={'启用屏蔽词过滤功能'}
  77. size='large'
  78. checkedText='|'
  79. uncheckedText='〇'
  80. defaultChecked={false}
  81. checked={false}
  82. onChange={(value) => {
  83. setInputs({
  84. ...inputs,
  85. CheckSensitiveEnabled: value,
  86. });
  87. }}
  88. />
  89. </Col>
  90. <Col span={8}>
  91. <Form.Switch
  92. field={'CheckSensitiveOnPromptEnabled'}
  93. label={'启用 Prompt 检查'}
  94. size='large'
  95. checkedText='|'
  96. uncheckedText='〇'
  97. defaultChecked={false}
  98. checked={false}
  99. onChange={(value) =>
  100. setInputs({
  101. ...inputs,
  102. CheckSensitiveOnPromptEnabled: value,
  103. })
  104. }
  105. />
  106. </Col>
  107. </Row>
  108. <Row>
  109. <Col span={16}>
  110. <Form.TextArea
  111. label={'屏蔽词列表'}
  112. extraText={'一行一个屏蔽词,不需要符号分割'}
  113. placeholder={'一行一个屏蔽词,不需要符号分割'}
  114. field={'SensitiveWords'}
  115. onChange={(value) =>
  116. setInputs({
  117. ...inputs,
  118. SensitiveWords: value,
  119. })
  120. }
  121. style={{ fontFamily: 'JetBrains Mono, Consolas' }}
  122. autosize={{ minRows: 6, maxRows: 12 }}
  123. />
  124. </Col>
  125. </Row>
  126. <Row>
  127. <Button size='large' onClick={onSubmit}>
  128. 保存屏蔽词过滤设置
  129. </Button>
  130. </Row>
  131. </Form.Section>
  132. </Form>
  133. </Spin>
  134. </>
  135. );
  136. }