diff --git a/client/app/components/sidebar/Sidebar.tsx b/client/app/components/sidebar/Sidebar.tsx index fb575cf..15ac8b5 100644 --- a/client/app/components/sidebar/Sidebar.tsx +++ b/client/app/components/sidebar/Sidebar.tsx @@ -2,6 +2,7 @@ import { ExternalLink, History, Home, Info } from "lucide-react"; import SidebarSearch from "./SidebarSearch"; import SidebarItem from "./SidebarItem"; import SidebarSettings from "./SidebarSettings"; +import { getRewindYear } from "~/utils/utils"; export default function Sidebar() { const iconSize = 20; @@ -44,7 +45,7 @@ export default function Sidebar() { {}} modal={<>} diff --git a/client/app/routes/RewindPage.tsx b/client/app/routes/RewindPage.tsx index 1951caa..b14e5fc 100644 --- a/client/app/routes/RewindPage.tsx +++ b/client/app/routes/RewindPage.tsx @@ -1,13 +1,14 @@ import Rewind from "~/components/rewind/Rewind"; import type { Route } from "./+types/Home"; -import { getRewindStats, type RewindStats } from "api/api"; -import { useEffect, useState } from "react"; +import { type RewindStats } from "api/api"; +import { useState } from "react"; import type { LoaderFunctionArgs } from "react-router"; import { useLoaderData } from "react-router"; +import { getRewindYear } from "~/utils/utils"; export async function clientLoader({ request }: LoaderFunctionArgs) { const url = new URL(request.url); - const year = url.searchParams.get("year") || new Date().getFullYear(); + const year = url.searchParams.get("year") || getRewindYear(); const res = await fetch(`/apis/web/v1/summary?year=${year}`); if (!res.ok) { diff --git a/client/app/utils/utils.ts b/client/app/utils/utils.ts index 2c82bd8..50c0c16 100644 --- a/client/app/utils/utils.ts +++ b/client/app/utils/utils.ts @@ -15,6 +15,16 @@ const timeframeToInterval = (timeframe: Timeframe): string => { } }; +const getRewindYear = (): number => { + const today = new Date(); + if (today.getMonth() > 10 && today.getDate() >= 30) { + // if we are in december 30/31, just serve current year + return today.getFullYear(); + } else { + return today.getFullYear() - 1; + } +}; + function timeSince(date: Date) { const now = new Date(); const seconds = Math.floor((now.getTime() - date.getTime()) / 1000); @@ -104,5 +114,5 @@ const timeListenedString = (seconds: number) => { return `${minutes} minutes listened`; }; -export { hexToHSL, timeListenedString }; +export { hexToHSL, timeListenedString, getRewindYear }; export type { hsl };