From 85d0bd263bfa4a442ba374cc4fdb0591dc934c80 Mon Sep 17 00:00:00 2001 From: Beef Date: Sat, 15 Apr 2023 19:54:23 +0100 Subject: [PATCH] [Global] Code cleanup pass --- package.json | 12 +++---- src/def.d.ts | 2 +- src/index.ts | 2 +- src/lib/commands.ts | 2 +- src/lib/debug.ts | 2 +- src/lib/fixes.ts | 3 +- src/lib/metro/filters.ts | 36 ++++++++----------- src/lib/plugins.ts | 4 +-- src/lib/preinit.ts | 4 +-- src/lib/storage/index.ts | 2 +- src/lib/themes.ts | 2 +- src/lib/utils/findInReactTree.ts | 2 +- src/lib/utils/index.ts | 11 +++--- src/lib/windowObject.ts | 2 +- src/ui/components/ErrorBoundary.tsx | 3 +- src/ui/components/InputAlert.tsx | 2 +- src/ui/components/Summary.tsx | 15 ++++---- src/ui/quickInstall/forumPost.tsx | 2 +- src/ui/safeMode.tsx | 2 +- src/ui/settings/components/AssetDisplay.tsx | 4 +-- src/ui/settings/components/Card.tsx | 2 +- src/ui/settings/components/PluginCard.tsx | 2 +- .../settings/components/SettingsSection.tsx | 4 +-- src/ui/settings/components/ThemeCard.tsx | 2 +- src/ui/settings/index.tsx | 4 +-- src/ui/settings/pages/AssetBrowser.tsx | 3 +- src/ui/settings/pages/Developer.tsx | 5 ++- src/ui/settings/pages/General.tsx | 5 ++- src/ui/toasts.ts | 2 +- tsconfig.json | 2 -- 30 files changed, 65 insertions(+), 80 deletions(-) diff --git a/package.json b/package.json index 97a2c8b..a07bd6d 100644 --- a/package.json +++ b/package.json @@ -15,18 +15,18 @@ "license": "BSD-3-Clause", "devDependencies": { "@react-native-clipboard/clipboard": "1.10.0", - "@swc/core": "^1.3.35", + "@swc/core": "^1.3.50", "@types/chroma-js": "^2.4.0", - "@types/lodash": "^4.14.191", - "@types/react": "18.0.27", + "@types/lodash": "^4.14.194", + "@types/react": "18.0.35", "@types/react-native": "0.70.6", - "esbuild": "^0.17.11", + "esbuild": "^0.17.16", "esbuild-plugin-alias": "^0.2.1", "moment": "2.22.2", - "typescript": "^4.9.5" + "typescript": "^5.0.4" }, "dependencies": { - "@swc/helpers": "^0.4.14", + "@swc/helpers": "^0.5.0", "spitroast": "^1.4.2" }, "pnpm": { diff --git a/src/def.d.ts b/src/def.d.ts index f4f8d75..6b1bd0e 100644 --- a/src/def.d.ts +++ b/src/def.d.ts @@ -436,7 +436,7 @@ interface VendettaObject { Search: _React.ComponentType; } toasts: { - showToast: (content: string, asset: number) => void; + showToast: (content: string, asset?: number) => void; }; alerts: { showConfirmationAlert: (options: ConfirmationAlertOptions) => void; diff --git a/src/index.ts b/src/index.ts index 48e4304..d32c177 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,8 +1,8 @@ import { patchLogHook } from "@lib/debug"; import { patchCommands } from "@lib/commands"; import { initPlugins } from "@lib/plugins"; -import { patchAssets } from "@ui/assets"; import { patchChatBackground } from "@lib/themes"; +import { patchAssets } from "@ui/assets"; import initQuickInstall from "@ui/quickInstall"; import initSafeMode from "@ui/safeMode"; import initSettings from "@ui/settings"; diff --git a/src/lib/commands.ts b/src/lib/commands.ts index 694fb64..1fdcbfd 100644 --- a/src/lib/commands.ts +++ b/src/lib/commands.ts @@ -5,7 +5,7 @@ import { after } from "@lib/patcher"; let commands: ApplicationCommand[] = []; export function patchCommands() { - const unpatch = after("getBuiltInCommands", commandsModule, (args, res) => res.concat(commands)); + const unpatch = after("getBuiltInCommands", commandsModule, (_, res) => res.concat(commands)); return () => { commands = []; unpatch(); diff --git a/src/lib/debug.ts b/src/lib/debug.ts index ce83a37..c473185 100644 --- a/src/lib/debug.ts +++ b/src/lib/debug.ts @@ -1,8 +1,8 @@ import { RNConstants } from "@types"; import { ReactNative as RN } from "@metro/common"; import { after } from "@lib/patcher"; -import { ClientInfoManager, DeviceManager, BundleUpdaterManager } from "@lib/native"; import { getCurrentTheme, selectTheme } from "@lib/themes"; +import { ClientInfoManager, DeviceManager, BundleUpdaterManager } from "@lib/native"; import { getAssetIDByName } from "@ui/assets"; import { showToast } from "@ui/toasts"; import settings from "@lib/settings"; diff --git a/src/lib/fixes.ts b/src/lib/fixes.ts index c8971a5..6c4397f 100644 --- a/src/lib/fixes.ts +++ b/src/lib/fixes.ts @@ -11,8 +11,7 @@ function onDispatch({ locale }: { locale: string }) { // Theming // Based on https://github.com/Aliucord/AliucordRN/blob/main/src/ui/patchTheme.ts try { - const theme = ThemeStore.theme || "dark"; - ThemeManager.overrideTheme(theme); + ThemeManager.overrideTheme(ThemeStore?.theme ?? "dark"); if (AMOLEDThemeManager && UnsyncedUserSettingsStore.useAMOLEDTheme === 2) AMOLEDThemeManager.setAMOLEDThemeEnabled(true); } catch(e) { logger.error("Failed to fix theme...", e); diff --git a/src/lib/metro/filters.ts b/src/lib/metro/filters.ts index 59df4d1..ee19801 100644 --- a/src/lib/metro/filters.ts +++ b/src/lib/metro/filters.ts @@ -4,14 +4,12 @@ import { MetroModules, PropsFinder, PropsFinderAll } from "@types"; declare const __r: (moduleId: number) => any; // Function to blacklist a module, preventing it from being searched again -function blacklist(id: number) { - Object.defineProperty(window.modules, id, { - value: window.modules[id], - enumerable: false, - configurable: true, - writable: true - }) -} +const blacklist = (id: number) => Object.defineProperty(window.modules, id, { + value: window.modules[id], + enumerable: false, + configurable: true, + writable: true +}); // Blacklist any "bad-actor" modules, e.g. the dreaded null proxy, the window itself, or undefined modules for (const key in window.modules) { @@ -34,25 +32,21 @@ const filterModules = (modules: MetroModules, single = false) => (filter: (m: an if (!modules[id].isInitialized) try { __r(id); - } catch {}; + } catch {} if (!module) { blacklist(id); continue; - }; + } - try { - if (module.default && module.__esModule && filter(module.default)) { - if (single) return module.default; - found.push(module.default); - } + if (module.default && module.__esModule && filter(module.default)) { + if (single) return module.default; + found.push(module.default); + } - if (filter(module)) { - if (single) return module; - else found.push(module); - } - } catch (e: Error | any) { - console.error(`Failed to filter modules... ${e.stack || e.toString()}`); + if (filter(module)) { + if (single) return module; + else found.push(module); } } diff --git a/src/lib/plugins.ts b/src/lib/plugins.ts index 12a7f09..3749e55 100644 --- a/src/lib/plugins.ts +++ b/src/lib/plugins.ts @@ -1,9 +1,9 @@ import { PluginManifest, Plugin } from "@types"; +import { safeFetch } from "@lib/utils"; import { awaitSyncWrapper, createMMKVBackend, createStorage, wrapSync } from "@lib/storage"; import { MMKVManager } from "@lib/native"; -import settings from "@lib/settings"; import logger, { logModule } from "@lib/logger"; -import safeFetch from "@utils/safeFetch"; +import settings from "@lib/settings"; type EvaledPlugin = { onLoad?(): void; diff --git a/src/lib/preinit.ts b/src/lib/preinit.ts index b05a109..140ac25 100644 --- a/src/lib/preinit.ts +++ b/src/lib/preinit.ts @@ -1,8 +1,8 @@ +import { initThemes } from "@lib/themes"; + // Hoist required modules // This used to be in filters.ts, but things became convoluted -import { initThemes } from "@lib/themes"; - // Early find logic const basicFind = (prop: string) => Object.values(window.modules).find(m => m?.publicModule.exports?.[prop])?.publicModule?.exports; diff --git a/src/lib/storage/index.ts b/src/lib/storage/index.ts index 7e4601a..92bd8df 100644 --- a/src/lib/storage/index.ts +++ b/src/lib/storage/index.ts @@ -1,5 +1,5 @@ import { Emitter, StorageBackend } from "@types"; -import createEmitter from "../emitter"; +import createEmitter from "@lib/emitter"; const emitterSymbol = Symbol("emitter accessor"); const syncAwaitSymbol = Symbol("wrapSync promise accessor"); diff --git a/src/lib/themes.ts b/src/lib/themes.ts index e588e5d..daa679d 100644 --- a/src/lib/themes.ts +++ b/src/lib/themes.ts @@ -3,7 +3,7 @@ import { ReactNative as RN, chroma } from "@metro/common"; import { findByName, findByProps } from "@metro/filters"; import { instead, after } from "@lib/patcher"; 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. // Somehow, this is late enough, though? diff --git a/src/lib/utils/findInReactTree.ts b/src/lib/utils/findInReactTree.ts index 027a532..002f3e3 100644 --- a/src/lib/utils/findInReactTree.ts +++ b/src/lib/utils/findInReactTree.ts @@ -1,5 +1,5 @@ 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, { walkable: ["props", "children", "child", "sibling"], diff --git a/src/lib/utils/index.ts b/src/lib/utils/index.ts index 68502cf..8a3c295 100644 --- a/src/lib/utils/index.ts +++ b/src/lib/utils/index.ts @@ -1,6 +1,7 @@ // 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 safeFetch } from "@utils/safeFetch"; -export { default as unfreeze } from "@utils/unfreeze"; -export { default as without } from "@utils/without"; \ No newline at end of file + +export { default as findInReactTree } from "@lib/utils/findInReactTree"; +export { default as findInTree } from "@lib/utils/findInTree"; +export { default as safeFetch } from "@lib/utils/safeFetch"; +export { default as unfreeze } from "@lib/utils/unfreeze"; +export { default as without } from "@lib/utils/without"; \ No newline at end of file diff --git a/src/lib/windowObject.ts b/src/lib/windowObject.ts index 9a8245e..cb780d5 100644 --- a/src/lib/windowObject.ts +++ b/src/lib/windowObject.ts @@ -15,7 +15,7 @@ import * as toasts from "@ui/toasts"; import * as alerts from "@ui/alerts"; import * as assets from "@ui/assets"; import * as color from "@ui/color"; -import * as utils from "@utils"; +import * as utils from "@lib/utils"; export default async (unloads: any[]): Promise => ({ patcher: utils.without(patcher, "unpatchAll"), diff --git a/src/ui/components/ErrorBoundary.tsx b/src/ui/components/ErrorBoundary.tsx index 63de2b8..12b068d 100644 --- a/src/ui/components/ErrorBoundary.tsx +++ b/src/ui/components/ErrorBoundary.tsx @@ -1,7 +1,6 @@ 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 { semanticColors } from "@ui/color"; interface ErrorBoundaryState { hasErr: boolean; diff --git a/src/ui/components/InputAlert.tsx b/src/ui/components/InputAlert.tsx index 3bc7f6e..98f50f7 100644 --- a/src/ui/components/InputAlert.tsx +++ b/src/ui/components/InputAlert.tsx @@ -1,6 +1,6 @@ +import { InputAlertProps } from "@types"; import { findByProps } from "@metro/filters"; import { Forms, Alert } from "@ui/components"; -import { InputAlertProps } from "@types"; const { FormInput } = Forms; const Alerts = findByProps("openLazy", "close"); diff --git a/src/ui/components/Summary.tsx b/src/ui/components/Summary.tsx index 55c38a7..60d3507 100644 --- a/src/ui/components/Summary.tsx +++ b/src/ui/components/Summary.tsx @@ -1,26 +1,25 @@ import { SummaryProps } from "@types"; -import { Forms } from "@ui/components"; -import { getAssetIDByName } from "@ui/assets"; import { ReactNative as RN } from "@metro/common"; - -// TODO: Destructuring Forms doesn't work here. Why? +import { getAssetIDByName } from "@ui/assets"; +import { Forms } from "@ui/components"; export default function Summary({ label, icon, noPadding = false, noAnimation = false, children }: SummaryProps) { + const { FormRow, FormDivider } = Forms; const [hidden, setHidden] = React.useState(true); return ( <> - } - trailing={} + leading={icon && } + trailing={} onPress={() => { setHidden(!hidden); if (!noAnimation) RN.LayoutAnimation.configureNext(RN.LayoutAnimation.Presets.easeInEaseOut); }} /> {!hidden && <> - + {children} } diff --git a/src/ui/quickInstall/forumPost.tsx b/src/ui/quickInstall/forumPost.tsx index 7d41064..468321b 100644 --- a/src/ui/quickInstall/forumPost.tsx +++ b/src/ui/quickInstall/forumPost.tsx @@ -5,8 +5,8 @@ import { installPlugin } from "@lib/plugins"; import { installTheme } from "@lib/themes"; import { findInReactTree } from "@lib/utils"; import { getAssetIDByName } from "@ui/assets"; -import { Forms } from "@ui/components"; import { showToast } from "@ui/toasts"; +import { Forms } from "@ui/components"; const ForumPostLongPressActionSheet = findByName("ForumPostLongPressActionSheet", false); const { FormRow } = Forms; diff --git a/src/ui/safeMode.tsx b/src/ui/safeMode.tsx index 68fe368..c3f7806 100644 --- a/src/ui/safeMode.tsx +++ b/src/ui/safeMode.tsx @@ -2,8 +2,8 @@ import { ButtonColors } from "@types"; import { ReactNative as RN, stylesheet } from "@metro/common"; import { findByName, findByProps, findByStoreName } from "@metro/filters"; import { after } from "@lib/patcher"; -import { DeviceManager } from "@lib/native"; import { toggleSafeMode } from "@lib/debug"; +import { DeviceManager } from "@lib/native"; import { semanticColors } from "@ui/color"; import { Button, Codeblock, ErrorBoundary as _ErrorBoundary, SafeAreaView } from "@ui/components"; import settings from "@lib/settings"; diff --git a/src/ui/settings/components/AssetDisplay.tsx b/src/ui/settings/components/AssetDisplay.tsx index 242d737..581dd2f 100644 --- a/src/ui/settings/components/AssetDisplay.tsx +++ b/src/ui/settings/components/AssetDisplay.tsx @@ -4,9 +4,7 @@ import { showToast } from "@ui/toasts"; import { getAssetIDByName } from "@ui/assets"; import { Forms } from "@ui/components"; -interface AssetDisplayProps { - asset: Asset; -} +interface AssetDisplayProps { asset: Asset } const { FormRow } = Forms; diff --git a/src/ui/settings/components/Card.tsx b/src/ui/settings/components/Card.tsx index 5b8bdc4..9dc54a3 100644 --- a/src/ui/settings/components/Card.tsx +++ b/src/ui/settings/components/Card.tsx @@ -1,8 +1,8 @@ import { ReactNative as RN, stylesheet } from "@metro/common"; import { findByProps } from "@metro/filters"; -import { Forms } from "@ui/components"; import { getAssetIDByName } from "@ui/assets"; import { semanticColors } from "@ui/color"; +import { Forms } from "@ui/components"; const { FormRow, FormSwitch, FormRadio } = Forms; const { hideActionSheet } = findByProps("openLazy", "hideActionSheet"); diff --git a/src/ui/settings/components/PluginCard.tsx b/src/ui/settings/components/PluginCard.tsx index 85b91b1..e4ffdec 100644 --- a/src/ui/settings/components/PluginCard.tsx +++ b/src/ui/settings/components/PluginCard.tsx @@ -1,10 +1,10 @@ import { ButtonColors, Plugin } from "@types"; import { NavigationNative, clipboard } from "@metro/common"; +import { removePlugin, startPlugin, stopPlugin, getSettings, fetchPlugin } from "@lib/plugins"; import { MMKVManager } from "@lib/native"; import { getAssetIDByName } from "@ui/assets"; import { showToast } from "@ui/toasts"; import { showConfirmationAlert } from "@ui/alerts"; -import { removePlugin, startPlugin, stopPlugin, getSettings, fetchPlugin } from "@lib/plugins"; import Card, { CardWrapper } from "@ui/settings/components/Card"; async function stopThenStart(plugin: Plugin, callback: Function) { diff --git a/src/ui/settings/components/SettingsSection.tsx b/src/ui/settings/components/SettingsSection.tsx index 842e271..58325e0 100644 --- a/src/ui/settings/components/SettingsSection.tsx +++ b/src/ui/settings/components/SettingsSection.tsx @@ -1,7 +1,7 @@ import { NavigationNative } from "@metro/common"; -import { ErrorBoundary, Forms } from "@ui/components"; -import { getAssetIDByName } from "@ui/assets"; import { useProxy } from "@lib/storage"; +import { getAssetIDByName } from "@ui/assets"; +import { ErrorBoundary, Forms } from "@ui/components"; import settings from "@lib/settings"; const { FormRow, FormSection, FormDivider } = Forms; diff --git a/src/ui/settings/components/ThemeCard.tsx b/src/ui/settings/components/ThemeCard.tsx index 8747c50..f90763a 100644 --- a/src/ui/settings/components/ThemeCard.tsx +++ b/src/ui/settings/components/ThemeCard.tsx @@ -1,8 +1,8 @@ import { ButtonColors, Theme } from "@types"; import { clipboard } from "@metro/common"; import { fetchTheme, removeTheme, selectTheme } from "@lib/themes"; -import { BundleUpdaterManager } from "@lib/native"; import { useProxy } from "@lib/storage"; +import { BundleUpdaterManager } from "@lib/native"; import { getAssetIDByName } from "@ui/assets"; import { showConfirmationAlert } from "@ui/alerts"; import { showToast } from "@ui/toasts"; diff --git a/src/ui/settings/index.tsx b/src/ui/settings/index.tsx index 979cd2e..bc6b842 100644 --- a/src/ui/settings/index.tsx +++ b/src/ui/settings/index.tsx @@ -4,8 +4,8 @@ import { after } from "@lib/patcher"; import { installPlugin } from "@lib/plugins"; import { installTheme } from "@lib/themes"; import { Forms } from "@ui/components"; -import findInReactTree from "@utils/findInReactTree"; -import without from "@utils/without"; +import findInReactTree from "@lib/utils/findInReactTree"; +import without from "@lib/utils/without"; import ErrorBoundary from "@ui/components/ErrorBoundary"; import SettingsSection from "@ui/settings/components/SettingsSection"; import InstallButton from "@ui/settings/components/InstallButton"; diff --git a/src/ui/settings/pages/AssetBrowser.tsx b/src/ui/settings/pages/AssetBrowser.tsx index e1ab53b..139c769 100644 --- a/src/ui/settings/pages/AssetBrowser.tsx +++ b/src/ui/settings/pages/AssetBrowser.tsx @@ -1,7 +1,6 @@ import { ReactNative as RN } from "@metro/common"; import { all } from "@ui/assets"; -import { Forms, Search } from "@ui/components"; -import ErrorBoundary from "@ui/components/ErrorBoundary"; +import { Forms, Search, ErrorBoundary } from "@ui/components"; import AssetDisplay from "@ui/settings/components/AssetDisplay"; const { FormDivider } = Forms; diff --git a/src/ui/settings/pages/Developer.tsx b/src/ui/settings/pages/Developer.tsx index 80057a0..5b87b9d 100644 --- a/src/ui/settings/pages/Developer.tsx +++ b/src/ui/settings/pages/Developer.tsx @@ -1,11 +1,10 @@ import { ReactNative as RN, NavigationNative } from "@metro/common"; import { findByProps } from "@metro/filters"; -import { Forms } from "@ui/components"; -import { getAssetIDByName } from "@ui/assets"; import { connectToDebugger } from "@lib/debug"; import { useProxy } from "@lib/storage"; +import { getAssetIDByName } from "@ui/assets"; +import { Forms, ErrorBoundary } from "@ui/components"; import settings, { loaderConfig } from "@lib/settings"; -import ErrorBoundary from "@ui/components/ErrorBoundary"; const { FormSection, FormRow, FormSwitchRow, FormInput, FormDivider } = Forms; const { hideActionSheet } = findByProps("openLazy", "hideActionSheet"); diff --git a/src/ui/settings/pages/General.tsx b/src/ui/settings/pages/General.tsx index 76b5d65..1b94a30 100644 --- a/src/ui/settings/pages/General.tsx +++ b/src/ui/settings/pages/General.tsx @@ -1,13 +1,12 @@ 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 { getDebugInfo, toggleSafeMode } from "@lib/debug"; import { useProxy } from "@lib/storage"; import { BundleUpdaterManager } from "@lib/native"; +import { getAssetIDByName } from "@ui/assets"; +import { Forms, Summary, ErrorBoundary } from "@ui/components"; import settings from "@lib/settings"; import Version from "@ui/settings/components/Version"; -import ErrorBoundary from "@ui/components/ErrorBoundary"; const { FormRow, FormSwitchRow, FormSection, FormDivider } = Forms; const debugInfo = getDebugInfo(); diff --git a/src/ui/toasts.ts b/src/ui/toasts.ts index 7c8671c..c39a0a0 100644 --- a/src/ui/toasts.ts +++ b/src/ui/toasts.ts @@ -1,6 +1,6 @@ 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, source: asset, }); \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index 6ef2ba7..9410367 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -16,8 +16,6 @@ "@/*": ["src/*"], "@types": ["src/def.d.ts"], "@lib/*": ["src/lib/*"], - "@utils": ["src/lib/utils/index.ts"], - "@utils/*": ["src/lib/utils/*"], "@metro/*": ["src/lib/metro/*"], "@ui/*": ["src/ui/*"] }