From d9cdef5515006934a74e18d6c7dce48456dd2ac3 Mon Sep 17 00:00:00 2001 From: Liam Date: Sat, 28 Sep 2024 15:19:51 +0100 Subject: [PATCH] add tracked days count --- .../(pages)/api/player/isbeingtracked/route.ts | 1 + src/common/player-utils.ts | 3 +-- src/common/player/player-tracked-since.ts | 5 +++++ src/common/schema/player-schema.ts | 10 +++++----- src/components/player/player-data.tsx | 13 +++++++++---- src/components/player/player-tracked-status.tsx | 1 + src/hooks/use-is-mobile.ts | 17 +++++++++++++++++ 7 files changed, 39 insertions(+), 11 deletions(-) create mode 100644 src/hooks/use-is-mobile.ts diff --git a/src/app/(pages)/api/player/isbeingtracked/route.ts b/src/app/(pages)/api/player/isbeingtracked/route.ts index 8319238..66efc41 100644 --- a/src/app/(pages)/api/player/isbeingtracked/route.ts +++ b/src/app/(pages)/api/player/isbeingtracked/route.ts @@ -19,6 +19,7 @@ export async function GET(request: NextRequest) { }; if (foundPlayer != null) { response["trackedSince"] = foundPlayer.trackedSince?.toUTCString(); + response["daysTracked"] = foundPlayer.getStatisticHistory().size; } return NextResponse.json(response); } diff --git a/src/common/player-utils.ts b/src/common/player-utils.ts index acd059e..6f6bf08 100644 --- a/src/common/player-utils.ts +++ b/src/common/player-utils.ts @@ -74,8 +74,7 @@ export async function trackScoreSaberPlayer( if ( foundPlayer.rawPlayer && foundPlayer.rawPlayer.inactive && - Date.now() - foundPlayer.getLastTracked().getTime() > - INACTIVE_CHECK_AGAIN_TIME + Date.now() - foundPlayer.getked().getTime() > INACTIVE_CHECK_AGAIN_TIME ) { io && (await io.logger.warn( diff --git a/src/common/player/player-tracked-since.ts b/src/common/player/player-tracked-since.ts index 6841f65..8b3756f 100644 --- a/src/common/player/player-tracked-since.ts +++ b/src/common/player/player-tracked-since.ts @@ -8,4 +8,9 @@ export interface PlayerTrackedSince { * The date the player was first tracked */ trackedSince?: string; + + /** + * The amount of days the player has been tracked + */ + daysTracked?: number; } diff --git a/src/common/schema/player-schema.ts b/src/common/schema/player-schema.ts index 3011dd6..57d2f03 100644 --- a/src/common/schema/player-schema.ts +++ b/src/common/schema/player-schema.ts @@ -18,7 +18,7 @@ export interface IPlayer extends Document { /** * The last time the player was tracked */ - lastTracked: Date; + ked: Date; /** * The raw player data. @@ -33,7 +33,7 @@ export interface IPlayer extends Document { /** * Gets when this player was last tracked. */ - getLastTracked(): Date; + getked(): Date; /** * Gets the history for the given date @@ -64,14 +64,14 @@ export interface IPlayer extends Document { // Mongoose Schema definition for Player const PlayerSchema = new Schema({ _id: { type: String, required: true }, - lastTracked: { type: Date, default: new Date(), required: false }, + ked: { type: Date, default: new Date(), required: false }, rawPlayer: { type: Object, required: false }, statisticHistory: { type: Map, default: () => new Map(), required: false }, trackedSince: { type: Date, default: new Date(), required: false }, }); -PlayerSchema.methods.getLastTracked = function (): Date { - return this.lastTracked || new Date(); +PlayerSchema.methods.getked = function (): Date { + return this.ked || new Date(); }; PlayerSchema.methods.getHistory = function (date: Date): PlayerHistory { diff --git a/src/components/player/player-data.tsx b/src/components/player/player-data.tsx index 60a0e16..8391f45 100644 --- a/src/components/player/player-data.tsx +++ b/src/components/player/player-data.tsx @@ -11,6 +11,7 @@ import PlayerScores from "./player-scores"; import ScoreSaberPlayer from "@/common/model/player/impl/scoresaber-player"; import Card from "@/components/card"; import PlayerBadges from "@/components/player/player-badges"; +import { useIsMobile } from "@/hooks/use-is-mobile"; const REFRESH_INTERVAL = 5 * 60 * 1000; // 5 minutes @@ -29,6 +30,8 @@ export default function PlayerData({ sort, page, }: Props) { + const isMobile = useIsMobile(); + let player = initalPlayerData; const { data, isLoading, isError } = useQuery({ queryKey: ["player", player.id], @@ -58,10 +61,12 @@ export default function PlayerData({ page={page} /> - + {!isMobile && ( + + )} ); } diff --git a/src/components/player/player-tracked-status.tsx b/src/components/player/player-tracked-status.tsx index 81bc091..2eed6f5 100644 --- a/src/components/player/player-tracked-status.tsx +++ b/src/components/player/player-tracked-status.tsx @@ -49,6 +49,7 @@ export default function PlayerTrackedStatus({ player }: Props) {

Tracked Since: {format(trackedSince)} ({daysAgoFormatted})

+

Days Tracked: {formatNumberWithCommas(data.daysTracked!)}

} side="bottom" diff --git a/src/hooks/use-is-mobile.ts b/src/hooks/use-is-mobile.ts new file mode 100644 index 0000000..d2b12ee --- /dev/null +++ b/src/hooks/use-is-mobile.ts @@ -0,0 +1,17 @@ +import { useState, useEffect } from "react"; + +export function useIsMobile() { + const [isMobile, setIsMobile] = useState(false); + + useEffect(() => { + const handleResize = () => { + setIsMobile(window.innerWidth < 768); + }; + window.addEventListener("resize", handleResize); + handleResize(); + + return () => window.removeEventListener("resize", handleResize); + }, []); + + return isMobile; +}