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