-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.tsx
124 lines (109 loc) · 3.9 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import { addBadge, BadgePosition, BadgeUserArgs, ProfileBadge, removeBadge } from "@api/Badges";
import definePlugin, { OptionType } from "@utils/types";
import { React, Tooltip } from "@webpack/common";
type CustomBadge = string | {
name: string;
badge: string;
custom?: boolean;
};
interface BadgeCache {
badges: {
[mod: string]: CustomBadge[];
};
expires: number;
}
const API_URL = "https://clientmodbadges-api.herokuapp.com";
const cache = new Map<string, BadgeCache>();
const EXPIRES = 1000 * 60 * 15;
async function fetchBadges(id: string): Promise<BadgeCache["badges"] | undefined> {
const cachedValue = cache.get(id);
if (!cache.has(id) || (cachedValue && cachedValue.expires < Date.now())) {
const resp = await fetch(`${API_URL}/users/${id}`);
const body = await resp.json() as BadgeCache["badges"];
cache.set(id, { badges: body, expires: Date.now() + EXPIRES });
return body;
} else if (cachedValue) {
return cachedValue.badges;
}
};
function BadgeComponent({ name, img }: { name: string, img: string; }) {
return (
<Tooltip text={name} >
{(tooltipProps: any) => (
<img
{...tooltipProps}
src={img}
style={{
width: "20px",
height: "20px",
transform: name.includes("Replugged") ? "scale(0.9)" : null
}}
/>
)}
</Tooltip>
);
};
function GlobalBadges({ userId }: BadgeUserArgs) {
const [badges, setBadges] = React.useState<BadgeCache["badges"]>({});
React.useEffect(() => {
fetchBadges(userId)
.then(setBadges);
}, []);
if (!badges || !Object.keys(badges).length) return null;
const globalBadges: JSX.Element[] = [];
Object.keys(badges).forEach(mod => {
if (mod.toLowerCase() === "vencord") return;
badges[mod].forEach(badge => {
if (typeof badge === "string") {
const fullNames = { "hunter": "Bug Hunter", "early": "Early User" };
badge = {
name: fullNames[badge as string] ? fullNames[badge as string] : badge,
badge: `${API_URL}/badges/${mod}/${(badge as string).replace(mod, "").trim().split(" ")[0]}`
};
} else if (typeof badge === "object") badge.custom = true;
if (!showCustom() && badge.custom) return;
const cleanName = badge.name.replace(mod, "").trim();
const prefix = showPrefix() ? mod : "";
if (!badge.custom) badge.name = `${prefix} ${cleanName.charAt(0).toUpperCase() + cleanName.slice(1)}`;
globalBadges.push(<BadgeComponent name={badge.name} img={badge.badge} />);
});
});
return (
<div
className="vc-global-badges"
style={{
display: "flex",
alignItems: "center"
}}
>
{globalBadges}
</div>
);
};
const Badge: ProfileBadge = {
component: GlobalBadges,
position: BadgePosition.START
};
const showPrefix = () => Vencord.Settings.plugins.GlobalBadges.showPrefix;
const showCustom = () => Vencord.Settings.plugins.GlobalBadges.showCustom;
export default definePlugin({
name: "GlobalBadges",
description: "Adds global badges from other client mods",
authors: [{ name: "domi.btnr", id: 354191516979429376n }],
start: () => addBadge(Badge),
stop: () => removeBadge(Badge),
options: {
showPrefix: {
type: OptionType.BOOLEAN,
description: "Shows the Mod as Prefix",
default: true,
restartNeeded: false
},
showCustom: {
type: OptionType.BOOLEAN,
description: "Show Custom Badges",
default: true,
restartNeeded: false
}
}
});