SettingsChats.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import React, { useEffect, useState, useRef } from 'react';
  2. import {
  3. Banner,
  4. Button,
  5. Form,
  6. Space,
  7. Spin,
  8. } from '@douyinfe/semi-ui';
  9. import {
  10. compareObjects,
  11. API,
  12. showError,
  13. showSuccess,
  14. showWarning,
  15. verifyJSON,
  16. } from '../../../helpers';
  17. import { useTranslation } from 'react-i18next';
  18. export default function SettingsChats(props) {
  19. const { t } = useTranslation();
  20. const [loading, setLoading] = useState(false);
  21. const [inputs, setInputs] = useState({
  22. Chats: '[]',
  23. });
  24. const refForm = useRef();
  25. const [inputsRow, setInputsRow] = useState(inputs);
  26. async function onSubmit() {
  27. try {
  28. console.log('Starting validation...');
  29. await refForm.current
  30. .validate()
  31. .then(() => {
  32. console.log('Validation passed');
  33. const updateArray = compareObjects(inputs, inputsRow);
  34. if (!updateArray.length)
  35. return showWarning(t('你似乎并没有修改什么'));
  36. const requestQueue = updateArray.map((item) => {
  37. let value = '';
  38. if (typeof inputs[item.key] === 'boolean') {
  39. value = String(inputs[item.key]);
  40. } else {
  41. value = inputs[item.key];
  42. }
  43. return API.put('/api/option/', {
  44. key: item.key,
  45. value,
  46. });
  47. });
  48. setLoading(true);
  49. Promise.all(requestQueue)
  50. .then((res) => {
  51. if (requestQueue.length === 1) {
  52. if (res.includes(undefined)) return;
  53. } else if (requestQueue.length > 1) {
  54. if (res.includes(undefined))
  55. return showError(t('部分保存失败,请重试'));
  56. }
  57. showSuccess(t('保存成功'));
  58. props.refresh();
  59. })
  60. .catch(() => {
  61. showError(t('保存失败,请重试'));
  62. })
  63. .finally(() => {
  64. setLoading(false);
  65. });
  66. })
  67. .catch((error) => {
  68. console.error('Validation failed:', error);
  69. showError(t('请检查输入'));
  70. });
  71. } catch (error) {
  72. showError(t('请检查输入'));
  73. console.error(error);
  74. }
  75. }
  76. useEffect(() => {
  77. const currentInputs = {};
  78. for (let key in props.options) {
  79. if (Object.keys(inputs).includes(key)) {
  80. if (key === 'Chats') {
  81. const obj = JSON.parse(props.options[key]);
  82. currentInputs[key] = JSON.stringify(obj, null, 2);
  83. } else {
  84. currentInputs[key] = props.options[key];
  85. }
  86. }
  87. }
  88. setInputs(currentInputs);
  89. setInputsRow(structuredClone(currentInputs));
  90. refForm.current.setValues(currentInputs);
  91. }, [props.options]);
  92. return (
  93. <Spin spinning={loading}>
  94. <Form
  95. values={inputs}
  96. getFormApi={(formAPI) => (refForm.current = formAPI)}
  97. style={{ marginBottom: 15 }}
  98. >
  99. <Form.Section text={t('聊天设置')}>
  100. <Banner
  101. type='info'
  102. description={t(
  103. '链接中的{key}将自动替换为sk-xxxx,{address}将自动替换为系统设置的服务器地址,末尾不带/和/v1',
  104. )}
  105. />
  106. <Form.TextArea
  107. label={t('聊天配置')}
  108. extraText={''}
  109. placeholder={t('为一个 JSON 文本')}
  110. field={'Chats'}
  111. autosize={{ minRows: 6, maxRows: 12 }}
  112. trigger='blur'
  113. stopValidateWithError
  114. rules={[
  115. {
  116. validator: (rule, value) => {
  117. return verifyJSON(value);
  118. },
  119. message: t('不是合法的 JSON 字符串'),
  120. },
  121. ]}
  122. onChange={(value) =>
  123. setInputs({
  124. ...inputs,
  125. Chats: value,
  126. })
  127. }
  128. />
  129. </Form.Section>
  130. </Form>
  131. <Space>
  132. <Button onClick={onSubmit}>{t('保存聊天设置')}</Button>
  133. </Space>
  134. </Spin>
  135. );
  136. }