Added support for gitlab stars for the front page

This commit is contained in:
lplaat 2024-03-13 14:12:51 +01:00
parent b3f806b22b
commit 353abd83c1
3 changed files with 51 additions and 27 deletions

View file

@ -1 +1,2 @@
HCAPTCHA_KEY=ES_
HCAPTCHA_KEY=ES_
GITLAB_API_TOKEN=

View file

@ -1,38 +1,54 @@
import { building } from "$app/environment";
import { DISCORD_USER_TOKEN } from "$env/static/private";
import { DISCORD_USER_TOKEN, GITLAB_API_TOKEN } from "$env/static/private";
let memberCount = 0;
let roleMembers: { [key: string]: number } = {
"1214817156420862012": 50,
let starCount = 0;
let roleMembers = {
"1214817156420862012": 50,
};
async function setMemberCount() {
console.log("Fetching member count");
const promises = [
fetch("https://discord.com/api/v9/invites/suyu?with_counts=true&with_expiration=true"),
DISCORD_USER_TOKEN
? fetch("https://discord.com/api/v9/guilds/1214371687114477618/roles/member-counts", {
headers: {
Authorization: DISCORD_USER_TOKEN,
},
})
: Promise.resolve({ json: () => roleMembers }),
];
const [res, roles] = await Promise.all(promises);
const jsonPromises = [res.json(), roles.json()];
const [resJson, rolesJson] = await Promise.all(jsonPromises);
memberCount = resJson.approximate_member_count;
if (DISCORD_USER_TOKEN) roleMembers = rolesJson;
console.log("Member count:", memberCount);
console.log("Fetching member count");
const promises = [
fetch("https://discord.com/api/v9/invites/suyu?with_counts=true&with_expiration=true"),
DISCORD_USER_TOKEN
? fetch("https://discord.com/api/v9/guilds/1214371687114477618/roles/member-counts", {
headers: {
Authorization: DISCORD_USER_TOKEN,
},
})
: Promise.resolve({ json: () => roleMembers }),
GITLAB_API_TOKEN
? fetch("https://gitlab.com/api/v4/projects/suyu-emu%2Fsuyu/", {
headers: {
Authorization: `Bearer ${GITLAB_API_TOKEN}`,
},
})
: Promise.resolve({ json: () => ({ star_count: 0 }) }), // Default to 0 if no token is provided
];
const [res, roles, gitlabRes] = await Promise.all(promises);
const jsonPromises = [res.json(), roles.json(), gitlabRes.json()];
const [resJson, rolesJson, gitlabResJson] = await Promise.all(jsonPromises);
memberCount = resJson.approximate_member_count;
starCount = gitlabResJson.star_count;
if (DISCORD_USER_TOKEN) roleMembers = rolesJson;
console.log("Member count:", memberCount);
console.log("Stars count:", starCount);
}
if (!building) {
await setMemberCount();
setInterval(setMemberCount, 1000 * 60 * 10);
await setMemberCount();
setInterval(setMemberCount, 1000 * 60 * 10);
}
export async function load(opts) {
return {
memberCount,
roleMembers,
};
return {
memberCount,
starCount,
roleMembers,
};
}

View file

@ -8,6 +8,7 @@
export let data: PageData;
$: memberCount = parseFloat(data.memberCount.toPrecision(2));
$: contributors = parseFloat(data.roleMembers["1214817156420862012"].toPrecision(2));
$: starCount = data.starCount;
let metadata = {
url: "https://suyu.dev",
title: "suyu - Open-source, non-profit Switch emulator",
@ -157,6 +158,12 @@
</h2>
<div class="text-[#A6A5A7]">dedicated contributors</div>
</div>
<div class="flex flex-col gap-0">
<h2 class="flex items-center gap-1 text-[40px] leading-[1.1]">
{starCount}+
</h2>
<div class="text-[#A6A5A7]">gitlab Stars</div>
</div>
<div class="flex flex-col gap-0">
<h2 class="flex items-center gap-1 text-[40px] leading-[1.1]">
<!-- <AnimatedCounter