From f7d9be91407582fd85a70eef89c03a32e37c3c0f Mon Sep 17 00:00:00 2001 From: Vendicated Date: Mon, 7 Nov 2022 23:34:14 +0100 Subject: lint: Disallow utils index imports This keeps leading to issues due to circular imports. Import from specific files instead, index just reexports --- src/plugins/experiments.tsx | 2 +- src/plugins/ignoreActivities.ts | 2 +- src/plugins/messageActions.ts | 2 +- src/plugins/nitroBypass.ts | 2 +- src/plugins/petpet.ts | 2 +- .../pronoundb/components/PronounsChatComponent.tsx | 2 +- .../components/PronounsProfileWrapper.tsx | 4 +- src/plugins/pronoundb/pronoundbUtils.ts | 94 ++++++++++++++++++++++ src/plugins/pronoundb/utils.ts | 94 ---------------------- src/plugins/sendify.ts | 2 +- src/plugins/spotifyControls/PlayerComponent.tsx | 3 +- src/plugins/spotifyControls/SpotifyStore.ts | 4 +- src/plugins/startupTimings/StartupTimingPage.tsx | 4 +- src/plugins/startupTimings/index.tsx | 2 +- 14 files changed, 111 insertions(+), 108 deletions(-) create mode 100644 src/plugins/pronoundb/pronoundbUtils.ts delete mode 100644 src/plugins/pronoundb/utils.ts (limited to 'src/plugins') diff --git a/src/plugins/experiments.tsx b/src/plugins/experiments.tsx index b9df6f9..861bb05 100644 --- a/src/plugins/experiments.tsx +++ b/src/plugins/experiments.tsx @@ -16,8 +16,8 @@ * along with this program. If not, see . */ -import { lazyWebpack } from "../utils"; import { Devs } from "../utils/constants"; +import { lazyWebpack } from "../utils/misc"; import definePlugin, { OptionType } from "../utils/types"; import { Settings } from "../Vencord"; import { filters } from "../webpack"; diff --git a/src/plugins/ignoreActivities.ts b/src/plugins/ignoreActivities.ts index 5c1ddcc..2084277 100644 --- a/src/plugins/ignoreActivities.ts +++ b/src/plugins/ignoreActivities.ts @@ -17,8 +17,8 @@ */ import { DataStore } from "../api"; -import { lazyWebpack } from "../utils"; import { Devs } from "../utils/constants"; +import { lazyWebpack } from "../utils/misc"; import definePlugin from "../utils/types"; import { filters } from "../webpack"; diff --git a/src/plugins/messageActions.ts b/src/plugins/messageActions.ts index 98c920e..bb2ad17 100644 --- a/src/plugins/messageActions.ts +++ b/src/plugins/messageActions.ts @@ -17,8 +17,8 @@ */ import { addClickListener, removeClickListener } from "../api/MessageEvents"; -import { lazyWebpack } from "../utils"; import { Devs } from "../utils/constants"; +import { lazyWebpack } from "../utils/misc"; import definePlugin from "../utils/types"; import { filters } from "../webpack"; import { UserStore } from "../webpack/common"; diff --git a/src/plugins/nitroBypass.ts b/src/plugins/nitroBypass.ts index f4bdc84..d2e1e10 100644 --- a/src/plugins/nitroBypass.ts +++ b/src/plugins/nitroBypass.ts @@ -17,9 +17,9 @@ */ import { addPreEditListener, addPreSendListener, removePreEditListener, removePreSendListener } from "../api/MessageEvents"; -import { lazyWebpack } from "../utils"; import { Devs } from "../utils/constants"; import { ApngDisposeOp, getGifEncoder, importApngJs } from "../utils/dependencies"; +import { lazyWebpack } from "../utils/misc"; import definePlugin, { OptionType } from "../utils/types"; import { Settings } from "../Vencord"; import { filters } from "../webpack"; diff --git a/src/plugins/petpet.ts b/src/plugins/petpet.ts index 961d47a..59b4e56 100644 --- a/src/plugins/petpet.ts +++ b/src/plugins/petpet.ts @@ -17,9 +17,9 @@ */ import { ApplicationCommandInputType, ApplicationCommandOptionType, Argument, CommandContext, findOption } from "../api/Commands"; -import { lazyWebpack, makeLazy } from "../utils"; import { Devs } from "../utils/constants"; import { getGifEncoder } from "../utils/dependencies"; +import { lazyWebpack, makeLazy } from "../utils/misc"; import definePlugin from "../utils/types"; import { filters } from "../webpack"; diff --git a/src/plugins/pronoundb/components/PronounsChatComponent.tsx b/src/plugins/pronoundb/components/PronounsChatComponent.tsx index 2d20461..78cee48 100644 --- a/src/plugins/pronoundb/components/PronounsChatComponent.tsx +++ b/src/plugins/pronoundb/components/PronounsChatComponent.tsx @@ -22,8 +22,8 @@ import { classes, lazyWebpack, useAwaiter } from "../../../utils/misc"; import { Settings } from "../../../Vencord"; import { filters } from "../../../webpack"; import { UserStore } from "../../../webpack/common"; +import { fetchPronouns, formatPronouns } from "../pronoundbUtils"; import { PronounMapping } from "../types"; -import { fetchPronouns, formatPronouns } from "../utils"; const styles: Record = lazyWebpack(filters.byProps("timestampInline")); diff --git a/src/plugins/pronoundb/components/PronounsProfileWrapper.tsx b/src/plugins/pronoundb/components/PronounsProfileWrapper.tsx index 3f0022e..4d12dd5 100644 --- a/src/plugins/pronoundb/components/PronounsProfileWrapper.tsx +++ b/src/plugins/pronoundb/components/PronounsProfileWrapper.tsx @@ -16,11 +16,11 @@ * along with this program. If not, see . */ -import { useAwaiter } from "../../../utils"; +import { useAwaiter } from "../../../utils/misc"; import { Settings } from "../../../Vencord"; import { UserStore } from "../../../webpack/common"; +import { fetchPronouns, formatPronouns } from "../pronoundbUtils"; import { PronounMapping, UserProfileProps } from "../types"; -import { fetchPronouns, formatPronouns } from "../utils"; export default function PronounsProfileWrapper(props: UserProfileProps, pronounsComponent: JSX.Element) { // Don't bother fetching bot or system users diff --git a/src/plugins/pronoundb/pronoundbUtils.ts b/src/plugins/pronoundb/pronoundbUtils.ts new file mode 100644 index 0000000..73ec7b6 --- /dev/null +++ b/src/plugins/pronoundb/pronoundbUtils.ts @@ -0,0 +1,94 @@ +/* + * Vencord, a modification for Discord's desktop app + * Copyright (c) 2022 Vendicated and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . +*/ + +import { VENCORD_USER_AGENT } from "../../utils/constants"; +import { debounce } from "../../utils/debounce"; +import { Settings } from "../../Vencord"; +import { PronounsFormat } from "."; +import { PronounCode, PronounMapping, PronounsResponse } from "./types"; + +// A map of cached pronouns so the same request isn't sent twice +const cache: Record = {}; +// A map of ids and callbacks that should be triggered on fetch +const requestQueue: Record void)[]> = {}; + +// Executes all queued requests and calls their callbacks +const bulkFetch = debounce(async () => { + const ids = Object.keys(requestQueue); + const pronouns = await bulkFetchPronouns(ids); + for (const id of ids) { + // Call all callbacks for the id + requestQueue[id].forEach(c => c(pronouns[id])); + delete requestQueue[id]; + } +}); + +// Fetches the pronouns for one id, returning a promise that resolves if it was cached, or once the request is completed +export function fetchPronouns(id: string): Promise { + return new Promise(res => { + // If cached, return the cached pronouns + if (id in cache) res(cache[id]); + // If there is already a request added, then just add this callback to it + else if (id in requestQueue) requestQueue[id].push(res); + // If not already added, then add it and call the debounced function to make sure the request gets executed + else { + requestQueue[id] = [res]; + bulkFetch(); + } + }); +} + +async function bulkFetchPronouns(ids: string[]): Promise { + const params = new URLSearchParams(); + params.append("platform", "discord"); + params.append("ids", ids.join(",")); + + try { + const req = await fetch("https://pronoundb.org/api/v1/lookup-bulk?" + params.toString(), { + method: "GET", + headers: { + "Accept": "application/json", + "X-PronounDB-Source": VENCORD_USER_AGENT + } + }); + return await req.json() + .then((res: PronounsResponse) => { + Object.assign(cache, res); + return res; + }); + } catch (e) { + // If the request errors, treat it as if no pronouns were found for all ids, and log it + console.error("PronounDB fetching failed: ", e); + const dummyPronouns = Object.fromEntries(ids.map(id => [id, "unspecified"] as const)); + Object.assign(cache, dummyPronouns); + return dummyPronouns; + } +} + +export function formatPronouns(pronouns: PronounCode): string { + const { pronounsFormat } = Settings.plugins.PronounDB as { pronounsFormat: PronounsFormat, enabled: boolean; }; + // For capitalized pronouns, just return the mapping (it is by default capitalized) + if (pronounsFormat === PronounsFormat.Capitalized) return PronounMapping[pronouns]; + // If it is set to lowercase and a special code (any, ask, avoid), then just return the capitalized text + else if ( + pronounsFormat === PronounsFormat.Lowercase + && ["any", "ask", "avoid", "other"].includes(pronouns) + ) return PronounMapping[pronouns]; + // Otherwise (lowercase and not a special code), then convert the mapping to lowercase + else return PronounMapping[pronouns].toLowerCase(); +} diff --git a/src/plugins/pronoundb/utils.ts b/src/plugins/pronoundb/utils.ts deleted file mode 100644 index 73ec7b6..0000000 --- a/src/plugins/pronoundb/utils.ts +++ /dev/null @@ -1,94 +0,0 @@ -/* - * Vencord, a modification for Discord's desktop app - * Copyright (c) 2022 Vendicated and contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . -*/ - -import { VENCORD_USER_AGENT } from "../../utils/constants"; -import { debounce } from "../../utils/debounce"; -import { Settings } from "../../Vencord"; -import { PronounsFormat } from "."; -import { PronounCode, PronounMapping, PronounsResponse } from "./types"; - -// A map of cached pronouns so the same request isn't sent twice -const cache: Record = {}; -// A map of ids and callbacks that should be triggered on fetch -const requestQueue: Record void)[]> = {}; - -// Executes all queued requests and calls their callbacks -const bulkFetch = debounce(async () => { - const ids = Object.keys(requestQueue); - const pronouns = await bulkFetchPronouns(ids); - for (const id of ids) { - // Call all callbacks for the id - requestQueue[id].forEach(c => c(pronouns[id])); - delete requestQueue[id]; - } -}); - -// Fetches the pronouns for one id, returning a promise that resolves if it was cached, or once the request is completed -export function fetchPronouns(id: string): Promise { - return new Promise(res => { - // If cached, return the cached pronouns - if (id in cache) res(cache[id]); - // If there is already a request added, then just add this callback to it - else if (id in requestQueue) requestQueue[id].push(res); - // If not already added, then add it and call the debounced function to make sure the request gets executed - else { - requestQueue[id] = [res]; - bulkFetch(); - } - }); -} - -async function bulkFetchPronouns(ids: string[]): Promise { - const params = new URLSearchParams(); - params.append("platform", "discord"); - params.append("ids", ids.join(",")); - - try { - const req = await fetch("https://pronoundb.org/api/v1/lookup-bulk?" + params.toString(), { - method: "GET", - headers: { - "Accept": "application/json", - "X-PronounDB-Source": VENCORD_USER_AGENT - } - }); - return await req.json() - .then((res: PronounsResponse) => { - Object.assign(cache, res); - return res; - }); - } catch (e) { - // If the request errors, treat it as if no pronouns were found for all ids, and log it - console.error("PronounDB fetching failed: ", e); - const dummyPronouns = Object.fromEntries(ids.map(id => [id, "unspecified"] as const)); - Object.assign(cache, dummyPronouns); - return dummyPronouns; - } -} - -export function formatPronouns(pronouns: PronounCode): string { - const { pronounsFormat } = Settings.plugins.PronounDB as { pronounsFormat: PronounsFormat, enabled: boolean; }; - // For capitalized pronouns, just return the mapping (it is by default capitalized) - if (pronounsFormat === PronounsFormat.Capitalized) return PronounMapping[pronouns]; - // If it is set to lowercase and a special code (any, ask, avoid), then just return the capitalized text - else if ( - pronounsFormat === PronounsFormat.Lowercase - && ["any", "ask", "avoid", "other"].includes(pronouns) - ) return PronounMapping[pronouns]; - // Otherwise (lowercase and not a special code), then convert the mapping to lowercase - else return PronounMapping[pronouns].toLowerCase(); -} diff --git a/src/plugins/sendify.ts b/src/plugins/sendify.ts index 40836ad..b0fd52d 100644 --- a/src/plugins/sendify.ts +++ b/src/plugins/sendify.ts @@ -17,8 +17,8 @@ */ import { ApplicationCommandInputType, sendBotMessage } from "../api/Commands"; -import { lazyWebpack } from "../utils"; import { Devs } from "../utils/constants"; +import { lazyWebpack } from "../utils/misc"; import definePlugin from "../utils/types"; import { filters } from "../webpack"; import { FluxDispatcher } from "../webpack/common"; diff --git a/src/plugins/spotifyControls/PlayerComponent.tsx b/src/plugins/spotifyControls/PlayerComponent.tsx index 0e03121..15a7ef9 100644 --- a/src/plugins/spotifyControls/PlayerComponent.tsx +++ b/src/plugins/spotifyControls/PlayerComponent.tsx @@ -18,7 +18,8 @@ import ErrorBoundary from "../../components/ErrorBoundary"; import { Flex } from "../../components/Flex"; -import { classes, debounce, LazyComponent, lazyWebpack } from "../../utils"; +import { debounce } from "../../utils/debounce"; +import { classes, LazyComponent, lazyWebpack } from "../../utils/misc"; import { ContextMenu, FluxDispatcher, Forms, Menu, React, Tooltip } from "../../webpack/common"; import { filters, find } from "../../webpack/webpack"; import { SpotifyStore, Track } from "./SpotifyStore"; diff --git a/src/plugins/spotifyControls/SpotifyStore.ts b/src/plugins/spotifyControls/SpotifyStore.ts index d7d52bc..0dad503 100644 --- a/src/plugins/spotifyControls/SpotifyStore.ts +++ b/src/plugins/spotifyControls/SpotifyStore.ts @@ -18,7 +18,9 @@ import cssText from "~fileContent/styles.css"; -import { IpcEvents, lazyWebpack, proxyLazy } from "../../utils"; +import IpcEvents from "../../utils/IpcEvents"; +import { lazyWebpack } from "../../utils/misc"; +import { proxyLazy } from "../../utils/proxyLazy"; import { filters } from "../../webpack"; import { Flux, FluxDispatcher } from "../../webpack/common"; diff --git a/src/plugins/startupTimings/StartupTimingPage.tsx b/src/plugins/startupTimings/StartupTimingPage.tsx index f864138..7d8e86b 100644 --- a/src/plugins/startupTimings/StartupTimingPage.tsx +++ b/src/plugins/startupTimings/StartupTimingPage.tsx @@ -18,9 +18,9 @@ import ErrorBoundary from "../../components/ErrorBoundary"; import { Flex } from "../../components/Flex"; -import { lazyWebpack } from "../../utils"; -import { filters } from "../../webpack"; +import { lazyWebpack } from "../../utils/misc"; import { Forms, React } from "../../webpack/common"; +import { filters } from "../../webpack/webpack"; interface AppStartPerformance { prefix: string; diff --git a/src/plugins/startupTimings/index.tsx b/src/plugins/startupTimings/index.tsx index 3f121f7..d5493c1 100644 --- a/src/plugins/startupTimings/index.tsx +++ b/src/plugins/startupTimings/index.tsx @@ -17,8 +17,8 @@ */ -import { LazyComponent } from "../../utils"; import { Devs } from "../../utils/constants"; +import { LazyComponent } from "../../utils/misc"; import definePlugin from "../../utils/types"; export default definePlugin({ -- cgit