added demo quiz reset option. Added timer for next available daily quiz

This commit is contained in:
masongga
2026-05-24 07:48:51 -06:00
parent 4998576e64
commit 1a151b5c7c
6 changed files with 199 additions and 7 deletions

View File

@@ -5,6 +5,11 @@ import {
type DailyQuizSubmission,
} from "@/lib/avatar-state";
import { notifyHabitPetDataUpdated } from "@/lib/app-events";
import {
getMillisecondsUntilLocalMidnight,
getNextLocalMidnight,
getTodayDateKey,
} from "@/lib/date-keys";
import { computeAvatarConditionWithHabitBoosts } from "@/lib/habit-wellness-effects";
import { getCompletedHabitLabelsForToday } from "@/lib/habits-storage";
import { validateJournalEntry } from "@/lib/journal-safety";
@@ -20,8 +25,45 @@ type DailyEntryRow = {
journal: string | null;
};
export function getTodayDateKey(date = new Date()) {
return date.toISOString().slice(0, 10);
export { getTodayDateKey };
export function getNextDailyQuizAvailableAt(from = new Date()) {
return getNextLocalMidnight(from);
}
export function getMillisecondsUntilNextDailyQuiz(from = new Date()) {
return getMillisecondsUntilLocalMidnight(from);
}
export function formatDailyQuizCountdown(milliseconds: number) {
const totalSeconds = Math.max(0, Math.floor(milliseconds / 1000));
const hours = Math.floor(totalSeconds / 3600);
const minutes = Math.floor((totalSeconds % 3600) / 60);
const seconds = totalSeconds % 60;
return `${hours}:${String(minutes).padStart(2, "0")}:${String(seconds).padStart(2, "0")}`;
}
export async function resetTodaysDailyQuiz() {
const userId = await getAuthenticatedUserId();
if (!userId) {
throw new Error("You must be signed in to reset today's quiz.");
}
const supabase = createClient();
const entryDate = getTodayDateKey();
const { error } = await supabase
.from("daily_entries")
.delete()
.eq("user_id", userId)
.eq("entry_date", entryDate);
if (error) {
throw new Error(error.message);
}
notifyHabitPetDataUpdated();
}
function mapRowToSubmission(

19
lib/date-keys.ts Normal file
View File

@@ -0,0 +1,19 @@
export function getTodayDateKey(date = new Date()) {
return date.toLocaleDateString("en-CA");
}
export function getNextLocalMidnight(from = new Date()) {
return new Date(
from.getFullYear(),
from.getMonth(),
from.getDate() + 1,
0,
0,
0,
0,
);
}
export function getMillisecondsUntilLocalMidnight(from = new Date()) {
return Math.max(0, getNextLocalMidnight(from).getTime() - from.getTime());
}

View File

@@ -16,6 +16,9 @@ import {
getStreakMilestoneBonus,
} from "@/lib/coins";
import { createClient } from "@/lib/supabase/client";
import { getTodayDateKey } from "@/lib/date-keys";
export { getTodayDateKey };
export type Habit = {
id: string;
@@ -43,10 +46,6 @@ type HabitRow = {
const habitSelectFields =
"id, name, streak, last_completed_on, claimed_streak_milestones";
export function getTodayDateKey(date = new Date()) {
return date.toISOString().slice(0, 10);
}
function getDayDiff(fromDate: string, toDate: string) {
const from = new Date(`${fromDate}T00:00:00`);
const to = new Date(`${toDate}T00:00:00`);