import { useState, useEffect } from 'react'; import { NETEASECLOUD_QUALITY_LIST } from "../../../constants/constant.js"; import { readYamlConfig, updateYamlConfig } from '../../utils/yamlHelper'; import Toast from "../toast.jsx"; import { ConfigToggle, ConfigInput, ConfigSelect } from '../common/ConfigItem'; // 定义配置项 const NCM_CONFIG = { textareas: [ { key: 'neteaseCookie', label: 'Cookie', placeholder: '请输入网易云Cookie...' } ], inputs: [ { key: 'neteaseCloudAPIServer', label: '自建API服务器地址', type: 'text', placeholder: '请输入API服务器地址...' }, { key: 'neteaseUserId', label: '用户ID', type: 'text', placeholder: '网易云用户ID', hint: '不要手动更改!' }, { key: 'songRequestMaxList', label: '点歌最大列表数', type: 'number' } ], toggles: [ { key: 'useLocalNeteaseAPI', label: '使用自建API' }, { key: 'useNeteaseSongRequest', label: '开启点歌功能' }, { key: 'isSendVocal', label: '发送群语音' } ], selects: [ { key: 'neteaseCloudAudioQuality', label: '音频质量', options: NETEASECLOUD_QUALITY_LIST } ] }; // 默认配置 const DEFAULT_CONFIG = { useLocalNeteaseAPI: false, useNeteaseSongRequest: false, isSendVocal: true, songRequestMaxList: 10, neteaseCookie: '', neteaseCloudAPIServer: '', neteaseCloudAudioQuality: 'exhigh', neteaseUserId: '' }; export default function Ncm() { const [config, setConfig] = useState(DEFAULT_CONFIG); const [loading, setLoading] = useState(false); useEffect(() => { const loadConfig = async () => { const yamlConfig = await readYamlConfig(); if (yamlConfig) { const newConfig = {}; Object.keys(DEFAULT_CONFIG).forEach(key => { newConfig[key] = yamlConfig[key] ?? DEFAULT_CONFIG[key]; }); setConfig(newConfig); } }; loadConfig(); }, []); const handleSave = async () => { setLoading(true); try { const success = await updateYamlConfig(config); if (success) { document.getElementById('ncm-toast-success').classList.remove('hidden'); setTimeout(() => { document.getElementById('ncm-toast-success').classList.add('hidden'); }, 3000); } } catch (error) { console.error('保存配置失败:', error); } finally { setLoading(false); } }; const handleConfigChange = (key, value) => { setConfig(prev => ({ ...prev, [key]: value })); }; return (