Skip to content

Commit

Permalink
Fixed CoreMods badges
Browse files Browse the repository at this point in the history
  • Loading branch information
zrodevkaan committed Apr 3, 2024
1 parent 1ceb6a9 commit 7f18a70
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions src/renderer/coremods/badges/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import { Logger } from "@replugged";
import type { User } from "discord-types/general";
import { Injector } from "../../modules/injector";
import { getByProps, waitForProps } from "../../modules/webpack";
import {util} from "@replugged";

Check warning on line 6 in src/renderer/coremods/badges/index.tsx

View workflow job for this annotation

GitHub Actions / Run ESLint

'@Replugged' import is duplicated
import { generalSettings } from "../settings/pages/General";
import { APIBadges, BadgeSizes, Custom, badgeElements, getBadgeSizeClass } from "./badge";
import {Tree} from "../../util";

const injector = new Injector();

Expand All @@ -22,19 +24,25 @@ interface BadgeModArgs {

type BadgeMod = (args: BadgeModArgs) =>
| React.ReactElement<{
children?: React.ReactElement[];
className: string;
}>
children?: React.ReactElement[];
className: string;
}>
| undefined;

interface BadgeCache {
badges: APIBadges;
lastFetch: number;
}

interface BadgeHighs
{
props:{children:{props:{children:{}}}}
}

// todo: guilds
const cache = new Map<string, BadgeCache>();
const REFRESH_INTERVAL = 1000 * 60 * 30;
type TreeWithCombine = Tree & { className: string };

export async function start(): Promise<void> {
const mod = await waitForProps<{ BadgeSizes: BadgeSizes; default: BadgeMod }>("BadgeSizes");
Expand All @@ -43,7 +51,7 @@ export async function start(): Promise<void> {
"containerWithContent",
)!;

injector.after(mod, "default", ([props], res) => {
injector.after(mod, "default", ([props], res: BadgeHighs) => {
let {
user: { id },
shrinkAtCount,
Expand Down Expand Up @@ -93,7 +101,7 @@ export async function start(): Promise<void> {
if (!badges) {
return res;
}
const children = res?.props.children;
const children = res?.props.children.props.children;
if (!children || !Array.isArray(children)) {
logger.error("Error injecting badges: res.props.children is not an array", { children });
return res;
Expand Down Expand Up @@ -134,15 +142,19 @@ export async function start(): Promise<void> {
}
});

const badgesClassName: TreeWithCombine | any = util.findInTree(res as unknown as Tree, x => Boolean(x?.className));
if (!badgesClassName) return;

if (children.length > 0) {
if (!res.props.className.includes(containerWithContent)) {
res.props.className += ` ${containerWithContent}`;
if (!badgesClassName.className.includes(containerWithContent)) {
badgesClassName.className += ` ${containerWithContent}`;
}
if (!res.props.className.includes("replugged-badges-container")) {
res.props.className += " replugged-badges-container";
if (!badgesClassName.className.includes("replugged-badges-container")) {
badgesClassName.className += " replugged-badges-container";
}
}


return res;
} catch (err) {
logger.error(err);
Expand Down

0 comments on commit 7f18a70

Please sign in to comment.