1// import {Quality} from '@/constants/commonConst'; 2import {CustomizedColors} from '@/hooks/useColors'; 3import {getStorage, setStorage} from '@/utils/storage'; 4import produce from 'immer'; 5import {useEffect, useState} from 'react'; 6 7type ExceptionType = IMusic.IMusicItem | IMusic.IMusicItem[] | IMusic.IQuality; 8interface IConfig { 9 setting: { 10 basic: { 11 autoPlayWhenAppStart: boolean; 12 /** 使用移动网络播放 */ 13 useCelluarNetworkPlay: boolean; 14 /** 使用移动网络下载 */ 15 useCelluarNetworkDownload: boolean; 16 /** 最大同时下载 */ 17 maxDownload: number | string; 18 /** 播放歌曲行为 */ 19 clickMusicInSearch: '播放歌曲' | '播放歌曲并替换播放列表'; 20 /** 点击专辑单曲 */ 21 clickMusicInAlbum: '播放专辑' | '播放单曲'; 22 /** 下载文件夹 */ 23 downloadPath: string; 24 /** 同时播放 */ 25 notInterrupt: boolean; 26 /** 打断时 */ 27 tempRemoteDuck: '暂停' | '降低音量'; 28 /** 播放错误时自动停止 */ 29 autoStopWhenError: boolean; 30 /** 插件缓存策略 todo */ 31 pluginCacheControl: string; 32 /** 最大音乐缓存 */ 33 maxCacheSize: number; 34 /** 默认播放音质 */ 35 defaultPlayQuality: IMusic.IQualityKey; 36 /** 音质顺序 */ 37 playQualityOrder: 'asc' | 'desc'; 38 /** 默认下载音质 */ 39 defaultDownloadQuality: IMusic.IQualityKey; 40 /** 下载音质顺序 */ 41 downloadQualityOrder: 'asc' | 'desc'; 42 /** 歌曲详情页 */ 43 musicDetailDefault: 'album' | 'lyric'; 44 /** 歌曲详情页常亮 */ 45 musicDetailAwake: boolean; 46 debug: { 47 errorLog: boolean; 48 traceLog: boolean; 49 devLog: boolean; 50 }; 51 /** 最大历史记录条目 */ 52 maxHistoryLen: number; 53 /** 启动时自动更新插件 */ 54 autoUpdatePlugin: boolean; 55 // 不检查插件版本号 56 notCheckPluginVersion: boolean; 57 /** 关联歌词方式 */ 58 associateLyricType: 'input' | 'search'; 59 }; 60 /** 歌词 */ 61 lyric: { 62 showStatusBarLyric: boolean; 63 topPercent: number; 64 leftPercent: number; 65 align: number; 66 color: string; 67 backgroundColor: string; 68 widthPercent: number; 69 fontSize: number; 70 }; 71 72 /** 主题 */ 73 theme: { 74 background: string; 75 backgroundOpacity: number; 76 backgroundBlur: number; 77 colors: CustomizedColors; 78 customColors?: CustomizedColors; 79 followSystem: boolean; 80 selectedTheme: string; 81 }; 82 83 backup: { 84 resumeMode: 'append' | 'overwrite'; 85 }; 86 87 plugin: { 88 subscribeUrl: string; 89 }; 90 webdav: { 91 url: string; 92 username: string; 93 password: string; 94 }; 95 }; 96 status: { 97 music: { 98 /** 当前的音乐 */ 99 track: IMusic.IMusicItem; 100 /** 进度 */ 101 progress: number; 102 /** 模式 */ 103 repeatMode: string; 104 /** 列表 */ 105 musicQueue: IMusic.IMusicItem[]; 106 /** 速度 */ 107 rate: number; 108 }; 109 app: { 110 /** 跳过特定版本 */ 111 skipVersion: string; 112 }; 113 }; 114} 115 116type FilterType<T, R = never> = T extends Record<string | number, any> 117 ? { 118 [P in keyof T]: T[P] extends ExceptionType ? R : T[P]; 119 } 120 : never; 121 122type KeyPaths< 123 T extends object, 124 Root extends boolean = true, 125 R = FilterType<T, ''>, 126 K extends keyof R = keyof R, 127> = K extends string | number 128 ? 129 | (Root extends true ? `${K}` : `.${K}`) 130 | (R[K] extends Record<string | number, any> 131 ? `${Root extends true ? `${K}` : `.${K}`}${KeyPaths< 132 R[K], 133 false 134 >}` 135 : never) 136 : never; 137 138type KeyPathValue<T extends object, K extends string> = T extends Record< 139 string | number, 140 any 141> 142 ? K extends `${infer S}.${infer R}` 143 ? KeyPathValue<T[S], R> 144 : T[K] 145 : never; 146 147type KeyPathsObj< 148 T extends object, 149 K extends string = KeyPaths<T>, 150> = T extends Record<string | number, any> 151 ? { 152 [R in K]: KeyPathValue<T, R>; 153 } 154 : never; 155 156type DeepPartial<T> = { 157 [K in keyof T]?: T[K] extends Record<string | number, any> 158 ? T[K] extends ExceptionType 159 ? T[K] 160 : DeepPartial<T[K]> 161 : T[K]; 162}; 163 164export type IConfigPaths = KeyPaths<IConfig>; 165type PartialConfig = DeepPartial<IConfig> | null; 166type IConfigPathsObj = KeyPathsObj<DeepPartial<IConfig>, IConfigPaths>; 167 168let config: PartialConfig = null; 169/** 初始化config */ 170async function setup() { 171 config = (await getStorage('local-config')) ?? {}; 172 // await checkValidPath(['setting.theme.background']); 173 notify(); 174} 175 176/** 设置config */ 177async function setConfig<T extends IConfigPaths>( 178 key: T, 179 value: IConfigPathsObj[T], 180 shouldNotify = true, 181) { 182 if (config === null) { 183 return; 184 } 185 const keys = key.split('.'); 186 187 const result = produce(config, draft => { 188 draft[keys[0] as keyof IConfig] = draft[keys[0] as keyof IConfig] ?? {}; 189 let conf: any = draft[keys[0] as keyof IConfig]; 190 for (let i = 1; i < keys.length - 1; ++i) { 191 if (!conf?.[keys[i]]) { 192 conf[keys[i]] = {}; 193 } 194 conf = conf[keys[i]]; 195 } 196 conf[keys[keys.length - 1]] = value; 197 return draft; 198 }); 199 200 setStorage('local-config', result); 201 config = result; 202 if (shouldNotify) { 203 notify(); 204 } 205} 206 207// todo: 获取兜底 208/** 获取config */ 209function getConfig(): PartialConfig; 210function getConfig<T extends IConfigPaths>(key: T): IConfigPathsObj[T]; 211function getConfig(key?: string) { 212 let result: any = config; 213 if (key && config) { 214 result = getPathValue(config, key); 215 } 216 217 return result; 218} 219 220/** 通过path获取值 */ 221function getPathValue(obj: Record<string, any>, path: string) { 222 const keys = path.split('.'); 223 let tmp = obj; 224 for (let i = 0; i < keys.length; ++i) { 225 tmp = tmp?.[keys[i]]; 226 } 227 return tmp; 228} 229 230/** 同步hook */ 231const notifyCbs = new Set<() => void>(); 232function notify() { 233 notifyCbs.forEach(_ => _?.()); 234} 235 236/** hook */ 237function useConfig(): PartialConfig; 238function useConfig<T extends IConfigPaths>(key: T): IConfigPathsObj[T]; 239function useConfig(key?: string) { 240 // TODO: 应该有性能损失 241 const [_cfg, _setCfg] = useState<PartialConfig>(config); 242 function setCfg() { 243 _setCfg(config); 244 } 245 useEffect(() => { 246 notifyCbs.add(setCfg); 247 return () => { 248 notifyCbs.delete(setCfg); 249 }; 250 }, []); 251 252 if (key) { 253 return _cfg ? getPathValue(_cfg, key) : undefined; 254 } else { 255 return _cfg; 256 } 257} 258 259const Config = { 260 get: getConfig, 261 set: setConfig, 262 useConfig, 263 setup, 264}; 265 266export default Config; 267