From 4070a5d678dd4988676f5d5bbc5312fdf65d1311 Mon Sep 17 00:00:00 2001 From: lovegaoshi <106490582+lovegaoshi@users.noreply.github.com> Date: Wed, 18 Dec 2024 14:21:34 -0800 Subject: [PATCH] feat: mfsdk --- .../developer/plugins/musicfree/Searchbar.tsx | 29 +++++-------------- .../developer/plugins/musicfree/View.tsx | 8 +++++ src/stores/useMFsdk.ts | 11 ++++--- src/utils/mfsdk.ts | 5 ++-- 4 files changed, 25 insertions(+), 28 deletions(-) diff --git a/src/components/setting/developer/plugins/musicfree/Searchbar.tsx b/src/components/setting/developer/plugins/musicfree/Searchbar.tsx index 1ab24cd1..64b408f8 100644 --- a/src/components/setting/developer/plugins/musicfree/Searchbar.tsx +++ b/src/components/setting/developer/plugins/musicfree/Searchbar.tsx @@ -4,40 +4,25 @@ import { useTranslation } from 'react-i18next'; import { useNoxSetting } from '@stores/useApp'; import logger from '@utils/Logger'; import SearchBar from '@components/commonui/SearchBar'; -import { getUniqObjects } from '@utils/Utils'; +import { fetchMFsdk } from '@utils/mfsdk'; export default () => { const { t } = useTranslation(); - const playerStyles = useNoxSetting(state => state.playerStyles); - const setPlayerStyles = useNoxSetting(state => state.setPlayerStyles); - const loadCustomSkin = (skins: NoxTheme.Style[]) => { - // skins MUST BE an array of objects - if (!Array.isArray(skins)) { - throw new Error('requested skin URL is not an array. aborting.'); - } - const uniqueSkins = getUniqObjects( - skins.filter(skin => skin.metaData).concat(playerStyles), - () => '', - ); - setPlayerStyles(uniqueSkins); - }; - + const addMFsdks = useNoxSetting(state => state.addMFsdks); return ( { try { - const res = await fetch(v); - const searchedResult = await res.json(); - loadCustomSkin(searchedResult); + addMFsdks(await fetchMFsdk(v)); } catch (e) { - logger.warn(`[SkinSearchbar] failed to search ${e}`); + logger.warn(`[mfsdk] failed to search ${v}: ${e}`); setSnack({ - snackMsg: { success: t('CustomSkin.SearchFailMsg') }, + snackMsg: { success: t('MFSDK.SearchFailMsg') }, }); } }} - placeholder={t('CustomSkin.SearchBarLabel')} + placeholder={t('MFSDK.url')} /> ); }; diff --git a/src/components/setting/developer/plugins/musicfree/View.tsx b/src/components/setting/developer/plugins/musicfree/View.tsx index 57d74e88..4b834035 100644 --- a/src/components/setting/developer/plugins/musicfree/View.tsx +++ b/src/components/setting/developer/plugins/musicfree/View.tsx @@ -11,11 +11,19 @@ import { FlashList } from '@shopify/flash-list'; import { styles } from '@components/style'; import Searchbar from './Searchbar'; +import { useNoxSetting } from '@stores/useApp'; const MFSettings = () => { + const MFsdks = useNoxSetting(state => state.MFsdks); return ( + ( + {`${item.platform} ${item.version}`} + )} + /> ); }; diff --git a/src/stores/useMFsdk.ts b/src/stores/useMFsdk.ts index 460e5955..e22aa66d 100644 --- a/src/stores/useMFsdk.ts +++ b/src/stores/useMFsdk.ts @@ -8,23 +8,26 @@ export interface MFsdkStore { MFsdks: MFsdk[]; setMFsdks: (mf: MFsdk[]) => void; addMFsdks: (mf: MFsdk[]) => void; - removeMFsdk: (mf: MFsdk) => void; + rmMFsdks: (mf: MFsdk[]) => void; } const store: StateCreator = (set, get) => ({ MFsdks: [], setMFsdks: mf => set({ MFsdks: mf }), addMFsdks: mf => { + if (mf.length === 0) return; addMFsdks(mf.map(v => v.path)); set(s => ({ MFsdks: getUniqObjects([...mf, ...s.MFsdks], sdk => sdk.srcUrl), })); }, - removeMFsdk: mf => { + rmMFsdks: mf => { + if (mf.length === 0) return; const { MFsdks } = get(); - rmMFsdks(MFsdks.filter(v => v.srcUrl === mf.srcUrl).map(v => v.path)); + const rmUrls = mf.map(v => v.srcUrl); + rmMFsdks(MFsdks.filter(v => rmUrls.includes(v.srcUrl)).map(v => v.path)); set(s => ({ - MFsdks: s.MFsdks.filter(v => v.srcUrl !== mf.srcUrl), + MFsdks: s.MFsdks.filter(v => !rmUrls.includes(v.srcUrl)), })); }, }); diff --git a/src/utils/mfsdk.ts b/src/utils/mfsdk.ts index a92a018c..4d3988b4 100644 --- a/src/utils/mfsdk.ts +++ b/src/utils/mfsdk.ts @@ -20,7 +20,7 @@ export const rmMFsdks = async (paths: string[]) => { export const addMFsdks = async (paths: string[]) => { const mfsdkPaths = await getMFsdk(); - saveItem(StorageKeys.MFSDK_PATHS, [...mfsdkPaths, ...paths]); + saveItem(StorageKeys.MFSDK_PATHS, [...new Set([...mfsdkPaths, ...paths])]); }; export const initMFsdk = async () => { @@ -47,8 +47,9 @@ export const fetchMFsdk = async (url: string) => { const sdkLocalPath = `${loadedSDK.platform}.${loadedSDK.version}.js`; loadedSDK.path = sdkLocalPath; writeTxtFile(sdkLocalPath, [text], mfsdkSubFolder); + return [loadedSDK]; } catch { logger.warn(`[mfsdk] failed to fetch and parse ${url}`); } - return; + return []; };