feat: adjusting achievements page

This commit is contained in:
Zamitto 2024-10-14 12:46:25 -03:00
parent 1d29bc3620
commit 034e88e286
6 changed files with 153 additions and 52 deletions

View file

@ -338,6 +338,7 @@
"achievement": { "achievement": {
"achievement_unlocked": "Achievement unlocked", "achievement_unlocked": "Achievement unlocked",
"user_achievements": "{{displayName}}'s Achievements", "user_achievements": "{{displayName}}'s Achievements",
"your_achievements": "Your Achievements" "your_achievements": "Your Achievements",
"unlocked_at": "Unlocked at:"
} }
} }

View file

@ -340,6 +340,7 @@
"achievement": { "achievement": {
"achievement_unlocked": "Conquista desbloqueada", "achievement_unlocked": "Conquista desbloqueada",
"your_achievements": "Suas Conquistas", "your_achievements": "Suas Conquistas",
"user_achievements": "Conquistas de {{displayName}}" "user_achievements": "Conquistas de {{displayName}}",
"unlocked_at": "Desbloqueado em:"
} }
} }

View file

@ -282,6 +282,7 @@
"your_friend_code": "Seu código de amigo:" "your_friend_code": "Seu código de amigo:"
}, },
"achievement": { "achievement": {
"achievement_unlocked": "Conquista desbloqueada" "achievement_unlocked": "Conquista desbloqueada",
"unlocked_at": "Desbloqueado em:"
} }
} }

View file

