SettingsDrawing.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 SettingsDrawing(props) {
  11. const [loading, setLoading] = useState(false);
  12. const [inputs, setInputs] = useState({
  13. DrawingEnabled: false,
  14. MjNotifyEnabled: false,
  15. MjAccountFilterEnabled: false,
  16. MjForwardUrlEnabled: false,
  17. MjModeClearEnabled: false,
  18. });
  19. const refForm = useRef();
  20. const [inputsRow, setInputsRow] = useState(inputs);
  21. function onSubmit() {
  22. const updateArray = compareObjects(inputs, inputsRow);
  23. if (!updateArray.length) return showWarning('你似乎并没有修改什么');
  24. const requestQueue = updateArray.map((item) => {
  25. let value = '';
  26. if (typeof inputs[item.key] === 'boolean') {
  27. value = String(inputs[item.key]);
  28. } else {
  29. value = inputs[item.key];
  30. }
  31. return API.put('/api/option/', {
  32. key: item.key,
  33. value,
  34. });
  35. });
  36. setLoading(true);
  37. Promise.all(requestQueue)
  38. .then((res) => {
  39. if (requestQueue.length === 1) {
  40. if (res.includes(undefined)) return;
  41. } else if (requestQueue.length > 1) {
  42. if (res.includes(undefined)) return showError('部分保存失败,请重试');
  43. }
  44. showSuccess('保存成功');
  45. props.refresh();
  46. })
  47. .catch(() => {
  48. showError('保存失败,请重试');
  49. })
  50. .finally(() => {
  51. setLoading(false);
  52. });
  53. }
  54. useEffect(() => {
  55. const currentInputs = {};
  56. for (let key in props.options) {
  57. if (Object.keys(inputs).includes(key)) {
  58. currentInputs[key] = props.options[key];
  59. }
  60. }
  61. setInputs(currentInputs);
  62. setInputsRow(structuredClone(currentInputs));
  63. refForm.current.setValues(currentInputs);
  64. localStorage.setItem('mj_notify_enabled', String(inputs.MjNotifyEnabled));
  65. }, [props.options]);
  66. return (
  67. <>
  68. <Spin spinning={loading}>
  69. <Form
  70. values={inputs}
  71. getFormApi={(formAPI) => (refForm.current = formAPI)}
  72. style={{ marginBottom: 15 }}
  73. >
  74. <Form.Section text={'绘图设置'}>
  75. <Row gutter={16}>
  76. <Col span={8}>
  77. <Form.Switch
  78. field={'DrawingEnabled'}
  79. label={'启用绘图功能'}
  80. size='large'
  81. checkedText='|'
  82. uncheckedText='〇'
  83. onChange={(value) => {
  84. setInputs({
  85. ...inputs,
  86. DrawingEnabled: value,
  87. });
  88. }}
  89. />
  90. </Col>
  91. <Col span={8}>
  92. <Form.Switch
  93. field={'MjNotifyEnabled'}
  94. label={'允许回调(会泄露服务器 IP 地址)'}
  95. size='large'
  96. checkedText='|'
  97. uncheckedText='〇'
  98. onChange={(value) =>
  99. setInputs({
  100. ...inputs,
  101. MjNotifyEnabled: value,
  102. })
  103. }
  104. />
  105. </Col>
  106. <Col span={8}>
  107. <Form.Switch
  108. field={'MjAccountFilterEnabled'}
  109. label={'允许 AccountFilter 参数'}
  110. size='large'
  111. checkedText='|'
  112. uncheckedText='〇'
  113. onChange={(value) =>
  114. setInputs({
  115. ...inputs,
  116. MjAccountFilterEnabled: value,
  117. })
  118. }
  119. />
  120. </Col>
  121. <Col span={8}>
  122. <Form.Switch
  123. field={'MjForwardUrlEnabled'}
  124. label={'开启之后将上游地址替换为服务器地址'}
  125. size='large'
  126. checkedText='|'
  127. uncheckedText='〇'
  128. onChange={(value) =>
  129. setInputs({
  130. ...inputs,
  131. MjForwardUrlEnabled: value,
  132. })
  133. }
  134. />
  135. </Col>
  136. <Col span={8}>
  137. <Form.Switch
  138. field={'MjModeClearEnabled'}
  139. label={
  140. <>
  141. 开启之后会清除用户提示词中的 <Tag>--fast</Tag> 、
  142. <Tag>--relax</Tag> 以及 <Tag>--turbo</Tag> 参数
  143. </>
  144. }
  145. size='large'
  146. checkedText='|'
  147. uncheckedText='〇'
  148. onChange={(value) =>
  149. setInputs({
  150. ...inputs,
  151. MjModeClearEnabled: value,
  152. })
  153. }
  154. />
  155. </Col>
  156. </Row>
  157. <Row>
  158. <Button size='large' onClick={onSubmit}>
  159. 保存绘图设置
  160. </Button>
  161. </Row>
  162. </Form.Section>
  163. </Form>
  164. </Spin>
  165. </>
  166. );
  167. }