import { useState, useEffect } from 'react'; import { BILI_CDN_SELECT_LIST, BILI_DOWNLOAD_METHOD, BILI_RESOLUTION_LIST } from "../../../constants/constant.js"; import { readYamlConfig, updateYamlConfig } from '../../utils/yamlHelper'; import Toast from "../toast.jsx"; import { ConfigToggle, ConfigInput, ConfigSelect } from '../common/ConfigItem'; // 定义配置项 const BILI_CONFIG = { toggles: [ { key: 'biliDisplayCover', label: '显示封面' }, { key: 'biliDisplayInfo', label: '显示视频信息' }, { key: 'biliDisplayIntro', label: '显示简介' }, { key: 'biliDisplayOnline', label: '显示在线人数' }, { key: 'biliDisplaySummary', label: '显示总结' }, { key: 'biliUseBBDown', label: '使用BBDown' }, ], inputs: [ { key: 'biliSessData', label: 'SESSDATA', type: 'text', placeholder: '请输入Bilibili SESSDATA' }, { key: 'biliDuration', label: '视频时长限制(秒)', type: 'number' }, { key: 'biliIntroLenLimit', label: '简介长度限制', type: 'number' }, ], selects: [ { key: 'biliCDN', label: 'CDN选择', options: BILI_CDN_SELECT_LIST }, { key: 'biliDownloadMethod', label: '下载方式', options: BILI_DOWNLOAD_METHOD }, { key: 'biliResolution', label: '视频画质', options: BILI_RESOLUTION_LIST }, ] }; // 默认配置 const DEFAULT_CONFIG = { biliSessData: '', biliDuration: 480, biliIntroLenLimit: 50, biliDisplayCover: true, biliDisplayInfo: true, biliDisplayIntro: true, biliDisplayOnline: true, biliDisplaySummary: false, biliUseBBDown: false, biliCDN: 0, biliDownloadMethod: 0, biliResolution: 5 }; export default function Bili() { 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('toast-success').classList.remove('hidden'); setTimeout(() => { document.getElementById('toast-success').classList.add('hidden'); }, 3000); } } catch (error) { console.error('保存配置失败:', error); } finally { setLoading(false); } }; const handleConfigChange = (key, value) => { setConfig(prev => ({ ...prev, [key]: value })); }; return (

Bilibili 配置

基础配置

{/* 输入框配置 */}
{BILI_CONFIG.inputs.map(item => ( handleConfigChange(item.key, value)} placeholder={item.placeholder} /> ))}
{/* 开关配置 */}
{BILI_CONFIG.toggles.map(item => ( handleConfigChange(item.key, value)} /> ))}
{/* 选择框配置 */}
{BILI_CONFIG.selects.map(item => ( handleConfigChange(item.key, value)} options={item.options} /> ))}
{/* 操作按钮 */}
); }