From f2502761803ba617bf2b23a350868faf716ac568 Mon Sep 17 00:00:00 2001 From: PalmDevs Date: Tue, 22 Jul 2025 18:22:56 +0700 Subject: [PATCH] fix(plugins/settings): don't add custom sections twice, cleanup --- src/plugins/start/settings/index.ts | 47 +++++++++++++++++++++-------- 1 file changed, 34 insertions(+), 13 deletions(-) diff --git a/src/plugins/start/settings/index.ts b/src/plugins/start/settings/index.ts index cb2468e..038d93d 100644 --- a/src/plugins/start/settings/index.ts +++ b/src/plugins/start/settings/index.ts @@ -58,6 +58,7 @@ function patchSettingsNavigator(exports: any) { const reRender = useReRender() useEffect(() => { sRefresher.callNavigator = reRender + return () => { sRefresher.navigator = false sRefresher.callNavigator = noop @@ -66,7 +67,11 @@ function patchSettingsNavigator(exports: any) { // useMemo(() => getSettingScreens(), []) const unpatchMemo = instead(React, 'useMemo', (args, orig) => { - if (!args[1]?.length && sRefresher.navigator) args[1] = undefined + if (!args[1]?.length && sRefresher.navigator) { + args[1] = undefined + sRefresher.navigator = false + } + return Reflect.apply(orig, React, args) }) @@ -76,6 +81,8 @@ function patchSettingsNavigator(exports: any) { }) } +let sectionsInst: object | undefined + function patchSettingsOverviewScreen(exports: any) { instead( exports as { @@ -86,33 +93,47 @@ function patchSettingsOverviewScreen(exports: any) { const reRender = useReRender() useEffect(() => { sRefresher.callOverviewScreen = reRender + return () => { sRefresher.overviewScreen = false sRefresher.callOverviewScreen = noop } }, [reRender]) - // In: SearchableSettingsList - // useMemo(() => { - // return doSettingsBlocklistFiltering(props.sections, canShowNitroSettings) - // }, [canShowNitroSettings]) + /** + * In useOverviewSettings (called by SettingsOverviewScreen): + * + * const hasPremiumSubscriptionToDisplay = useHasPremiumSubscriptionToDisplay() + * const sections = useMemo(() => + * (...constructed sections array...), + * [hasPremiumSubscriptionToDisplay]) + */ const unpatchMemo = instead(React, 'useMemo', (args, orig) => { if (sRefresher.overviewScreen) args[1] = undefined - const sections = Reflect.apply(orig, React, args) + let sections = Reflect.apply(orig, React, args) - // Add our custom sections here - for (const section of Object.values(sSections)) - if (section.index) - sections.splice(section.index, 0, section) - else sections.unshift(section) + // Add our custom sections here, and only do this per instance! + if (sectionsInst !== sections) { + for (const section of Object.values(sSections)) + if (section.index) + sections.splice(section.index, 0, section) + else sections.unshift(section) + + sectionsInst = sections + } + + if (sRefresher.overviewScreen) { + sections = sectionsInst = [...sections] + sRefresher.overviewScreen = false + } return sections }) - const tree = Reflect.apply(orig, undefined, args) + const fiber = Reflect.apply(orig, undefined, args) unpatchMemo() - return tree + return fiber }, ) }