@ -3,7 +3,7 @@ import {
gameRepository, gameRepository,
userPreferencesRepository, userPreferencesRepository,
} from "@main/repository"; } from "@main/repository";
import type { GameShop, UnlockedAchievement } from "@types"; import type { AchievementData, GameShop, UnlockedAchievement } from "@types";
import { WindowManager } from "../window-manager"; import { WindowManager } from "../window-manager";
import { HydraApi } from "../hydra-api"; import { HydraApi } from "../hydra-api";
import { getGameAchievements } from "@main/events/catalogue/get-game-achievements"; import { getGameAchievements } from "@main/events/catalogue/get-game-achievements";
@ -52,9 +52,13 @@ export const mergeAchievements = async (
userPreferencesRepository.findOne({ where: { id: 1 } }), userPreferencesRepository.findOne({ where: { id: 1 } }),
]); ]);
const achievementsData = JSON.parse(
localGameAchievement?.achievements || "[]"
) as AchievementData[];
const unlockedAchievements = JSON.parse( const unlockedAchievements = JSON.parse(
localGameAchievement?.unlockedAchievements || "[]" localGameAchievement?.unlockedAchievements || "[]"
).filter((achievement) => achievement.name); ).filter((achievement) => achievement.name) as UnlockedAchievement[];
const newAchievements = achievements const newAchievements = achievements
.filter((achievement) => { .filter((achievement) => {
@ -81,20 +85,18 @@ export const mergeAchievements = async (
return a.unlockTime - b.unlockTime; return a.unlockTime - b.unlockTime;
}) })
.map((achievement) => { .map((achievement) => {
return JSON.parse(localGameAchievement?.achievements || "[]").find( return achievementsData.find((steamAchievement) => {
(steamAchievement) => {
return ( return (
achievement.name.toUpperCase() === achievement.name.toUpperCase() ===
steamAchievement.name.toUpperCase() steamAchievement.name.toUpperCase()
); );
} });
);
}) })
.filter((achievement) => achievement) .filter((achievement) => achievement)
.map((achievement) => { .map((achievement) => {
return { return {
displayName: achievement.displayName, displayName: achievement!.displayName,
iconUrl: achievement.icon, iconUrl: achievement!.icon,
}; };
}); });

View file

@ -1,7 +1,7 @@
import { setHeaderTitle } from "@renderer/features"; import { setHeaderTitle } from "@renderer/features";
import { useAppDispatch, useDate } from "@renderer/hooks"; import { useAppDispatch, useDate } from "@renderer/hooks";
import { steamUrlBuilder } from "@shared"; import { steamUrlBuilder } from "@shared";
import { useContext, useEffect, useRef, useState } from "react"; import { useContext, useEffect, useMemo, useRef, useState } from "react";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
import * as styles from "./achievements.css"; import * as styles from "./achievements.css";
import { formatDownloadProgress } from "@renderer/helpers"; import { formatDownloadProgress } from "@renderer/helpers";
@ -21,6 +21,7 @@ interface AchievementsContentProps {
interface AchievementListProps { interface AchievementListProps {
achievements: UserAchievement[]; achievements: UserAchievement[];
otherUserAchievements: UserAchievement[];
} }
interface AchievementPanelProps { interface AchievementPanelProps {
@ -92,13 +93,22 @@ function AchievementPanel({
); );
} }
function AchievementList({ achievements }: AchievementListProps) { function AchievementList({
achievements,
otherUserAchievements,
}: AchievementListProps) {
const { t } = useTranslation("achievement");
const { formatDateTime } = useDate(); const { formatDateTime } = useDate();
if (otherUserAchievements.length === 0) {
return ( return (
<ul className={styles.list}> <ul className={styles.list}>
{achievements.map((achievement, index) => ( {achievements.map((achievement, index) => (
<li key={index} className={styles.listItem}> <li
key={index}
className={styles.listItem}
style={{ display: "flex" }}
>
<img <img
className={styles.listItemImage({ className={styles.listItemImage({
unlocked: achievement.unlocked, unlocked: achievement.unlocked,
@ -107,12 +117,82 @@ function AchievementList({ achievements }: AchievementListProps) {
alt={achievement.displayName} alt={achievement.displayName}
loading="lazy" loading="lazy"
/> />
<div> <div style={{ flex: 1 }}>
<p>{achievement.displayName}</p> <h4>{achievement.displayName}</h4>
<p>{achievement.description}</p> <p>{achievement.description}</p>
<small> </div>
{achievement.unlockTime && formatDateTime(achievement.unlockTime)} {achievement.unlockTime && (
</small> <div style={{ whiteSpace: "nowrap" }}>
<small>{t("unlocked_at")}</small>
<p>{formatDateTime(achievement.unlockTime)}</p>
</div>
)}
</li>
))}
</ul>
);
}
return (
<ul className={styles.list}>
{otherUserAchievements.map((otherUserAchievement, index) => (
<li
key={index}
className={styles.listItem}
style={{ display: "grid", gridTemplateColumns: "1fr auto 1fr" }}
>
<div
style={{
display: "flex",
flexDirection: "row",
alignItems: "center",
gap: `${SPACING_UNIT}px`,
}}
>
<img
className={styles.listItemImage({
unlocked: otherUserAchievement.unlocked,
})}
src={otherUserAchievement.icon}
alt={otherUserAchievement.displayName}
loading="lazy"
/>
{otherUserAchievement.unlockTime && (
<div style={{ whiteSpace: "nowrap" }}>
<small>{t("unlocked_at")}</small>
<p>{formatDateTime(otherUserAchievement.unlockTime)}</p>
</div>
)}
</div>
<div style={{ textAlign: "center" }}>
<h4>{otherUserAchievement.displayName}</h4>
<p>{otherUserAchievement.description}</p>
</div>
<div
style={{
display: "flex",
flexDirection: "row-reverse",
alignItems: "center",
gap: `${SPACING_UNIT}px`,
textAlign: "right",
}}
>
<img
className={styles.listItemImage({
unlocked: achievements[index].unlocked,
})}
src={achievements[index].icon}
alt={achievements[index].displayName}
loading="lazy"
/>
{achievements[index].unlockTime && (
<div style={{ whiteSpace: "nowrap" }}>
<small>{t("unlocked_at")}</small>
<p>{formatDateTime(achievements[index].unlockTime)}</p>
</div>
)}
</div> </div>
</li> </li>
))} ))}
@ -128,13 +208,27 @@ export function AchievementsContent({
const containerRef = useRef<HTMLDivElement | null>(null); const containerRef = useRef<HTMLDivElement | null>(null);
const [isHeaderStuck, setIsHeaderStuck] = useState(false); const [isHeaderStuck, setIsHeaderStuck] = useState(false);
const [backdropOpactiy, setBackdropOpacity] = useState(1); const [backdropOpactiy, setBackdropOpacity] = useState(1);
const [pageAchievements, setPageAchievements] = useState<UserAchievement[]>( const [otherUserAchievements, setOtherUserAchievements] = useState<
[] UserAchievement[]
); >([]);
const { gameTitle, objectId, shop, achievements, gameColor, setGameColor } = const { gameTitle, objectId, shop, achievements, gameColor, setGameColor } =
useContext(gameDetailsContext); useContext(gameDetailsContext);
const sortedAchievements = useMemo(() => {
if (otherUserAchievements.length === 0) return achievements;
return achievements.sort((a, b) => {
const indexA = otherUserAchievements.findIndex(
(achievement) => achievement.name === a.name
);
const indexB = otherUserAchievements.findIndex(
(achievement) => achievement.name === b.name
);
return indexA - indexB;
});
}, [achievements, otherUserAchievements]);
const dispatch = useAppDispatch(); const dispatch = useAppDispatch();
useEffect(() => { useEffect(() => {
@ -161,7 +255,7 @@ export function AchievementsContent({
window.electron window.electron
.getGameAchievements(objectId, shop as GameShop, userId) .getGameAchievements(objectId, shop as GameShop, userId)
.then((achievements) => { .then((achievements) => {
setPageAchievements(achievements); setOtherUserAchievements(achievements);
}); });
} }
}, [objectId, shop, userId]); }, [objectId, shop, userId]);
@ -188,8 +282,6 @@ export function AchievementsContent({
if (!objectId || !shop || !gameTitle) return null; if (!objectId || !shop || !gameTitle) return null;
const userAchievements = userId ? pageAchievements : achievements;
return ( return (
<div className={styles.wrapper}> <div className={styles.wrapper}>
<img <img
@ -228,25 +320,30 @@ export function AchievementsContent({
</div> </div>
<div className={styles.panel({ stuck: isHeaderStuck })}> <div className={styles.panel({ stuck: isHeaderStuck })}>
{userId && (
<AchievementPanel <AchievementPanel
displayName={displayName} displayName={displayName}
achievements={userAchievements} achievements={otherUserAchievements}
/> />
{pageAchievements.length > 0 && (
<AchievementPanel displayName={null} achievements={achievements} />
)} )}
<AchievementPanel
displayName={null}
achievements={sortedAchievements}
/>
</div> </div>
<div <div
style={{ style={{
display: "flex", display: "flex",
flexDirection: "row", flexDirection: "row",
width: "100%",
backgroundColor: vars.color.background,
}} }}
> >
{pageAchievements.length > 0 && ( <AchievementList
<AchievementList achievements={pageAchievements} /> achievements={sortedAchievements}
)} otherUserAchievements={otherUserAchievements}
/>
<AchievementList achievements={achievements} />
</div> </div>
</section> </section>
</div> </div>

View file

@ -93,11 +93,10 @@ export const list = style({
flexDirection: "column", flexDirection: "column",
gap: `${SPACING_UNIT * 2}px`, gap: `${SPACING_UNIT * 2}px`,
padding: `${SPACING_UNIT * 2}px`, padding: `${SPACING_UNIT * 2}px`,
backgroundColor: vars.color.background, width: "100%",
}); });
export const listItem = style({ export const listItem = style({
display: "flex",
transition: "all ease 0.1s", transition: "all ease 0.1s",
color: vars.color.muted, color: vars.color.muted,
width: "100%", width: "100%",