/* Copyright (C) 2025 QuantumNous This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . For commercial licensing, please contact support@quantumnous.com */ import React, { useEffect, useRef, useState } from 'react'; import { Banner, Button, Col, Form, Row, Spin } from '@douyinfe/semi-ui'; import { useTranslation } from 'react-i18next'; import { API, compareObjects, showError, showSuccess, showWarning, toBoolean, } from '../../../helpers'; // InputNumber 清空时 onChange 会收到 ''/null,parseInt 得到 NaN, // String(NaN) 会把字面量 "NaN" 永久落库(同 SettingsMonitoring 的既有教训)。 // 统一经此转换:非法输入回退空串,后端 Atoi('') 按 0 处理,再由 setting 层的 // GetXxx() 回退默认值。 const toIntString = (value) => { const parsed = parseInt(value, 10); return Number.isNaN(parsed) ? '' : String(parsed); }; const DEFAULTS = { SkillMarketRequireAuth: false, SkillInjectEnabled: false, SkillInjectMaxSkills: '5', SkillInjectMaxRunes: '3000', SkillInjectCacheTTLSeconds: '60', }; export default function SettingsSkillInject(props) { const { t } = useTranslation(); const [loading, setLoading] = useState(false); const [inputs, setInputs] = useState(DEFAULTS); const [inputsRow, setInputsRow] = useState(DEFAULTS); const refForm = useRef(); function onSubmit() { const updateArray = compareObjects(inputs, inputsRow); if (!updateArray.length) return showWarning(t('你似乎并没有修改什么')); const requestQueue = updateArray.map((item) => { const raw = inputs[item.key]; const value = typeof raw === 'boolean' ? String(raw) : String(raw); return API.put('/api/option/', { key: item.key, value }); }); setLoading(true); Promise.all(requestQueue) .then((res) => { if (res.includes(undefined)) { return showError(t('部分保存失败,请重试')); } showSuccess(t('保存成功')); props.refresh(); }) .catch(() => showError(t('保存失败,请重试'))) .finally(() => setLoading(false)); } useEffect(() => { const currentInputs = {}; for (const key in DEFAULTS) { const value = Object.prototype.hasOwnProperty.call(props.options, key) ? props.options[key] : DEFAULTS[key]; currentInputs[key] = typeof DEFAULTS[key] === 'boolean' ? toBoolean(value) : value; } setInputs(currentInputs); setInputsRow(structuredClone(currentInputs)); refForm.current?.setValues(currentInputs); }, [props.options]); return (
(refForm.current = formAPI)} style={{ marginBottom: 15 }} > setInputs({ ...inputs, SkillMarketRequireAuth: value }) } /> setInputs({ ...inputs, SkillInjectEnabled: value }) } /> setInputs({ ...inputs, SkillInjectMaxSkills: toIntString(value), }) } /> setInputs({ ...inputs, SkillInjectMaxRunes: toIntString(value), }) } /> setInputs({ ...inputs, SkillInjectCacheTTLSeconds: toIntString(value), }) } />
); }