feat: showing achievements queue

This commit is contained in:
Zamitto 2024-10-02 22:58:10 -03:00
parent cadb9e8dff
commit f0e0abae8c
2 changed files with 35 additions and 14 deletions

View File

@ -66,6 +66,9 @@ export const mergeAchievements = async (
if (newAchievements.length && publishNotification) {
const achievementsInfo = newAchievements
.sort((a, b) => {
return a.unlockTime - b.unlockTime;
})
.map((achievement) => {
return JSON.parse(localGameAchievement?.achievements || "[]").find(
(steamAchievement) => {

View File

@ -1,15 +1,18 @@
import { useEffect, useMemo, useState } from "react";
import { useEffect, useMemo, useRef, useState } from "react";
import achievementSound from "@renderer/assets/audio/achievement.wav";
import { useTranslation } from "react-i18next";
import { vars } from "@renderer/theme.css";
interface AchievementInfo {
displayName: string;
iconUrl: string;
}
export function Achievement() {
const { t } = useTranslation("achievement");
const [achievementInfo, setAchievementInfo] = useState<{
displayName: string;
icon: string;
} | null>(null);
const [achievements, setAchievements] = useState<AchievementInfo[]>([]);
const achievementAnimation = useRef(-1);
const audio = useMemo(() => {
const audio = new Audio(achievementSound);
@ -24,11 +27,7 @@ export function Achievement() {
if (!achievements) return;
if (achievements.length) {
const achievement = achievements[0];
setAchievementInfo({
displayName: achievement.displayName,
icon: achievement.iconUrl,
});
setAchievements((ach) => ach.concat(achievements));
}
audio.play();
@ -40,7 +39,26 @@ export function Achievement() {
};
}, [audio]);
if (!achievementInfo) return null;
const hasAchievementsPending = achievements.length > 0;
useEffect(() => {
if (hasAchievementsPending) {
let zero = performance.now();
achievementAnimation.current = requestAnimationFrame(
function animateLock(time) {
if (time - zero > 3000) {
zero = performance.now();
setAchievements((ach) => ach.slice(1));
}
achievementAnimation.current = requestAnimationFrame(animateLock);
}
);
} else {
cancelAnimationFrame(achievementAnimation.current);
}
}, [hasAchievementsPending]);
if (!hasAchievementsPending) return null;
return (
<div
@ -53,13 +71,13 @@ export function Achievement() {
}}
>
<img
src={achievementInfo.icon}
alt={achievementInfo.displayName}
src={achievements[0].iconUrl}
alt={achievements[0].displayName}
style={{ width: 60, height: 60 }}
/>
<div>
<p>{t("achievement_unlocked")}</p>
<p>{achievementInfo.displayName}</p>
<p>{achievements[0].displayName}</p>
</div>
</div>
);