From fa2a92d5ed6ed58568e6ca7d33302f4daae9a4a3 Mon Sep 17 00:00:00 2001 From: Zamitto <167933696+zamitto@users.noreply.github.com> Date: Fri, 14 Jun 2024 14:46:30 -0300 Subject: [PATCH] feat: adding time played / last played --- src/locales/pt/translation.json | 3 + src/main/events/profile/get-user-profile.ts | 12 +++- .../src/components/sidebar/sidebar.tsx | 2 +- .../src/pages/profile/profile-content.tsx | 62 ++++++++++++++++--- .../src/pages/profile/profile.css.tsx | 2 +- src/types/index.ts | 2 + 6 files changed, 70 insertions(+), 13 deletions(-) diff --git a/src/locales/pt/translation.json b/src/locales/pt/translation.json index 67732144..6f6855c1 100644 --- a/src/locales/pt/translation.json +++ b/src/locales/pt/translation.json @@ -225,5 +225,8 @@ }, "forms": { "toggle_password_visibility": "Alternar visibilidade da senha" + }, + "user_profile": { + "amount_hours": "Jogou por {{amount}}" } } diff --git a/src/main/events/profile/get-user-profile.ts b/src/main/events/profile/get-user-profile.ts index d8d16782..7829e928 100644 --- a/src/main/events/profile/get-user-profile.ts +++ b/src/main/events/profile/get-user-profile.ts @@ -24,7 +24,11 @@ const getUserProfile = async ( ? getSteamAppAsset("icon", game.objectId, steamGame.clientIcon) : null; - return { ...convertSteamGameToCatalogueEntry(steamGame), iconUrl }; + return { + ...game, + ...convertSteamGameToCatalogueEntry(steamGame), + iconUrl, + }; }) ); @@ -37,7 +41,11 @@ const getUserProfile = async ( ? getSteamAppAsset("icon", game.objectId, steamGame.clientIcon) : null; - return { ...convertSteamGameToCatalogueEntry(steamGame), iconUrl }; + return { + ...game, + ...convertSteamGameToCatalogueEntry(steamGame), + iconUrl, + }; }) ); diff --git a/src/renderer/src/components/sidebar/sidebar.tsx b/src/renderer/src/components/sidebar/sidebar.tsx index b81c84b8..de72798b 100644 --- a/src/renderer/src/components/sidebar/sidebar.tsx +++ b/src/renderer/src/components/sidebar/sidebar.tsx @@ -144,7 +144,7 @@ export function Sidebar() { }; const handleClickProfile = () => { - navigate("/profile/pmbk5ezJ"); + navigate("/profile/olejRejN"); }; return ( diff --git a/src/renderer/src/pages/profile/profile-content.tsx b/src/renderer/src/pages/profile/profile-content.tsx index d9d62256..9b0d1cb4 100644 --- a/src/renderer/src/pages/profile/profile-content.tsx +++ b/src/renderer/src/pages/profile/profile-content.tsx @@ -1,13 +1,41 @@ -import { UserProfile } from "@types"; +import { ProfileGame, UserProfile } from "@types"; import cn from "classnames"; import * as styles from "./profile.css"; import { SPACING_UNIT, vars } from "@renderer/theme.css"; +import { useMemo } from "react"; +import { useTranslation } from "react-i18next"; +import { formatDistance } from "date-fns"; export interface ProfileContentProps { userProfile: UserProfile; } +const MAX_MINUTES_TO_SHOW_IN_PLAYTIME = 120; + export const ProfileContent = ({ userProfile }: ProfileContentProps) => { + const { t, i18n } = useTranslation("user_profile"); + + const numberFormatter = useMemo(() => { + return new Intl.NumberFormat(i18n.language, { + maximumFractionDigits: 0, + }); + }, [i18n.language]); + + const formatPlayTime = (game: ProfileGame) => { + console.log(game); + const seconds = game.playTimeInSeconds; + const minutes = seconds / 60; + + if (minutes < MAX_MINUTES_TO_SHOW_IN_PLAYTIME) { + return t("amount_minutes", { + amount: minutes.toFixed(0), + }); + } + + const hours = minutes / 60; + return t("amount_hours", { amount: numberFormatter.format(hours) }); + }; + return ( <>
@@ -46,12 +74,18 @@ export const ProfileContent = ({ userProfile }: ProfileContentProps) => {
{userProfile.recentGames.map((game) => { return ( -
+
{ />

{game.title}

-

há 3 horas

+

+ {formatDistance(game.lastTimePlayed!, new Date(), { + addSuffix: true, + })} +

); @@ -91,12 +129,18 @@ export const ProfileContent = ({ userProfile }: ProfileContentProps) => {
{userProfile.libraryGames.map((game) => { return ( -
+
{ />

{game.title}

-

Jogou por 10 horas

+

{formatPlayTime(game)}

); diff --git a/src/renderer/src/pages/profile/profile.css.tsx b/src/renderer/src/pages/profile/profile.css.tsx index a6aa04e8..83174de8 100644 --- a/src/renderer/src/pages/profile/profile.css.tsx +++ b/src/renderer/src/pages/profile/profile.css.tsx @@ -46,7 +46,7 @@ export const profileGameSection = style({ width: "100%", display: "flex", flexDirection: "column", - gap: `${SPACING_UNIT}px`, + gap: `${SPACING_UNIT * 2}px`, }); export const contentSidebar = style({ diff --git a/src/types/index.ts b/src/types/index.ts index 88b5c426..24104488 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -91,6 +91,8 @@ export interface ProfileGame { title: string; iconUrl; string?; + playTimeInSeconds: number; + lastTimePlayed: Date | null; } export interface DownloadQueue {