added demo quiz reset option. Added timer for next available daily quiz
This commit is contained in:
67
components/daily-quiz/daily-quiz-countdown.tsx
Normal file
67
components/daily-quiz/daily-quiz-countdown.tsx
Normal file
@@ -0,0 +1,67 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useRef, useState } from "react";
|
||||
|
||||
import {
|
||||
formatDailyQuizCountdown,
|
||||
getMillisecondsUntilNextDailyQuiz,
|
||||
getNextDailyQuizAvailableAt,
|
||||
} from "@/lib/daily-quiz-storage";
|
||||
|
||||
type DailyQuizCountdownProps = {
|
||||
onAvailable?: () => void;
|
||||
};
|
||||
|
||||
export function DailyQuizCountdown({ onAvailable }: DailyQuizCountdownProps) {
|
||||
const [remainingMs, setRemainingMs] = useState(() =>
|
||||
getMillisecondsUntilNextDailyQuiz(),
|
||||
);
|
||||
const hasNotifiedRef = useRef(false);
|
||||
|
||||
useEffect(() => {
|
||||
const tick = () => {
|
||||
const nextRemainingMs = getMillisecondsUntilNextDailyQuiz();
|
||||
setRemainingMs(nextRemainingMs);
|
||||
|
||||
if (nextRemainingMs === 0) {
|
||||
if (!hasNotifiedRef.current) {
|
||||
hasNotifiedRef.current = true;
|
||||
onAvailable?.();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
hasNotifiedRef.current = false;
|
||||
};
|
||||
|
||||
tick();
|
||||
const intervalId = window.setInterval(tick, 1000);
|
||||
|
||||
return () => {
|
||||
window.clearInterval(intervalId);
|
||||
};
|
||||
}, [onAvailable]);
|
||||
|
||||
const nextAvailableAt = getNextDailyQuizAvailableAt();
|
||||
const nextAvailableLabel = nextAvailableAt.toLocaleString(undefined, {
|
||||
weekday: "short",
|
||||
month: "short",
|
||||
day: "numeric",
|
||||
hour: "numeric",
|
||||
minute: "2-digit",
|
||||
});
|
||||
|
||||
return (
|
||||
<div className="space-y-1 pt-1">
|
||||
<p className="text-sm text-muted-foreground">
|
||||
Next check-in available in{" "}
|
||||
<span className="font-medium tabular-nums text-foreground">
|
||||
{formatDailyQuizCountdown(remainingMs)}
|
||||
</span>
|
||||
</p>
|
||||
<p className="text-xs text-muted-foreground">
|
||||
Opens at midnight ({nextAvailableLabel})
|
||||
</p>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import Link from "next/link";
|
||||
import { useCallback, useEffect, useState } from "react";
|
||||
|
||||
import { WellnessSlider } from "@/components/daily-quiz/wellness-slider";
|
||||
import { DailyQuizCountdown } from "@/components/daily-quiz/daily-quiz-countdown";
|
||||
import { Badge } from "@/components/ui/badge";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import {
|
||||
@@ -28,6 +29,7 @@ import {
|
||||
getCachedQuizTabData,
|
||||
prefetchQuizTabData,
|
||||
} from "@/lib/app-tab-data-cache";
|
||||
import { HABIT_PET_DATA_UPDATED_EVENT } from "@/lib/app-events";
|
||||
import { saveDailyEntry } from "@/lib/daily-quiz-storage";
|
||||
import { JOURNAL_MAX_LENGTH } from "@/lib/journal-safety";
|
||||
import { routes } from "@/lib/routes";
|
||||
@@ -63,7 +65,7 @@ export function DailyQuizForm() {
|
||||
|
||||
async function loadDailyCheckIn() {
|
||||
try {
|
||||
const data = await prefetchQuizTabData();
|
||||
const data = await prefetchQuizTabData({ force: true });
|
||||
|
||||
if (cancelled) {
|
||||
return;
|
||||
@@ -89,8 +91,15 @@ export function DailyQuizForm() {
|
||||
|
||||
void loadDailyCheckIn();
|
||||
|
||||
const handleDataUpdated = () => {
|
||||
void loadDailyCheckIn();
|
||||
};
|
||||
|
||||
window.addEventListener(HABIT_PET_DATA_UPDATED_EVENT, handleDataUpdated);
|
||||
|
||||
return () => {
|
||||
cancelled = true;
|
||||
window.removeEventListener(HABIT_PET_DATA_UPDATED_EVENT, handleDataUpdated);
|
||||
};
|
||||
}, [applyQuizData]);
|
||||
|
||||
@@ -161,6 +170,11 @@ export function DailyQuizForm() {
|
||||
</Link>{" "}
|
||||
whenever you want to see how they are doing.
|
||||
</p>
|
||||
<DailyQuizCountdown
|
||||
onAvailable={() => {
|
||||
void prefetchQuizTabData({ force: true }).then(applyQuizData);
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
Reference in New Issue
Block a user