mirror of
https://github.com/Vendicated/Vencord.git
synced 2025-01-10 09:56:24 +00:00
fix: HEAD usrbg urls
This commit is contained in:
parent
052c4d928d
commit
7604628496
1 changed files with 33 additions and 15 deletions
|
@ -11,10 +11,11 @@ import { Devs } from "@utils/constants";
|
||||||
import { fetchUserProfile } from "@utils/discord";
|
import { fetchUserProfile } from "@utils/discord";
|
||||||
import { Queue } from "@utils/Queue";
|
import { Queue } from "@utils/Queue";
|
||||||
import definePlugin, { OptionType } from "@utils/types";
|
import definePlugin, { OptionType } from "@utils/types";
|
||||||
import { useEffect, UserProfileStore, useStateFromStores } from "@webpack/common";
|
import { useEffect, UserProfileStore, useState, useStateFromStores } from "@webpack/common";
|
||||||
import { User } from "discord-types/general";
|
import { User } from "discord-types/general";
|
||||||
|
|
||||||
import style from "./index.css";
|
import style from "./index.css";
|
||||||
|
import { useAwaiter } from "@utils/react";
|
||||||
|
|
||||||
const settings = definePluginSettings({
|
const settings = definePluginSettings({
|
||||||
animate: {
|
animate: {
|
||||||
|
@ -24,16 +25,20 @@ const settings = definePluginSettings({
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const queue = new Queue();
|
const discordQueue = new Queue();
|
||||||
|
const usrbgQueue = new Queue();
|
||||||
|
|
||||||
|
|
||||||
const useFetchMemberProfile = (userId: string): string => {
|
const useFetchMemberProfile = (userId: string): string => {
|
||||||
const profile = useStateFromStores([UserProfileStore], () => UserProfileStore.getUserProfile(userId));
|
const profile = useStateFromStores([UserProfileStore], () => UserProfileStore.getUserProfile(userId));
|
||||||
|
|
||||||
|
const usrbgUrl = (Vencord.Plugins.plugins.USRBG as any)?.getImageUrl(userId);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
if (usrbgUrl) return;
|
||||||
let cancel = false;
|
let cancel = false;
|
||||||
|
|
||||||
queue.push(() => {
|
discordQueue.push(() => {
|
||||||
if (cancel) return Promise.resolve(void 0);
|
if (cancel) return Promise.resolve(void 0);
|
||||||
return fetchUserProfile(userId).finally(async () => {
|
return fetchUserProfile(userId).finally(async () => {
|
||||||
await new Promise<void>(resolve => setTimeout(resolve, 1000));
|
await new Promise<void>(resolve => setTimeout(resolve, 1000));
|
||||||
|
@ -43,6 +48,8 @@ const useFetchMemberProfile = (userId: string): string => {
|
||||||
return () => { cancel = true; };
|
return () => { cancel = true; };
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
if (usrbgUrl) return usrbgUrl;
|
||||||
|
|
||||||
if (!profile?.banner) return "";
|
if (!profile?.banner) return "";
|
||||||
const extension = settings.store.animate && profile.banner.startsWith("a_")
|
const extension = settings.store.animate && profile.banner.startsWith("a_")
|
||||||
? ".gif"
|
? ".gif"
|
||||||
|
@ -85,18 +92,29 @@ export default definePlugin({
|
||||||
},
|
},
|
||||||
|
|
||||||
memberListBanner: ErrorBoundary.wrap(({ user }: { user: User; }) => {
|
memberListBanner: ErrorBoundary.wrap(({ user }: { user: User; }) => {
|
||||||
let url: string | null = null;
|
const url = useFetchMemberProfile(user.id);
|
||||||
// usrbg api has no way of telling if the banner is animated or not
|
|
||||||
// if the user doesnt want animated banners, just get rid of usrbg until there is a way to tell
|
const [shouldShow] = useAwaiter(async () => {
|
||||||
if (settings.store.animate && Vencord.Plugins.isPluginEnabled("USRBG")) {
|
// This will get re-run when the url changes
|
||||||
const USRBG = Vencord.Plugins.plugins.USRBG as unknown as typeof import("../usrbg/index").default;
|
if (!url || url === "") return false;
|
||||||
url = USRBG.getImageUrl(user.id);
|
if (!settings.store.animate) {
|
||||||
}
|
// Discord cdn can return both png and gif, useFetchMemberProfile gives it respectively
|
||||||
if (!url) {
|
if (url!.includes("cdn.discordapp.com")) return true;
|
||||||
url = useFetchMemberProfile(user.id);
|
|
||||||
}
|
// HEAD request to check if the image is a png
|
||||||
if (url === "") return null;
|
return await new Promise(async (resolve) => {
|
||||||
if (!settings.store.animate) url = url.replace(".gif", ".png");
|
usrbgQueue.push(() => fetch(url!.replace(".gif", ".png"), { method: "HEAD" }).then(async res => {
|
||||||
|
console.log(res);
|
||||||
|
await new Promise<void>(resolve => setTimeout(resolve, 1000));
|
||||||
|
resolve(res.ok && res.headers.get("content-type")?.startsWith("image/png"));
|
||||||
|
return;
|
||||||
|
}));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}, { fallbackValue: false, deps: [url] });
|
||||||
|
|
||||||
|
if (!shouldShow) return null;
|
||||||
return (
|
return (
|
||||||
<img src={url} className="vc-banners-everywhere-memberlist"></img>
|
<img src={url} className="vc-banners-everywhere-memberlist"></img>
|
||||||
);
|
);
|
||||||
|
|
Loading…
Reference in a new issue