SettingsDataDashboard.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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))
  48. return showError(t('部分保存失败,请重试'));
  49. }
  50. showSuccess(t('保存成功'));
  51. props.refresh();
  52. })
  53. .catch(() => {
  54. showError(t('保存失败,请重试'));
  55. })
  56. .finally(() => {
  57. setLoading(false);
  58. });
  59. }
  60. useEffect(() => {
  61. const currentInputs = {};
  62. for (let key in props.options) {
  63. if (Object.keys(inputs).includes(key)) {
  64. currentInputs[key] = props.options[key];
  65. }
  66. }
  67. setInputs(currentInputs);
  68. setInputsRow(structuredClone(currentInputs));
  69. refForm.current.setValues(currentInputs);
  70. localStorage.setItem(
  71. 'data_export_default_time',
  72. String(inputs.DataExportDefaultTime),
  73. );
  74. }, [props.options]);
  75. return (
  76. <>
  77. <Spin spinning={loading}>
  78. <Form
  79. values={inputs}
  80. getFormApi={(formAPI) => (refForm.current = formAPI)}
  81. style={{ marginBottom: 15 }}
  82. >
  83. <Form.Section text={t('数据看板设置')}>
  84. <Row gutter={16}>
  85. <Col xs={24} sm={12} md={8} lg={8} xl={8}>
  86. <Form.Switch
  87. field={'DataExportEnabled'}
  88. label={t('启用数据看板(实验性)')}
  89. size='default'
  90. checkedText='|'
  91. uncheckedText='〇'
  92. onChange={(value) => {
  93. setInputs({
  94. ...inputs,
  95. DataExportEnabled: value,
  96. });
  97. }}
  98. />
  99. </Col>
  100. </Row>
  101. <Row>
  102. <Col xs={24} sm={12} md={8} lg={8} xl={8}>
  103. <Form.InputNumber
  104. label={t('数据看板更新间隔')}
  105. step={1}
  106. min={1}
  107. suffix={t('分钟')}
  108. extraText={t('设置过短会影响数据库性能')}
  109. placeholder={t('数据看板更新间隔')}
  110. field={'DataExportInterval'}
  111. onChange={(value) =>
  112. setInputs({
  113. ...inputs,
  114. DataExportInterval: String(value),
  115. })
  116. }
  117. />
  118. </Col>
  119. <Col xs={24} sm={12} md={8} lg={8} xl={8}>
  120. <Form.Select
  121. label={t('数据看板默认时间粒度')}
  122. optionList={optionsDataExportDefaultTime}
  123. field={'DataExportDefaultTime'}
  124. extraText={t('仅修改展示粒度,统计精确到小时')}
  125. placeholder={t('数据看板默认时间粒度')}
  126. style={{ width: 180 }}
  127. onChange={(value) =>
  128. setInputs({
  129. ...inputs,
  130. DataExportDefaultTime: String(value),
  131. })
  132. }
  133. />
  134. </Col>
  135. </Row>
  136. <Row>
  137. <Button size='default' onClick={onSubmit}>
  138. {t('保存数据看板设置')}
  139. </Button>
  140. </Row>
  141. </Form.Section>
  142. </Form>
  143. </Spin>
  144. </>
  145. );
  146. }