Repository navigation
Show the 00:00 UTC reset on the day leaderboard and the free spin (#1844) #1998
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
53d847c
Specs for #1844: UTC day reset label, leaderboard refetch on mount, s…
feruzm 080befc
Show the 00:00 UTC reset on the day leaderboard and the free spin, re…
feruzm a21d192
Specs for #1844 review: no time in server HTML, reload the board when…
feruzm bc51d02
Render the reset countdown in the browser only, reload the board when…
feruzm 53bcb43
Specs for #1844 review round 2: reload the spin status at 00:00 UTC; …
feruzm c0d7b13
Reload the spin status at 00:00 UTC through a shared UTC clock hook (…
feruzm e6bd5f3
Specs for #1844 review round 3: reset fires at 00:00:00, reload on re…
feruzm af23928
Land the UTC clock on minute boundaries, reload the day board when co…
feruzm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| "use client"; | ||
|
|
||
| import i18next from "i18next"; | ||
| import { useUtcClock } from "@/core/hooks/use-utc-clock"; | ||
|
|
||
| const DAY_MS = 24 * 60 * 60 * 1000; | ||
|
|
||
| /** | ||
| * Hours and minutes until the next 00:00 UTC, when the day leaderboard, the daily | ||
| * quests and the free spin reset. A part minute rounds up, so the last minute of the | ||
| * day reads "0h 1m", never "0h 0m". | ||
| */ | ||
| export function timeUntilUtcReset(now: number): { hours: number; minutes: number } { | ||
| const left = DAY_MS - (now % DAY_MS); | ||
| const totalMinutes = Math.ceil(left / 60000); | ||
| return { hours: Math.floor(totalMinutes / 60), minutes: totalMinutes % 60 }; | ||
| } | ||
|
|
||
| interface Props { | ||
| /** Called once when the UTC day changes while the label is on screen. */ | ||
| onDayChange?: () => void; | ||
| } | ||
|
|
||
| export function DayResetLabel({ onDayChange }: Props) { | ||
| const now = useUtcClock(onDayChange); | ||
|
|
||
| if (now === null) { | ||
| return null; | ||
| } | ||
|
|
||
| return ( | ||
| <div className="text-xs opacity-50"> | ||
| {i18next.t("leaderboard.day-resets-in", timeUntilUtcReset(now))} | ||
| </div> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| "use client"; | ||
|
|
||
| import { useEffect, useRef, useState } from "react"; | ||
|
|
||
| const MINUTE_MS = 60 * 1000; | ||
| const DAY_MS = 24 * 60 * MINUTE_MS; | ||
|
|
||
| /** | ||
| * The time, updated on every UTC minute boundary, for anything tied to the 00:00 UTC | ||
| * reset (day leaderboard, daily quests, free spin). `onDayChange` runs once when the UTC day | ||
| * changes while the component is mounted, so it can reload what reset. | ||
| * | ||
| * Null until mounted: server HTML can be served later from a cache, so whatever | ||
| * renders a time from this renders it from the browser's clock only. | ||
| */ | ||
| export function useUtcClock(onDayChange?: () => void): number | null { | ||
| const [now, setNow] = useState<number | null>(null); | ||
| const onDayChangeRef = useRef(onDayChange); | ||
|
|
||
| useEffect(() => { | ||
| onDayChangeRef.current = onDayChange; | ||
| }, [onDayChange]); | ||
|
|
||
| useEffect(() => { | ||
| let day: number | null = null; | ||
| const tick = () => { | ||
| const t = Date.now(); | ||
| const today = Math.floor(t / DAY_MS); | ||
| if (day !== null && today !== day) { | ||
| onDayChangeRef.current?.(); | ||
| } | ||
| day = today; | ||
| setNow(t); | ||
| }; | ||
| // Each tick lands on the next minute boundary, not 60 s after mount, so the | ||
| // reset is seen at 00:00:00 UTC however far into a minute the page opened. | ||
| let id: ReturnType<typeof setTimeout>; | ||
| const schedule = () => { | ||
| id = setTimeout(() => { | ||
| tick(); | ||
| schedule(); | ||
| }, MINUTE_MS - (Date.now() % MINUTE_MS)); | ||
| }; | ||
| tick(); | ||
| schedule(); | ||
| return () => clearTimeout(id); | ||
| }, []); | ||
|
|
||
| return now; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
apps/web/src/specs/app/discover/day-reset-label.spec.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; | ||
| import { act, render, screen } from "@testing-library/react"; | ||
| import "@testing-library/jest-dom"; | ||
| import i18next from "i18next"; | ||
| import { renderToString } from "react-dom/server"; | ||
| import { DayResetLabel, timeUntilUtcReset } from "@/app/discover/_components/day-reset-label"; | ||
|
|
||
| const at = (iso: string) => new Date(iso).getTime(); | ||
|
|
||
| describe("timeUntilUtcReset", () => { | ||
| it("counts down to the next 00:00 UTC", () => { | ||
| expect(timeUntilUtcReset(at("2026-10-30T18:48:00Z"))).toEqual({ hours: 5, minutes: 12 }); | ||
| }); | ||
|
|
||
| it("rounds a part minute up, so the last minute never reads 0h 0m", () => { | ||
| expect(timeUntilUtcReset(at("2026-10-30T23:59:30Z"))).toEqual({ hours: 0, minutes: 1 }); | ||
| }); | ||
|
|
||
| it("is a full day right at the reset", () => { | ||
| expect(timeUntilUtcReset(at("2026-10-30T00:00:00Z"))).toEqual({ hours: 24, minutes: 0 }); | ||
| }); | ||
|
|
||
| it("does not depend on the viewer's time zone", () => { | ||
| // 01:30 in Berlin on 10-31 is still 23:30 UTC on 10-30 | ||
| expect(timeUntilUtcReset(at("2026-10-31T01:30:00+02:00"))).toEqual({ hours: 0, minutes: 30 }); | ||
| }); | ||
| }); | ||
|
|
||
| describe("DayResetLabel", () => { | ||
| beforeEach(() => { | ||
| vi.useFakeTimers(); | ||
| vi.mocked(i18next.t).mockClear(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.useRealTimers(); | ||
| }); | ||
|
|
||
| it("shows the UTC day and the time left", () => { | ||
| vi.setSystemTime(new Date("2026-10-30T18:48:00Z")); | ||
| render(<DayResetLabel />); | ||
|
|
||
| expect(screen.getByText("leaderboard.day-resets-in")).toBeInTheDocument(); | ||
| expect(i18next.t).toHaveBeenCalledWith("leaderboard.day-resets-in", { hours: 5, minutes: 12 }); | ||
| }); | ||
|
|
||
| it("keeps counting while the page stays open", () => { | ||
| vi.setSystemTime(new Date("2026-10-30T18:48:00Z")); | ||
| render(<DayResetLabel />); | ||
|
|
||
| act(() => { | ||
| vi.advanceTimersByTime(60 * 60 * 1000); | ||
| }); | ||
|
|
||
| expect(i18next.t).toHaveBeenLastCalledWith("leaderboard.day-resets-in", { hours: 4, minutes: 12 }); | ||
| }); | ||
|
|
||
| it("puts no time in the server HTML, which can be served later from a cache", () => { | ||
| vi.setSystemTime(new Date("2026-10-30T18:48:00Z")); | ||
| const html = renderToString(<DayResetLabel />); | ||
|
|
||
| expect(html).not.toContain("leaderboard.day-resets-in"); | ||
| expect(i18next.t).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("tells the page once when the UTC day changes, so it can load the new board", () => { | ||
| vi.setSystemTime(new Date("2026-10-30T23:58:30Z")); | ||
| const onDayChange = vi.fn(); | ||
| render(<DayResetLabel onDayChange={onDayChange} />); | ||
|
|
||
| act(() => { | ||
| vi.advanceTimersByTime(60 * 1000); | ||
| }); | ||
| expect(onDayChange).not.toHaveBeenCalled(); | ||
|
|
||
| act(() => { | ||
| vi.advanceTimersByTime(60 * 1000); | ||
| }); | ||
| expect(onDayChange).toHaveBeenCalledTimes(1); | ||
|
|
||
| act(() => { | ||
| vi.advanceTimersByTime(10 * 60 * 1000); | ||
| }); | ||
| expect(onDayChange).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it("fires the day change at 00:00:00 UTC, not up to a minute later", () => { | ||
| vi.setSystemTime(new Date("2026-10-30T23:59:30Z")); | ||
| const onDayChange = vi.fn(); | ||
| render(<DayResetLabel onDayChange={onDayChange} />); | ||
|
|
||
| act(() => { | ||
| vi.advanceTimersByTime(29_999); | ||
| }); | ||
| expect(onDayChange).not.toHaveBeenCalled(); | ||
|
|
||
| act(() => { | ||
| vi.advanceTimersByTime(1); | ||
| }); | ||
| expect(onDayChange).toHaveBeenCalledTimes(1); | ||
| expect(i18next.t).toHaveBeenLastCalledWith("leaderboard.day-resets-in", { hours: 24, minutes: 0 }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.