SettingsLog.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import React, { useEffect, useState, useRef } from 'react';
  2. import { Button, Col, Form, Row, Spin, DatePicker } from '@douyinfe/semi-ui';
  3. import dayjs from 'dayjs';
  4. import { useTranslation } from 'react-i18next';
  5. import {
  6. compareObjects,
  7. API,
  8. showError,
  9. showSuccess,
  10. showWarning,
  11. } from '../../../helpers';
  12. export default function SettingsLog(props) {
  13. const { t } = useTranslation();
  14. const [loading, setLoading] = useState(false);
  15. const [loadingCleanHistoryLog, setLoadingCleanHistoryLog] = useState(false);
  16. const [inputs, setInputs] = useState({
  17. LogConsumeEnabled: false,
  18. historyTimestamp: dayjs().subtract(1, 'month').toDate(),
  19. });
  20. const refForm = useRef();
  21. const [inputsRow, setInputsRow] = useState(inputs);
  22. function onSubmit() {
  23. const updateArray = compareObjects(inputs, inputsRow).filter(
  24. (item) => item.key !== 'historyTimestamp',
  25. );
  26. if (!updateArray.length) return showWarning(t('你似乎并没有修改什么'));
  27. const requestQueue = updateArray.map((item) => {
  28. let value = '';
  29. if (typeof inputs[item.key] === 'boolean') {
  30. value = String(inputs[item.key]);
  31. } else {
  32. value = inputs[item.key];
  33. }
  34. return API.put('/api/option/', {
  35. key: item.key,
  36. value,
  37. });
  38. });
  39. setLoading(true);
  40. Promise.all(requestQueue)
  41. .then((res) => {
  42. if (requestQueue.length === 1) {
  43. if (res.includes(undefined)) return;
  44. } else if (requestQueue.length > 1) {
  45. if (res.includes(undefined)) return showError(t('部分保存失败,请重试'));
  46. }
  47. showSuccess(t('保存成功'));
  48. props.refresh();
  49. })
  50. .catch(() => {
  51. showError(t('保存失败,请重试'));
  52. })
  53. .finally(() => {
  54. setLoading(false);
  55. });
  56. }
  57. async function onCleanHistoryLog() {
  58. try {
  59. setLoadingCleanHistoryLog(true);
  60. if (!inputs.historyTimestamp) throw new Error(t('请选择日志记录时间'));
  61. const res = await API.delete(
  62. `/api/log/?target_timestamp=${Date.parse(inputs.historyTimestamp) / 1000}`,
  63. );
  64. const { success, message, data } = res.data;
  65. if (success) {
  66. showSuccess(`${data} ${t('条日志已清理!')}`);
  67. return;
  68. } else {
  69. throw new Error(t('日志清理失败:') + message);
  70. }
  71. } catch (error) {
  72. showError(error.message);
  73. } finally {
  74. setLoadingCleanHistoryLog(false);
  75. }
  76. }
  77. useEffect(() => {
  78. const currentInputs = {};
  79. for (let key in props.options) {
  80. if (Object.keys(inputs).includes(key)) {
  81. currentInputs[key] = props.options[key];
  82. }
  83. }
  84. currentInputs['historyTimestamp'] = inputs.historyTimestamp;
  85. setInputs(Object.assign(inputs, currentInputs));
  86. setInputsRow(structuredClone(currentInputs));
  87. refForm.current.setValues(currentInputs);
  88. }, [props.options]);
  89. return (
  90. <>
  91. <Spin spinning={loading}>
  92. <Form
  93. values={inputs}
  94. getFormApi={(formAPI) => (refForm.current = formAPI)}
  95. style={{ marginBottom: 15 }}
  96. >
  97. <Form.Section text={t('日志设置')}>
  98. <Row gutter={16}>
  99. <Col xs={24} sm={12} md={8} lg={8} xl={8}>
  100. <Form.Switch
  101. field={'LogConsumeEnabled'}
  102. label={t('启用额度消费日志记录')}
  103. size='default'
  104. checkedText='|'
  105. uncheckedText='〇'
  106. onChange={(value) => {
  107. setInputs({
  108. ...inputs,
  109. LogConsumeEnabled: value,
  110. });
  111. }}
  112. />
  113. </Col>
  114. <Col xs={24} sm={12} md={8} lg={8} xl={8}>
  115. <Spin spinning={loadingCleanHistoryLog}>
  116. <Form.DatePicker
  117. label={t('日志记录时间')}
  118. field={'historyTimestamp'}
  119. type='dateTime'
  120. inputReadOnly={true}
  121. onChange={(value) => {
  122. setInputs({
  123. ...inputs,
  124. historyTimestamp: value,
  125. });
  126. }}
  127. />
  128. <Button size='default' onClick={onCleanHistoryLog}>
  129. {t('清除历史日志')}
  130. </Button>
  131. </Spin>
  132. </Col>
  133. </Row>
  134. <Row>
  135. <Button size='default' onClick={onSubmit}>
  136. {t('保存日志设置')}
  137. </Button>
  138. </Row>
  139. </Form.Section>
  140. </Form>
  141. </Spin>
  142. </>
  143. );
  144. }