import { useState, useEffect } from 'react'; import { YOUTUBE_GRAPHICS_LIST } from "../../../constants/constant.js"; import { readYamlConfig, updateYamlConfig } from '../../utils/yamlHelper'; import Toast from "../toast.jsx"; import { ConfigInput, ConfigSelect } from '../common/ConfigItem'; // 定义配置项 const YOUTUBE_CONFIG = { inputs: [ { key: 'youtubeCookiePath', label: 'Cookie文件路径', type: 'text', placeholder: '请输入Cookie.txt文件路径...' }, { key: 'youtubeClipTime', label: '最大截取时长(秒)', type: 'number', hint: '建议不超过5分钟' }, { key: 'youtubeDuration', label: '视频时长限制(秒)', type: 'number', hint: '建议不超过30分钟' } ], selects: [ { key: 'youtubeGraphicsOptions', label: '下载画质', options: YOUTUBE_GRAPHICS_LIST, hint: '0为原画' } ] }; // 默认配置 const DEFAULT_CONFIG = { youtubeGraphicsOptions: 720, youtubeClipTime: 0, youtubeDuration: 480, youtubeCookiePath: '' }; export default function Youtube() { 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('youtube-toast-success').classList.remove('hidden'); setTimeout(() => { document.getElementById('youtube-toast-success').classList.add('hidden'); }, 3000); } } catch (error) { console.error('保存配置失败:', error); } finally { setLoading(false); } }; const handleConfigChange = (key, value) => { setConfig(prev => ({ ...prev, [key]: value })); }; return (

YouTube 配置

基础配置

{/* 输入框配置 */}
{YOUTUBE_CONFIG.inputs.map(item => ( handleConfigChange(item.key, value)} placeholder={item.placeholder} /> ))}
{/* 选择框配置 */}
{YOUTUBE_CONFIG.selects.map(item => ( handleConfigChange(item.key, value)} options={item.options} /> ))}
{/* 保存按钮 */}
); }