Update index.tsx
This commit is contained in:
parent
6c68f5b5d6
commit
380a7d8cf8
1 changed files with 32 additions and 42 deletions
|
|
@ -1,6 +1,7 @@
|
|||
import { after } from "@lib/api/patcher";
|
||||
import { onJsxCreate } from "@lib/api/react/jsx";
|
||||
import { findByNameLazy } from "@metro";
|
||||
import { findByName, findByNameLazy } from "@metro";
|
||||
import { useEffect, useState } from "react";
|
||||
import { defineCorePlugin } from "..";
|
||||
import { FluxDispatcher } from "@metro/common";
|
||||
|
||||
|
|
@ -9,9 +10,14 @@ interface Badge {
|
|||
url: string;
|
||||
}
|
||||
|
||||
interface CustomBadge {
|
||||
label: string;
|
||||
url: string;
|
||||
}
|
||||
|
||||
interface UserBadgeData {
|
||||
roles?: string[];
|
||||
custom?: Badge[];
|
||||
custom?: CustomBadge[];
|
||||
}
|
||||
|
||||
interface BadgeData {
|
||||
|
|
@ -39,20 +45,20 @@ const badgeProps = new Map<string, Record<string, any>>();
|
|||
const pendingRequests = new Set<string>();
|
||||
|
||||
export default defineCorePlugin({
|
||||
manifest: {
|
||||
id: "bunny.badges",
|
||||
version: "1.2.0",
|
||||
type: "plugin",
|
||||
spec: 3,
|
||||
main: "",
|
||||
display: {
|
||||
name: "Badges",
|
||||
description: "Adds badges to user's profile",
|
||||
authors: [{ name: "cocobo1" }, { name: "pylixonly" }],
|
||||
},
|
||||
manifest: {
|
||||
id: "bunny.badges",
|
||||
version: "1.2.0",
|
||||
type: "plugin",
|
||||
spec: 3,
|
||||
main: "",
|
||||
display: {
|
||||
name: "Badges",
|
||||
description: "Adds badges to user's profile",
|
||||
authors: [{ name: "cocobo1" }, { name: "pylixonly" }],
|
||||
},
|
||||
},
|
||||
|
||||
start() {
|
||||
start() {
|
||||
onJsxCreate("ProfileBadge", (component, ret) => {
|
||||
if (ret.props.id?.startsWith("rain-")) {
|
||||
const cachedProps = badgeProps.get(ret.props.id);
|
||||
|
|
@ -73,36 +79,22 @@ export default defineCorePlugin({
|
|||
}
|
||||
});
|
||||
|
||||
// Small helper so one failing/optional source can't take down the others.
|
||||
const safeFetchJson = async <T,>(url: string, fallback: T): Promise<T> => {
|
||||
try {
|
||||
const res = await fetch(url);
|
||||
if (!res.ok) return fallback;
|
||||
return (await res.json()) as T;
|
||||
} catch {
|
||||
return fallback;
|
||||
}
|
||||
};
|
||||
|
||||
const fetchAndProcessBadges = async (userId: string) => {
|
||||
if (pendingRequests.has(userId)) return;
|
||||
pendingRequests.add(userId);
|
||||
|
||||
try {
|
||||
const [badgesData, rolesData, equicordData] = await Promise.all([
|
||||
safeFetchJson<BadgeData>(
|
||||
"https://codeberg.org/chocomint-chan/GoonCord_Badges/raw/branch/main/badges.json",
|
||||
{}
|
||||
),
|
||||
safeFetchJson<RolesData>(
|
||||
"https://codeberg.org/chocomint-chan/GoonCord_Badges/raw/branch/main/assets/roles/roles.json",
|
||||
{}
|
||||
),
|
||||
safeFetchJson<Record<string, EquicordBadge[]>>(
|
||||
"https://badge.equicord.org/badges.json",
|
||||
{}
|
||||
),
|
||||
]);
|
||||
const [badgesRes, rolesRes, equicordRes] = await Promise.all([
|
||||
fetch("https://codeberg.org/chocomint-chan/GoonCord_Badges/raw/branch/main/badges.json"),
|
||||
fetch("https://codeberg.org/chocomint-chan/GoonCord_Badges/raw/branch/main/assets/roles/roles.json"),
|
||||
fetch("https://badge.equicord.org/badges.json"),
|
||||
fetch("https://badges.vencord.dev/badges/"),
|
||||
fetch("https://codeberg.org/raincord/badges/raw/branch/main/badges.json"),
|
||||
]);
|
||||
|
||||
const badgesData: BadgeData = await badgesRes.json();
|
||||
const rolesData: RolesData = await rolesRes.json();
|
||||
const equicordData: Record<string, EquicordBadge[]> = equicordRes.ok ? await equicordRes.json() : {};
|
||||
|
||||
const userBadgeData = badgesData[userId] || { roles: [], custom: [] };
|
||||
|
||||
|
|
@ -145,14 +137,12 @@ export default defineCorePlugin({
|
|||
});
|
||||
|
||||
FluxDispatcher.dispatch({ type: "USER_UPDATE", user: { id: userId } });
|
||||
} catch (err) {
|
||||
console.error("[bunny.badges] Failed to fetch/process badges:", err);
|
||||
} finally {
|
||||
pendingRequests.delete(userId);
|
||||
}
|
||||
};
|
||||
|
||||
return after("default", useBadgesModule, ([user], result) => {
|
||||
after("default", useBadgesModule, ([user], result) => {
|
||||
if (!user) return;
|
||||
|
||||
const userId = user.userId;
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue