style: apply prettier formatting across codebase
This commit is contained in:
+64
-30
@@ -16,7 +16,10 @@ export function useBlitzGame() {
|
||||
const [timeLeft, setTimeLeft] = useState(BLITZ_DURATION);
|
||||
const [clicks, setClicks] = useState(0);
|
||||
const [history, setHistory] = useState<string[]>([]);
|
||||
const [puzzle, setPuzzle] = useState<{ start: string; target: string } | null>(null);
|
||||
const [puzzle, setPuzzle] = useState<{
|
||||
start: string;
|
||||
target: string;
|
||||
} | null>(null);
|
||||
|
||||
const clicksRef = useRef(0);
|
||||
const pathRef = useRef<string[]>([]);
|
||||
@@ -26,7 +29,10 @@ export function useBlitzGame() {
|
||||
const startTimeRef = useRef(0);
|
||||
|
||||
function stopTimer() {
|
||||
if (intervalRef.current) { clearInterval(intervalRef.current); intervalRef.current = null; }
|
||||
if (intervalRef.current) {
|
||||
clearInterval(intervalRef.current);
|
||||
intervalRef.current = null;
|
||||
}
|
||||
}
|
||||
|
||||
function startTimer() {
|
||||
@@ -46,7 +52,12 @@ export function useBlitzGame() {
|
||||
|
||||
useEffect(() => () => stopTimer(), []);
|
||||
|
||||
async function saveGame(won: boolean, path: string[], clicks: number, timeSeconds: number) {
|
||||
async function saveGame(
|
||||
won: boolean,
|
||||
path: string[],
|
||||
clicks: number,
|
||||
timeSeconds: number,
|
||||
) {
|
||||
if (!puzzle) return;
|
||||
fetch("/api/games", {
|
||||
method: "POST",
|
||||
@@ -70,7 +81,10 @@ export function useBlitzGame() {
|
||||
const art = await fetchArticle(t);
|
||||
setLoading(false);
|
||||
loadingRef.current = false;
|
||||
if (!art) { setLoadError(`Impossible de charger "${t}".`); return null; }
|
||||
if (!art) {
|
||||
setLoadError(`Impossible de charger "${t}".`);
|
||||
return null;
|
||||
}
|
||||
setHtml(art.html);
|
||||
setTitle(art.title);
|
||||
return art.title;
|
||||
@@ -79,8 +93,10 @@ export function useBlitzGame() {
|
||||
async function start() {
|
||||
setLoading(true);
|
||||
stopTimer();
|
||||
clicksRef.current = 0; setClicks(0);
|
||||
pathRef.current = []; setHistory([]);
|
||||
clicksRef.current = 0;
|
||||
setClicks(0);
|
||||
pathRef.current = [];
|
||||
setHistory([]);
|
||||
endedRef.current = false;
|
||||
setTimeLeft(BLITZ_DURATION);
|
||||
setLoadError(null);
|
||||
@@ -96,34 +112,43 @@ export function useBlitzGame() {
|
||||
startTimer();
|
||||
}
|
||||
|
||||
const navigate = useCallback(async (t: string) => {
|
||||
if (loadingRef.current || endedRef.current) return;
|
||||
clicksRef.current += 1;
|
||||
setClicks(clicksRef.current);
|
||||
const navigate = useCallback(
|
||||
async (t: string) => {
|
||||
if (loadingRef.current || endedRef.current) return;
|
||||
clicksRef.current += 1;
|
||||
setClicks(clicksRef.current);
|
||||
|
||||
const canonical = await loadArticle(t);
|
||||
if (!canonical) return;
|
||||
const canonical = await loadArticle(t);
|
||||
if (!canonical) return;
|
||||
|
||||
const newPath = [...pathRef.current, canonical];
|
||||
pathRef.current = newPath;
|
||||
setHistory(newPath);
|
||||
window.scrollTo({ top: 0, behavior: "smooth" });
|
||||
const newPath = [...pathRef.current, canonical];
|
||||
pathRef.current = newPath;
|
||||
setHistory(newPath);
|
||||
window.scrollTo({ top: 0, behavior: "smooth" });
|
||||
|
||||
if (puzzle && normalizeTitle(canonical) === normalizeTitle(puzzle.target)) {
|
||||
endedRef.current = true;
|
||||
stopTimer();
|
||||
setPhase("won");
|
||||
saveGame(true, newPath, clicksRef.current, timeLeft);
|
||||
}
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [puzzle, timeLeft]);
|
||||
if (
|
||||
puzzle &&
|
||||
normalizeTitle(canonical) === normalizeTitle(puzzle.target)
|
||||
) {
|
||||
endedRef.current = true;
|
||||
stopTimer();
|
||||
setPhase("won");
|
||||
saveGame(true, newPath, clicksRef.current, timeLeft);
|
||||
}
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
},
|
||||
[puzzle, timeLeft],
|
||||
);
|
||||
|
||||
function reset() {
|
||||
stopTimer();
|
||||
endedRef.current = false;
|
||||
clicksRef.current = 0; setClicks(0);
|
||||
pathRef.current = []; setHistory([]);
|
||||
setHtml(""); setTitle("");
|
||||
clicksRef.current = 0;
|
||||
setClicks(0);
|
||||
pathRef.current = [];
|
||||
setHistory([]);
|
||||
setHtml("");
|
||||
setTitle("");
|
||||
setTimeLeft(BLITZ_DURATION);
|
||||
setPuzzle(null);
|
||||
setPhase("setup");
|
||||
@@ -131,10 +156,19 @@ export function useBlitzGame() {
|
||||
}
|
||||
|
||||
return {
|
||||
phase, puzzle, html, title, loading, loadError,
|
||||
timeLeft, clicks, history,
|
||||
phase,
|
||||
puzzle,
|
||||
html,
|
||||
title,
|
||||
loading,
|
||||
loadError,
|
||||
timeLeft,
|
||||
clicks,
|
||||
history,
|
||||
canGoBack: false, // pas de retour en blitz
|
||||
start, navigate, reset,
|
||||
start,
|
||||
navigate,
|
||||
reset,
|
||||
retryLoad: () => title && loadArticle(title),
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user