revenge-bundle-next/src/plugins/init/settings/index.ts
2025-05-22 00:06:36 +07:00

100 lines
3.8 KiB
TypeScript

import { InternalPluginFlags, registerPlugin } from '@revenge-mod/plugins/_'
import { PluginFlags } from '@revenge-mod/plugins/constants'
import { byName, byProps } from '@revenge-mod/modules/finders/filters'
import { waitForModules } from '@revenge-mod/modules/finders/wait'
import { _data, _subs } from '@revenge-mod/discord/_/modules/settings'
import { type SettingsItem, onSettingsModulesLoaded } from '@revenge-mod/discord/modules/settings'
import { after } from '@revenge-mod/patcher'
import type { FC } from 'react'
registerPlugin(
{
id: 'revenge.settings',
name: 'Settings',
description: 'Settings menus for Revenge',
author: 'Revenge',
icon: 'SettingsIcon',
},
{
start({ logger }) {
onSettingsModulesLoaded(() => require('./register'))
const unsubForRendererConfig = waitForModules(
byProps<{
SETTING_RENDERER_CONFIG: Record<string, SettingsItem>
}>('SETTING_RENDERER_CONFIG'),
SettingRendererConfig => {
unsubForRendererConfig()
logger.info('Settings modules loaded, running subscriptions and patching...')
for (const sub of _subs) sub()
// We don't ever need to call this again
_subs.clear()
_data[2] = true
let ORIGINAL_RENDERER_CONFIG = SettingRendererConfig.SETTING_RENDERER_CONFIG
Object.defineProperty(SettingRendererConfig, 'SETTING_RENDERER_CONFIG', {
get: () =>
({
...ORIGINAL_RENDERER_CONFIG,
..._data[1],
}) as Record<string, SettingsItem>,
set: v => (ORIGINAL_RENDERER_CONFIG = v),
})
},
)
const unsubForSettingsOverviewScreen = waitForModules(
byName('SettingsOverviewScreen'),
exports => {
unsubForSettingsOverviewScreen()
const customSections = _data[0]
after(exports as { default: FC }, 'default', tree => {
const {
props: { sections },
} = tree as {
props: {
sections: Array<{
label: string
settings: string[]
}>
}
}
// Check if we even have custom sections
const firstCustomSection = customSections[Object.keys(customSections)[0]]
if (!firstCustomSection) return tree
// Check if sections are already spliced
const firstCustomItem = firstCustomSection.settings[0]
if (
sections.findIndex(section =>
section.settings.some(setting => setting === firstCustomItem),
) !== -1
)
return tree
for (const section of Object.values(customSections))
if (!section.index) sections.unshift(section)
else sections.splice(section.index, 0, section)
return tree
})
},
{
returnNamespace: true,
},
)
},
},
PluginFlags.Enabled,
InternalPluginFlags.Internal | InternalPluginFlags.Essential,
)