From fe9a191a008b2f5e4d8fece1b36d6bd3b1f068f2 Mon Sep 17 00:00:00 2001 From: Fuwn Date: Wed, 6 Sep 2023 03:54:07 -0700 Subject: fix(activity): last activity check --- src/lib/AniList/activity.ts | 28 +++++++++++++++------------- src/routes/+layout.svelte | 30 ++++++++++++++++++++++++++++-- 2 files changed, 43 insertions(+), 15 deletions(-) (limited to 'src') diff --git a/src/lib/AniList/activity.ts b/src/lib/AniList/activity.ts index 995ff6eb..aa22a590 100644 --- a/src/lib/AniList/activity.ts +++ b/src/lib/AniList/activity.ts @@ -1,23 +1,25 @@ import type { UserIdentity } from './identity'; export const lastActivityDate = async (userIdentity: UserIdentity): Promise => { - const activityHistory = await ( - await fetch('https://graphql.anilist.co', { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - Accept: 'application/json' - }, - body: JSON.stringify({ - query: `{ Activity(userId: ${userIdentity.id}, type: MEDIA_LIST, sort: ID_DESC) { - __typename ... on ListActivity { createdAt } + const activityHistory = ( + await ( + await fetch('https://graphql.anilist.co', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + Accept: 'application/json' + }, + body: JSON.stringify({ + query: `{ User(id: ${userIdentity.id}) { + stats { activityHistory { date } } } }` + }) }) - }) - ).json(); + ).json() + )['data']['User']['stats']['activityHistory']; const date = new Date(0); - date.setUTCSeconds(activityHistory['data']['Activity']['createdAt']); + date.setUTCSeconds(activityHistory[activityHistory.length - 1]['date']); return date; }; diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte index a11ea3ee..15073db3 100644 --- a/src/routes/+layout.svelte +++ b/src/routes/+layout.svelte @@ -28,9 +28,32 @@ currentUserIdentity = JSON.parse($userIdentity); currentUserIdentity.name = currentUserIdentity.name; lastActivityWasToday = - (await lastActivityDate(currentUserIdentity)).toDateString() === new Date().toDateString(); + (await lastActivityDate(currentUserIdentity)).toDateString() >= new Date().toDateString(); } }); + + const timeLeftToday = () => { + const now = new Date(); + const currentHour = now.getHours(); + const currentMinute = now.getMinutes(); + const hoursLeft = 24 - currentHour; + let minutesLeft = 0; + let timeLeft = ''; + + if (hoursLeft > 0) { + minutesLeft = hoursLeft * 60 - currentMinute; + } else { + minutesLeft = 24 * 60 - (currentHour * 60 + currentMinute); + } + + if (minutesLeft > 60) { + timeLeft = `${Math.round(minutesLeft / 60)} hours`; + } else { + timeLeft = `${minutesLeft} minutes`; + } + + return timeLeft; + };

@@ -49,7 +72,10 @@ {#if !lastActivityWasToday}

-

You don't have any new activity statuses from the past day! Create one to keep your streak!

+

+ You don't have any new activity statuses from the past day! Create one within {timeLeftToday()} + to keep your streak! +

{/if}

-- cgit v1.2.3