[Global] Code cleanup pass

This commit is contained in:
Beef 2023-04-15 19:54:23 +01:00
parent ffbb507125
commit 85d0bd263b
30 changed files with 65 additions and 80 deletions

View file

@ -15,18 +15,18 @@
"license": "BSD-3-Clause", "license": "BSD-3-Clause",
"devDependencies": { "devDependencies": {
"@react-native-clipboard/clipboard": "1.10.0", "@react-native-clipboard/clipboard": "1.10.0",
"@swc/core": "^1.3.35", "@swc/core": "^1.3.50",
"@types/chroma-js": "^2.4.0", "@types/chroma-js": "^2.4.0",
"@types/lodash": "^4.14.191", "@types/lodash": "^4.14.194",
"@types/react": "18.0.27", "@types/react": "18.0.35",
"@types/react-native": "0.70.6", "@types/react-native": "0.70.6",
"esbuild": "^0.17.11", "esbuild": "^0.17.16",
"esbuild-plugin-alias": "^0.2.1", "esbuild-plugin-alias": "^0.2.1",
"moment": "2.22.2", "moment": "2.22.2",
"typescript": "^4.9.5" "typescript": "^5.0.4"
}, },
"dependencies": { "dependencies": {
"@swc/helpers": "^0.4.14", "@swc/helpers": "^0.5.0",
"spitroast": "^1.4.2" "spitroast": "^1.4.2"
}, },
"pnpm": { "pnpm": {

2
src/def.d.ts vendored
View file

@ -436,7 +436,7 @@ interface VendettaObject {
Search: _React.ComponentType<SearchProps>; Search: _React.ComponentType<SearchProps>;
} }
toasts: { toasts: {
showToast: (content: string, asset: number) => void; showToast: (content: string, asset?: number) => void;
}; };
alerts: { alerts: {
showConfirmationAlert: (options: ConfirmationAlertOptions) => void; showConfirmationAlert: (options: ConfirmationAlertOptions) => void;

View file

@ -1,8 +1,8 @@
import { patchLogHook } from "@lib/debug"; import { patchLogHook } from "@lib/debug";
import { patchCommands } from "@lib/commands"; import { patchCommands } from "@lib/commands";
import { initPlugins } from "@lib/plugins"; import { initPlugins } from "@lib/plugins";
import { patchAssets } from "@ui/assets";
import { patchChatBackground } from "@lib/themes"; import { patchChatBackground } from "@lib/themes";
import { patchAssets } from "@ui/assets";
import initQuickInstall from "@ui/quickInstall"; import initQuickInstall from "@ui/quickInstall";
import initSafeMode from "@ui/safeMode"; import initSafeMode from "@ui/safeMode";
import initSettings from "@ui/settings"; import initSettings from "@ui/settings";

View file

@ -5,7 +5,7 @@ import { after } from "@lib/patcher";
let commands: ApplicationCommand[] = []; let commands: ApplicationCommand[] = [];
export function patchCommands() { export function patchCommands() {
const unpatch = after("getBuiltInCommands", commandsModule, (args, res) => res.concat(commands)); const unpatch = after("getBuiltInCommands", commandsModule, (_, res) => res.concat(commands));
return () => { return () => {
commands = []; commands = [];
unpatch(); unpatch();

View file

@ -1,8 +1,8 @@
import { RNConstants } from "@types"; import { RNConstants } from "@types";
import { ReactNative as RN } from "@metro/common"; import { ReactNative as RN } from "@metro/common";
import { after } from "@lib/patcher"; import { after } from "@lib/patcher";
import { ClientInfoManager, DeviceManager, BundleUpdaterManager } from "@lib/native";
import { getCurrentTheme, selectTheme } from "@lib/themes"; import { getCurrentTheme, selectTheme } from "@lib/themes";
import { ClientInfoManager, DeviceManager, BundleUpdaterManager } from "@lib/native";
import { getAssetIDByName } from "@ui/assets"; import { getAssetIDByName } from "@ui/assets";
import { showToast } from "@ui/toasts"; import { showToast } from "@ui/toasts";
import settings from "@lib/settings"; import settings from "@lib/settings";

View file

@ -11,8 +11,7 @@ function onDispatch({ locale }: { locale: string }) {
// Theming // Theming
// Based on https://github.com/Aliucord/AliucordRN/blob/main/src/ui/patchTheme.ts // Based on https://github.com/Aliucord/AliucordRN/blob/main/src/ui/patchTheme.ts
try { try {
const theme = ThemeStore.theme || "dark"; ThemeManager.overrideTheme(ThemeStore?.theme ?? "dark");
ThemeManager.overrideTheme(theme);
if (AMOLEDThemeManager && UnsyncedUserSettingsStore.useAMOLEDTheme === 2) AMOLEDThemeManager.setAMOLEDThemeEnabled(true); if (AMOLEDThemeManager && UnsyncedUserSettingsStore.useAMOLEDTheme === 2) AMOLEDThemeManager.setAMOLEDThemeEnabled(true);
} catch(e) { } catch(e) {
logger.error("Failed to fix theme...", e); logger.error("Failed to fix theme...", e);

View file

@ -4,14 +4,12 @@ import { MetroModules, PropsFinder, PropsFinderAll } from "@types";
declare const __r: (moduleId: number) => any; declare const __r: (moduleId: number) => any;
// Function to blacklist a module, preventing it from being searched again // Function to blacklist a module, preventing it from being searched again
function blacklist(id: number) { const blacklist = (id: number) => Object.defineProperty(window.modules, id, {
Object.defineProperty(window.modules, id, {
value: window.modules[id], value: window.modules[id],
enumerable: false, enumerable: false,
configurable: true, configurable: true,
writable: true writable: true
}) });
}
// Blacklist any "bad-actor" modules, e.g. the dreaded null proxy, the window itself, or undefined modules // Blacklist any "bad-actor" modules, e.g. the dreaded null proxy, the window itself, or undefined modules
for (const key in window.modules) { for (const key in window.modules) {
@ -34,14 +32,13 @@ const filterModules = (modules: MetroModules, single = false) => (filter: (m: an
if (!modules[id].isInitialized) try { if (!modules[id].isInitialized) try {
__r(id); __r(id);
} catch {}; } catch {}
if (!module) { if (!module) {
blacklist(id); blacklist(id);
continue; continue;
}; }
try {
if (module.default && module.__esModule && filter(module.default)) { if (module.default && module.__esModule && filter(module.default)) {
if (single) return module.default; if (single) return module.default;
found.push(module.default); found.push(module.default);
@ -51,9 +48,6 @@ const filterModules = (modules: MetroModules, single = false) => (filter: (m: an
if (single) return module; if (single) return module;
else found.push(module); else found.push(module);
} }
} catch (e: Error | any) {
console.error(`Failed to filter modules... ${e.stack || e.toString()}`);
}
} }
if (!single) return found; if (!single) return found;

View file

@ -1,9 +1,9 @@
import { PluginManifest, Plugin } from "@types"; import { PluginManifest, Plugin } from "@types";
import { safeFetch } from "@lib/utils";
import { awaitSyncWrapper, createMMKVBackend, createStorage, wrapSync } from "@lib/storage"; import { awaitSyncWrapper, createMMKVBackend, createStorage, wrapSync } from "@lib/storage";
import { MMKVManager } from "@lib/native"; import { MMKVManager } from "@lib/native";
import settings from "@lib/settings";
import logger, { logModule } from "@lib/logger"; import logger, { logModule } from "@lib/logger";
import safeFetch from "@utils/safeFetch"; import settings from "@lib/settings";
type EvaledPlugin = { type EvaledPlugin = {
onLoad?(): void; onLoad?(): void;

View file

@ -1,8 +1,8 @@
import { initThemes } from "@lib/themes";
// Hoist required modules // Hoist required modules
// This used to be in filters.ts, but things became convoluted // This used to be in filters.ts, but things became convoluted
import { initThemes } from "@lib/themes";
// Early find logic // Early find logic
const basicFind = (prop: string) => Object.values(window.modules).find(m => m?.publicModule.exports?.[prop])?.publicModule?.exports; const basicFind = (prop: string) => Object.values(window.modules).find(m => m?.publicModule.exports?.[prop])?.publicModule?.exports;

View file

@ -1,5 +1,5 @@
import { Emitter, StorageBackend } from "@types"; import { Emitter, StorageBackend } from "@types";
import createEmitter from "../emitter"; import createEmitter from "@lib/emitter";
const emitterSymbol = Symbol("emitter accessor"); const emitterSymbol = Symbol("emitter accessor");
const syncAwaitSymbol = Symbol("wrapSync promise accessor"); const syncAwaitSymbol = Symbol("wrapSync promise accessor");

View file

@ -3,7 +3,7 @@ import { ReactNative as RN, chroma } from "@metro/common";
import { findByName, findByProps } from "@metro/filters"; import { findByName, findByProps } from "@metro/filters";
import { instead, after } from "@lib/patcher"; import { instead, after } from "@lib/patcher";
import { createFileBackend, createMMKVBackend, createStorage, wrapSync, awaitSyncWrapper } from "@lib/storage"; import { createFileBackend, createMMKVBackend, createStorage, wrapSync, awaitSyncWrapper } from "@lib/storage";
import { safeFetch } from "@utils"; import { safeFetch } from "@lib/utils";
//! As of 173.10, early-finding this does not work. //! As of 173.10, early-finding this does not work.
// Somehow, this is late enough, though? // Somehow, this is late enough, though?

View file

@ -1,5 +1,5 @@
import { SearchFilter } from "@types"; import { SearchFilter } from "@types";
import findInTree from "@utils/findInTree"; import { findInTree } from "@lib/utils";
export default (tree: { [key: string]: any }, filter: SearchFilter): any => findInTree(tree, filter, { export default (tree: { [key: string]: any }, filter: SearchFilter): any => findInTree(tree, filter, {
walkable: ["props", "children", "child", "sibling"], walkable: ["props", "children", "child", "sibling"],

View file

@ -1,6 +1,7 @@
// Makes mass-importing utils cleaner, chosen over moving utils to one file // Makes mass-importing utils cleaner, chosen over moving utils to one file
export { default as findInReactTree } from "@utils/findInReactTree";
export { default as findInTree } from "@utils/findInTree"; export { default as findInReactTree } from "@lib/utils/findInReactTree";
export { default as safeFetch } from "@utils/safeFetch"; export { default as findInTree } from "@lib/utils/findInTree";
export { default as unfreeze } from "@utils/unfreeze"; export { default as safeFetch } from "@lib/utils/safeFetch";
export { default as without } from "@utils/without"; export { default as unfreeze } from "@lib/utils/unfreeze";
export { default as without } from "@lib/utils/without";

View file

@ -15,7 +15,7 @@ import * as toasts from "@ui/toasts";
import * as alerts from "@ui/alerts"; import * as alerts from "@ui/alerts";
import * as assets from "@ui/assets"; import * as assets from "@ui/assets";
import * as color from "@ui/color"; import * as color from "@ui/color";
import * as utils from "@utils"; import * as utils from "@lib/utils";
export default async (unloads: any[]): Promise<VendettaObject> => ({ export default async (unloads: any[]): Promise<VendettaObject> => ({
patcher: utils.without(patcher, "unpatchAll"), patcher: utils.without(patcher, "unpatchAll"),

View file

@ -1,7 +1,6 @@
import { ErrorBoundaryProps } from "@types"; import { ErrorBoundaryProps } from "@types";
import { React, ReactNative as RN, stylesheet, constants } from "@metro/common"; import { React, ReactNative as RN, stylesheet } from "@metro/common";
import { Forms, Button, Codeblock } from "@ui/components"; import { Forms, Button, Codeblock } from "@ui/components";
import { semanticColors } from "@ui/color";
interface ErrorBoundaryState { interface ErrorBoundaryState {
hasErr: boolean; hasErr: boolean;

View file

@ -1,6 +1,6 @@
import { InputAlertProps } from "@types";
import { findByProps } from "@metro/filters"; import { findByProps } from "@metro/filters";
import { Forms, Alert } from "@ui/components"; import { Forms, Alert } from "@ui/components";
import { InputAlertProps } from "@types";
const { FormInput } = Forms; const { FormInput } = Forms;
const Alerts = findByProps("openLazy", "close"); const Alerts = findByProps("openLazy", "close");

View file

@ -1,26 +1,25 @@
import { SummaryProps } from "@types"; import { SummaryProps } from "@types";
import { Forms } from "@ui/components";
import { getAssetIDByName } from "@ui/assets";
import { ReactNative as RN } from "@metro/common"; import { ReactNative as RN } from "@metro/common";
import { getAssetIDByName } from "@ui/assets";
// TODO: Destructuring Forms doesn't work here. Why? import { Forms } from "@ui/components";
export default function Summary({ label, icon, noPadding = false, noAnimation = false, children }: SummaryProps) { export default function Summary({ label, icon, noPadding = false, noAnimation = false, children }: SummaryProps) {
const { FormRow, FormDivider } = Forms;
const [hidden, setHidden] = React.useState(true); const [hidden, setHidden] = React.useState(true);
return ( return (
<> <>
<Forms.FormRow <FormRow
label={label} label={label}
leading={icon && <Forms.FormRow.Icon source={getAssetIDByName(icon)} />} leading={icon && <FormRow.Icon source={getAssetIDByName(icon)} />}
trailing={<Forms.FormRow.Arrow style={{ transform: [{ rotate: `${hidden ? 180 : 90}deg` }] }} />} trailing={<FormRow.Arrow style={{ transform: [{ rotate: `${hidden ? 180 : 90}deg` }] }} />}
onPress={() => { onPress={() => {
setHidden(!hidden); setHidden(!hidden);
if (!noAnimation) RN.LayoutAnimation.configureNext(RN.LayoutAnimation.Presets.easeInEaseOut); if (!noAnimation) RN.LayoutAnimation.configureNext(RN.LayoutAnimation.Presets.easeInEaseOut);
}} }}
/> />
{!hidden && <> {!hidden && <>
<Forms.FormDivider /> <FormDivider />
<RN.View style={!noPadding && { paddingHorizontal: 15 }}>{children}</RN.View> <RN.View style={!noPadding && { paddingHorizontal: 15 }}>{children}</RN.View>
</>} </>}
</> </>

View file

@ -5,8 +5,8 @@ import { installPlugin } from "@lib/plugins";
import { installTheme } from "@lib/themes"; import { installTheme } from "@lib/themes";
import { findInReactTree } from "@lib/utils"; import { findInReactTree } from "@lib/utils";
import { getAssetIDByName } from "@ui/assets"; import { getAssetIDByName } from "@ui/assets";
import { Forms } from "@ui/components";
import { showToast } from "@ui/toasts"; import { showToast } from "@ui/toasts";
import { Forms } from "@ui/components";
const ForumPostLongPressActionSheet = findByName("ForumPostLongPressActionSheet", false); const ForumPostLongPressActionSheet = findByName("ForumPostLongPressActionSheet", false);
const { FormRow } = Forms; const { FormRow } = Forms;

View file

@ -2,8 +2,8 @@ import { ButtonColors } from "@types";
import { ReactNative as RN, stylesheet } from "@metro/common"; import { ReactNative as RN, stylesheet } from "@metro/common";
import { findByName, findByProps, findByStoreName } from "@metro/filters"; import { findByName, findByProps, findByStoreName } from "@metro/filters";
import { after } from "@lib/patcher"; import { after } from "@lib/patcher";
import { DeviceManager } from "@lib/native";
import { toggleSafeMode } from "@lib/debug"; import { toggleSafeMode } from "@lib/debug";
import { DeviceManager } from "@lib/native";
import { semanticColors } from "@ui/color"; import { semanticColors } from "@ui/color";
import { Button, Codeblock, ErrorBoundary as _ErrorBoundary, SafeAreaView } from "@ui/components"; import { Button, Codeblock, ErrorBoundary as _ErrorBoundary, SafeAreaView } from "@ui/components";
import settings from "@lib/settings"; import settings from "@lib/settings";

View file

@ -4,9 +4,7 @@ import { showToast } from "@ui/toasts";
import { getAssetIDByName } from "@ui/assets"; import { getAssetIDByName } from "@ui/assets";
import { Forms } from "@ui/components"; import { Forms } from "@ui/components";
interface AssetDisplayProps { interface AssetDisplayProps { asset: Asset }
asset: Asset;
}
const { FormRow } = Forms; const { FormRow } = Forms;

View file

@ -1,8 +1,8 @@
import { ReactNative as RN, stylesheet } from "@metro/common"; import { ReactNative as RN, stylesheet } from "@metro/common";
import { findByProps } from "@metro/filters"; import { findByProps } from "@metro/filters";
import { Forms } from "@ui/components";
import { getAssetIDByName } from "@ui/assets"; import { getAssetIDByName } from "@ui/assets";
import { semanticColors } from "@ui/color"; import { semanticColors } from "@ui/color";
import { Forms } from "@ui/components";
const { FormRow, FormSwitch, FormRadio } = Forms; const { FormRow, FormSwitch, FormRadio } = Forms;
const { hideActionSheet } = findByProps("openLazy", "hideActionSheet"); const { hideActionSheet } = findByProps("openLazy", "hideActionSheet");

View file

@ -1,10 +1,10 @@
import { ButtonColors, Plugin } from "@types"; import { ButtonColors, Plugin } from "@types";
import { NavigationNative, clipboard } from "@metro/common"; import { NavigationNative, clipboard } from "@metro/common";
import { removePlugin, startPlugin, stopPlugin, getSettings, fetchPlugin } from "@lib/plugins";
import { MMKVManager } from "@lib/native"; import { MMKVManager } from "@lib/native";
import { getAssetIDByName } from "@ui/assets"; import { getAssetIDByName } from "@ui/assets";
import { showToast } from "@ui/toasts"; import { showToast } from "@ui/toasts";
import { showConfirmationAlert } from "@ui/alerts"; import { showConfirmationAlert } from "@ui/alerts";
import { removePlugin, startPlugin, stopPlugin, getSettings, fetchPlugin } from "@lib/plugins";
import Card, { CardWrapper } from "@ui/settings/components/Card"; import Card, { CardWrapper } from "@ui/settings/components/Card";
async function stopThenStart(plugin: Plugin, callback: Function) { async function stopThenStart(plugin: Plugin, callback: Function) {

View file

@ -1,7 +1,7 @@
import { NavigationNative } from "@metro/common"; import { NavigationNative } from "@metro/common";
import { ErrorBoundary, Forms } from "@ui/components";
import { getAssetIDByName } from "@ui/assets";
import { useProxy } from "@lib/storage"; import { useProxy } from "@lib/storage";
import { getAssetIDByName } from "@ui/assets";
import { ErrorBoundary, Forms } from "@ui/components";
import settings from "@lib/settings"; import settings from "@lib/settings";
const { FormRow, FormSection, FormDivider } = Forms; const { FormRow, FormSection, FormDivider } = Forms;

View file

@ -1,8 +1,8 @@
import { ButtonColors, Theme } from "@types"; import { ButtonColors, Theme } from "@types";
import { clipboard } from "@metro/common"; import { clipboard } from "@metro/common";
import { fetchTheme, removeTheme, selectTheme } from "@lib/themes"; import { fetchTheme, removeTheme, selectTheme } from "@lib/themes";
import { BundleUpdaterManager } from "@lib/native";
import { useProxy } from "@lib/storage"; import { useProxy } from "@lib/storage";
import { BundleUpdaterManager } from "@lib/native";
import { getAssetIDByName } from "@ui/assets"; import { getAssetIDByName } from "@ui/assets";
import { showConfirmationAlert } from "@ui/alerts"; import { showConfirmationAlert } from "@ui/alerts";
import { showToast } from "@ui/toasts"; import { showToast } from "@ui/toasts";

View file

@ -4,8 +4,8 @@ import { after } from "@lib/patcher";
import { installPlugin } from "@lib/plugins"; import { installPlugin } from "@lib/plugins";
import { installTheme } from "@lib/themes"; import { installTheme } from "@lib/themes";
import { Forms } from "@ui/components"; import { Forms } from "@ui/components";
import findInReactTree from "@utils/findInReactTree"; import findInReactTree from "@lib/utils/findInReactTree";
import without from "@utils/without"; import without from "@lib/utils/without";
import ErrorBoundary from "@ui/components/ErrorBoundary"; import ErrorBoundary from "@ui/components/ErrorBoundary";
import SettingsSection from "@ui/settings/components/SettingsSection"; import SettingsSection from "@ui/settings/components/SettingsSection";
import InstallButton from "@ui/settings/components/InstallButton"; import InstallButton from "@ui/settings/components/InstallButton";

View file

@ -1,7 +1,6 @@
import { ReactNative as RN } from "@metro/common"; import { ReactNative as RN } from "@metro/common";
import { all } from "@ui/assets"; import { all } from "@ui/assets";
import { Forms, Search } from "@ui/components"; import { Forms, Search, ErrorBoundary } from "@ui/components";
import ErrorBoundary from "@ui/components/ErrorBoundary";
import AssetDisplay from "@ui/settings/components/AssetDisplay"; import AssetDisplay from "@ui/settings/components/AssetDisplay";
const { FormDivider } = Forms; const { FormDivider } = Forms;

View file

@ -1,11 +1,10 @@
import { ReactNative as RN, NavigationNative } from "@metro/common"; import { ReactNative as RN, NavigationNative } from "@metro/common";
import { findByProps } from "@metro/filters"; import { findByProps } from "@metro/filters";
import { Forms } from "@ui/components";
import { getAssetIDByName } from "@ui/assets";
import { connectToDebugger } from "@lib/debug"; import { connectToDebugger } from "@lib/debug";
import { useProxy } from "@lib/storage"; import { useProxy } from "@lib/storage";
import { getAssetIDByName } from "@ui/assets";
import { Forms, ErrorBoundary } from "@ui/components";
import settings, { loaderConfig } from "@lib/settings"; import settings, { loaderConfig } from "@lib/settings";
import ErrorBoundary from "@ui/components/ErrorBoundary";
const { FormSection, FormRow, FormSwitchRow, FormInput, FormDivider } = Forms; const { FormSection, FormRow, FormSwitchRow, FormInput, FormDivider } = Forms;
const { hideActionSheet } = findByProps("openLazy", "hideActionSheet"); const { hideActionSheet } = findByProps("openLazy", "hideActionSheet");

View file

@ -1,13 +1,12 @@
import { ReactNative as RN, url } from "@metro/common"; import { ReactNative as RN, url } from "@metro/common";
import { getAssetIDByName } from "@ui/assets";
import { Forms, Summary } from "@ui/components";
import { DISCORD_SERVER, GITHUB } from "@lib/constants"; import { DISCORD_SERVER, GITHUB } from "@lib/constants";
import { getDebugInfo, toggleSafeMode } from "@lib/debug"; import { getDebugInfo, toggleSafeMode } from "@lib/debug";
import { useProxy } from "@lib/storage"; import { useProxy } from "@lib/storage";
import { BundleUpdaterManager } from "@lib/native"; import { BundleUpdaterManager } from "@lib/native";
import { getAssetIDByName } from "@ui/assets";
import { Forms, Summary, ErrorBoundary } from "@ui/components";
import settings from "@lib/settings"; import settings from "@lib/settings";
import Version from "@ui/settings/components/Version"; import Version from "@ui/settings/components/Version";
import ErrorBoundary from "@ui/components/ErrorBoundary";
const { FormRow, FormSwitchRow, FormSection, FormDivider } = Forms; const { FormRow, FormSwitchRow, FormSection, FormDivider } = Forms;
const debugInfo = getDebugInfo(); const debugInfo = getDebugInfo();

View file

@ -1,6 +1,6 @@
import { toasts } from "@metro/common"; import { toasts } from "@metro/common";
export const showToast = (content: string, asset: number) => toasts.open({ export const showToast = (content: string, asset?: number) => toasts.open({
content: content, content: content,
source: asset, source: asset,
}); });

View file

@ -16,8 +16,6 @@
"@/*": ["src/*"], "@/*": ["src/*"],
"@types": ["src/def.d.ts"], "@types": ["src/def.d.ts"],
"@lib/*": ["src/lib/*"], "@lib/*": ["src/lib/*"],
"@utils": ["src/lib/utils/index.ts"],
"@utils/*": ["src/lib/utils/*"],
"@metro/*": ["src/lib/metro/*"], "@metro/*": ["src/lib/metro/*"],
"@ui/*": ["src/ui/*"] "@ui/*": ["src/ui/*"]
} }