93 lines
3.4 KiB
JavaScript
93 lines
3.4 KiB
JavaScript
// Rôle : charge et permet de relancer séparément le contenu éditorial et les données publiques.
|
|
import { useCallback, useEffect, useState } from "react";
|
|
import { INITIAL_ANIIMO_STATE, loadAniimoData, INITIAL_DIABLO4_STATE, INITIAL_MHWILDS_STATE, loadDiablo4Data, loadMhwildsData } from "../features/games/loaders.js";
|
|
|
|
function useJsonResource(url, initialValue, errorMessage, normalize = (value) => value) {
|
|
const [data, setData] = useState(initialValue);
|
|
const [error, setError] = useState("");
|
|
const [loading, setLoading] = useState(true);
|
|
const [attempt, setAttempt] = useState(0);
|
|
|
|
const retry = useCallback(() => {
|
|
setError("");
|
|
setLoading(true);
|
|
setAttempt((current) => current + 1);
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
const controller = new AbortController();
|
|
const { signal } = controller;
|
|
fetch(url, { signal })
|
|
.then((response) => {
|
|
if (!response.ok) throw new Error(errorMessage);
|
|
return response.json();
|
|
})
|
|
.then((payload) => {
|
|
if (signal.aborted) return;
|
|
setData(normalize(payload));
|
|
setError("");
|
|
setLoading(false);
|
|
})
|
|
.catch(() => {
|
|
if (signal.aborted) return;
|
|
setError(errorMessage);
|
|
setLoading(false);
|
|
});
|
|
return () => controller.abort();
|
|
}, [attempt, errorMessage, normalize, url]);
|
|
|
|
return { data, error, loading, retry };
|
|
}
|
|
|
|
const normalizeSiteContent = (payload) => payload;
|
|
const normalizeGames = (payload) => Array.isArray(payload?.games) ? payload.games : [];
|
|
|
|
function useGameData(active, initialState, load) {
|
|
const [requested, setRequested] = useState(active);
|
|
const [data, setData] = useState(initialState);
|
|
const [attempt, setAttempt] = useState(0);
|
|
if (active && !requested) setRequested(true);
|
|
|
|
const retry = useCallback(() => {
|
|
setData((current) => ({ ...current, loading: true, error: "" }));
|
|
setAttempt((current) => current + 1);
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
if (!requested) return undefined;
|
|
const controller = new AbortController();
|
|
load(controller.signal)
|
|
.then((result) => {
|
|
if (!controller.signal.aborted) setData(result);
|
|
})
|
|
.catch((error) => {
|
|
if (!controller.signal.aborted) setData((current) => ({ ...current, loaded: true, loading: false, error: error.message }));
|
|
});
|
|
return () => controller.abort();
|
|
}, [attempt, requested, load]);
|
|
|
|
// Une visite déclenche un seul chargement, conservé même si la route change.
|
|
return { ...data, loading: requested && (!data.loaded || data.loading), retry };
|
|
}
|
|
|
|
export function useAppData(route) {
|
|
const site = useJsonResource("/data/site.json", null, "Impossible de charger le contenu du site.", normalizeSiteContent);
|
|
const gameList = useJsonResource("/data/games.json", [], "Impossible de charger la liste des jeux.", normalizeGames);
|
|
const mhwilds = useGameData(route.startsWith("/games/mhwilds"), INITIAL_MHWILDS_STATE, loadMhwildsData);
|
|
const aniimo = useGameData(route.startsWith("/games/aniimo"), INITIAL_ANIIMO_STATE, loadAniimoData);
|
|
const diablo4 = useGameData(route.startsWith("/games/diablo4"), INITIAL_DIABLO4_STATE, loadDiablo4Data);
|
|
|
|
return {
|
|
siteContent: site.data,
|
|
siteContentError: site.error,
|
|
siteContentLoading: site.loading,
|
|
retrySiteContent: site.retry,
|
|
games: gameList.data,
|
|
gamesError: gameList.error,
|
|
gamesLoading: gameList.loading,
|
|
retryGames: gameList.retry,
|
|
mhwilds,
|
|
diablo4,
|
|
aniimo
|
|
};
|
|
}
|