SettingsDataDashboard.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import React, { useEffect, useState, useRef } from 'react';
  2. import { Button, Col, Form, Row, Spin } from '@douyinfe/semi-ui';
  3. import {
  4. compareObjects,
  5. API,
  6. showError,
  7. showSuccess,
  8. showWarning,
  9. } from '../../../helpers';
  10. import { useTranslation } from 'react-i18next';
  11. export default function DataDashboard(props) {
  12. const { t } = useTranslation();
  13. const optionsDataExportDefaultTime = [
  14. { key: 'hour', label: t('小时'), value: 'hour' },
  15. { key: 'day', label: t('天'), value: 'day' },
  16. { key: 'week', label: t('周'), value: 'week' },
  17. ];
  18. const [loading, setLoading] = useState(false);
  19. const [inputs, setInputs] = useState({
  20. DataExportEnabled: false,
  21. DataExportInterval: '',
  22. DataExportDefaultTime: '',
  23. });
  24. const refForm = useRef();
  25. const [inputsRow, setInputsRow] = useState(inputs);
  26. function onSubmit() {
  27. const updateArray = compareObjects(inputs, inputsRow);
  28. if (!updateArray.length) return showWarning(t('你似乎并没有修改什么'));
  29. const requestQueue = updateArray.map((item) => {
  30. let value = '';
  31. if (typeof inputs[item.key] === 'boolean') {
  32. value = String(inputs[item.key]);
  33. } else {
  34. value = inputs[item.key];
  35. }
  36. return API.put('/api/option/', {
  37. key: item.key,
  38. value,
  39. });
  40. });
  41. setLoading(true);
  42. Promise.all(requestQueue)
  43. .then((res) => {
  44. if (requestQueue.length === 1) {
  45. if (res.includes(undefined)) return;
  46. } else if (requestQueue.length > 1) {
  47. if (res.includes(undefined)) return showError(t('部分保存失败,请重试'));
  48. }
  49. showSuccess(t('保存成功'));
  50. props.refresh();
  51. })
  52. .catch(() => {
  53. showError(t('保存失败,请重试'));
  54. })
  55. .finally(() => {
  56. setLoading(false);
  57. });
  58. }
  59. useEffect(() => {
  60. const currentInputs = {};
  61. for (let key in props.options) {
  62. if (Object.keys(inputs).includes(key)) {
  63. currentInputs[key] = props.options[key];
  64. }
  65. }
  66. setInputs(currentInputs);
  67. setInputsRow(structuredClone(currentInputs));
  68. refForm.current.setValues(currentInputs);
  69. localStorage.setItem(
  70. 'data_export_default_time',
  71. String(inputs.DataExportDefaultTime),
  72. );
  73. }, [props.options]);
  74. return (
  75. <>
  76. <Spin spinning={loading}>
  77. <Form
  78. values={inputs}
  79. getFormApi={(formAPI) => (refForm.current = formAPI)}
  80. style={{ marginBottom: 15 }}
  81. >
  82. <Form.Section text={t('数据看板设置')}>
  83. <Row gutter={16}>
  84. <Col span={8}>
  85. <Form.Switch
  86. field={'DataExportEnabled'}
  87. label={t('启用数据看板(实验性)')}
  88. size='default'
  89. checkedText='|'
  90. uncheckedText='〇'
  91. onChange={(value) => {
  92. setInputs({
  93. ...inputs,
  94. DataExportEnabled: value,
  95. });
  96. }}
  97. />
  98. </Col>
  99. </Row>
  100. <Row>
  101. <Col span={8}>
  102. <Form.InputNumber
  103. label={t('数据看板更新间隔')}
  104. step={1}
  105. min={1}
  106. suffix={t('分钟')}
  107. extraText={t('设置过短会影响数据库性能')}
  108. placeholder={t('数据看板更新间隔')}
  109. field={'DataExportInterval'}
  110. onChange={(value) =>
  111. setInputs({
  112. ...inputs,
  113. DataExportInterval: String(value),
  114. })
  115. }
  116. />
  117. </Col>
  118. <Col span={8}>
  119. <Form.Select
  120. label={t('数据看板默认时间粒度')}
  121. optionList={optionsDataExportDefaultTime}
  122. field={'DataExportDefaultTime'}
  123. extraText={t('仅修改展示粒度,统计精确到小时')}
  124. placeholder={t('数据看板默认时间粒度')}
  125. style={{ width: 180 }}
  126. onChange={(value) =>
  127. setInputs({
  128. ...inputs,
  129. DataExportDefaultTime: String(value),
  130. })
  131. }
  132. />
  133. </Col>
  134. </Row>
  135. <Row>
  136. <Button size='default' onClick={onSubmit}>
  137. {t('保存数据看板设置')}
  138. </Button>
  139. </Row>
  140. </Form.Section>
  141. </Form>
  142. </Spin>
  143. </>
  144. );
  145. }