Add recovery for public data loading failures
This commit is contained in:
parent
1171eee510
commit
daa5eba8bd
14 changed files with 186 additions and 58 deletions
|
|
@ -1,5 +1,5 @@
|
|||
// Rôle : liste les jeux déclarés dans le contenu éditable.
|
||||
export function GamesPage({ siteContent, games, gamesError }) {
|
||||
// Rôle : liste les jeux déclarés et permet de reprendre le chargement de leur catalogue.
|
||||
export function GamesPage({ siteContent, games, gamesError, gamesLoading, retryGames }) {
|
||||
const content = siteContent.gamesPage;
|
||||
|
||||
return (
|
||||
|
|
@ -12,6 +12,10 @@ export function GamesPage({ siteContent, games, gamesError }) {
|
|||
</div>
|
||||
</section>
|
||||
<section className="game-list">
|
||||
{gamesError && games.length > 0 && <div className="nds-inline-notice" data-variant="warning" role="alert">
|
||||
<span>{gamesError}</span>
|
||||
<button className="nds-button" data-size="sm" type="button" onClick={retryGames}>{content.retryLabel}</button>
|
||||
</div>}
|
||||
{games.length ? games.map((game) => (
|
||||
<article className="card nds-card game-card" data-variant="interactive" key={game.id}>
|
||||
<a
|
||||
|
|
@ -37,9 +41,10 @@ export function GamesPage({ siteContent, games, gamesError }) {
|
|||
</div>
|
||||
</article>
|
||||
)) : (
|
||||
<div className="empty nds-empty-state">
|
||||
<h2>{content.emptyTitle}</h2>
|
||||
<p>{gamesError || content.emptyText}</p>
|
||||
<div className="empty nds-empty-state" aria-busy={gamesLoading}>
|
||||
<h2>{gamesLoading ? content.loadingTitle : content.emptyTitle}</h2>
|
||||
<p>{gamesLoading ? content.loadingText : gamesError || content.emptyText}</p>
|
||||
{gamesError && <button className="nds-button" data-size="sm" data-variant="primary" type="button" onClick={retryGames}>{content.retryLabel}</button>}
|
||||
</div>
|
||||
)}
|
||||
</section>
|
||||
|
|
|
|||
|
|
@ -57,8 +57,8 @@ function AniimoListing({ game, aniimo, filters, setFilters, content }) {
|
|||
export function AniimoPage({ category, game, aniimo, filters, setFilters, siteContent, links, actions }) {
|
||||
const content = siteContent.aniimo;
|
||||
if (category === "lists") {
|
||||
if (!aniimo.loaded) return <section className="empty nds-empty-state" aria-busy="true"><h1>{content.loading}</h1></section>;
|
||||
if (aniimo.error) return <section className="empty nds-empty-state" role="alert"><h1>{content.error}</h1><p>{aniimo.error}</p></section>;
|
||||
if (aniimo.loading || !aniimo.loaded) return <section className="empty nds-empty-state" aria-busy="true"><h1>{content.loading}</h1></section>;
|
||||
if (aniimo.error) return <section className="empty nds-empty-state" role="alert"><h1>{content.error}</h1><p>{aniimo.error}</p><button className="nds-button" data-size="sm" data-variant="primary" type="button" onClick={aniimo.retry}>{siteContent.gamesPage.retryLabel}</button></section>;
|
||||
return <AniimoLists game={game} aniimo={aniimo} linkedToolboxId={links?.[game.id] || ""} actions={actions} />;
|
||||
}
|
||||
if (category !== "aniimos") return <>
|
||||
|
|
@ -68,7 +68,7 @@ export function AniimoPage({ category, game, aniimo, filters, setFilters, siteCo
|
|||
<a className="feature game-home-card" href="#/games/aniimo/lists"><span className="game-home-card-icon" aria-hidden="true"><span className="module-icon-svg module-icon-checklist" /></span><strong>Listes</strong><span>Données copiables au format checklist pour préparer vos suivis.</span></a>
|
||||
</section>
|
||||
</>;
|
||||
if (!aniimo.loaded) return <section className="empty nds-empty-state" aria-busy="true"><h1>{content.loading}</h1></section>;
|
||||
if (aniimo.error) return <section className="empty nds-empty-state" role="alert"><h1>{content.error}</h1><p>{aniimo.error}</p></section>;
|
||||
if (aniimo.loading || !aniimo.loaded) return <section className="empty nds-empty-state" aria-busy="true"><h1>{content.loading}</h1></section>;
|
||||
if (aniimo.error) return <section className="empty nds-empty-state" role="alert"><h1>{content.error}</h1><p>{aniimo.error}</p><button className="nds-button" data-size="sm" data-variant="primary" type="button" onClick={aniimo.retry}>{siteContent.gamesPage.retryLabel}</button></section>;
|
||||
return <AniimoListing game={game} aniimo={aniimo} filters={filters} setFilters={setFilters} content={content} />;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,11 +2,11 @@
|
|||
import { Diablo4Listing } from "./Diablo4Listing.jsx";
|
||||
import { Diablo4Overview } from "./Diablo4Overview.jsx";
|
||||
|
||||
export function Diablo4Page({ category, game, diablo4, filters, setFilters }) {
|
||||
export function Diablo4Page({ category, game, diablo4, filters, setFilters, siteContent }) {
|
||||
const activeCategory = category === "affixes" ? category : "";
|
||||
const heroStyle = game?.images?.heroBg ? { "--game-hero-image": `url("${game.images.heroBg}")` } : undefined;
|
||||
|
||||
if (!diablo4.loaded) {
|
||||
if (diablo4.loading || !diablo4.loaded) {
|
||||
return (
|
||||
<>
|
||||
<section className="game-hero" style={heroStyle}>
|
||||
|
|
@ -26,9 +26,10 @@ export function Diablo4Page({ category, game, diablo4, filters, setFilters }) {
|
|||
|
||||
if (diablo4.error) {
|
||||
return (
|
||||
<section className="empty nds-empty-state">
|
||||
<section className="empty nds-empty-state" role="alert">
|
||||
<h1>Impossible de charger Diablo IV</h1>
|
||||
<p>{diablo4.error}</p>
|
||||
<button className="nds-button" data-size="sm" data-variant="primary" type="button" onClick={diablo4.retry}>{siteContent.gamesPage.retryLabel}</button>
|
||||
</section>
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,11 +3,11 @@ import { MhwildsListing } from "./MhwildsListing.jsx";
|
|||
import { MhwildsLists } from "./MhwildsLists.jsx";
|
||||
import { MhwildsOverview } from "./MhwildsOverview.jsx";
|
||||
|
||||
export function MhwildsPage({ category, game, mhwilds, filters, setFilters, t, links, actions }) {
|
||||
export function MhwildsPage({ category, game, mhwilds, filters, setFilters, t, links, actions, siteContent }) {
|
||||
const activeCategory = category === "monsters" || category === "endemic" || category === "lists" ? category : "";
|
||||
const heroStyle = game?.images?.heroBg ? { "--game-hero-image": `url("${game.images.heroBg}")` } : undefined;
|
||||
|
||||
if (!mhwilds.loaded) {
|
||||
if (mhwilds.loading || !mhwilds.loaded) {
|
||||
return (
|
||||
<>
|
||||
<section className="game-hero" style={heroStyle}>
|
||||
|
|
@ -27,9 +27,10 @@ export function MhwildsPage({ category, game, mhwilds, filters, setFilters, t, l
|
|||
|
||||
if (mhwilds.error) {
|
||||
return (
|
||||
<section className="empty nds-empty-state">
|
||||
<section className="empty nds-empty-state" role="alert">
|
||||
<h1>Impossible de charger MH Wilds</h1>
|
||||
<p>{mhwilds.error}</p>
|
||||
<button className="nds-button" data-size="sm" data-variant="primary" type="button" onClick={mhwilds.retry}>{siteContent.gamesPage.retryLabel}</button>
|
||||
</section>
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,12 +1,58 @@
|
|||
// Rôle : charge le contenu éditorial et les données publiques nécessaires à l'application.
|
||||
import { useEffect, useState } from "react";
|
||||
// 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();
|
||||
|
|
@ -15,55 +61,31 @@ function useGameData(active, initialState, load) {
|
|||
if (!controller.signal.aborted) setData(result);
|
||||
})
|
||||
.catch((error) => {
|
||||
if (!controller.signal.aborted) setData({ ...initialState, loaded: true, error: error.message });
|
||||
if (!controller.signal.aborted) setData((current) => ({ ...current, loaded: true, loading: false, error: error.message }));
|
||||
});
|
||||
return () => controller.abort();
|
||||
}, [requested, initialState, load]);
|
||||
}, [attempt, requested, load]);
|
||||
|
||||
// Une visite déclenche un seul chargement, conservé même si la route change.
|
||||
return requested && !data.loaded ? { ...data, loading: true } : data;
|
||||
return { ...data, loading: requested && (!data.loaded || data.loading), retry };
|
||||
}
|
||||
|
||||
export function useAppData(route) {
|
||||
const [siteContent, setSiteContent] = useState(null);
|
||||
const [siteContentError, setSiteContentError] = useState("");
|
||||
const [games, setGames] = useState([]);
|
||||
const [gamesError, setGamesError] = useState("");
|
||||
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);
|
||||
|
||||
useEffect(() => {
|
||||
const controller = new AbortController();
|
||||
const { signal } = controller;
|
||||
Promise.all([
|
||||
fetch("/data/site.json", { signal }).then((response) => {
|
||||
if (!response.ok) throw new Error("Impossible de charger le contenu du site.");
|
||||
return response.json();
|
||||
}).catch((error) => {
|
||||
if (!signal.aborted) setSiteContentError(error.message);
|
||||
return null;
|
||||
}),
|
||||
fetch("/data/games.json", { signal }).then((response) => {
|
||||
if (!response.ok) throw new Error("Impossible de charger la liste des jeux.");
|
||||
return response.json();
|
||||
}).catch((error) => {
|
||||
if (!signal.aborted) setGamesError(error.message);
|
||||
return { games: [] };
|
||||
})
|
||||
]).then(([content, gamesPayload]) => {
|
||||
if (signal.aborted) return;
|
||||
if (content) setSiteContent(content);
|
||||
setGames(Array.isArray(gamesPayload.games) ? gamesPayload.games : []);
|
||||
});
|
||||
return () => controller.abort();
|
||||
}, []);
|
||||
|
||||
return {
|
||||
siteContent,
|
||||
siteContentError,
|
||||
games,
|
||||
gamesError,
|
||||
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
|
||||
|
|
|
|||
|
|
@ -44,7 +44,19 @@ function App() {
|
|||
const route = useHashRoute();
|
||||
const compactGameLayout = useMediaQuery("(max-width: 1439px)");
|
||||
const reducedSidebar = useMediaQuery("(max-width: 1024px)");
|
||||
const { siteContent, siteContentError, games, gamesError, mhwilds, diablo4, aniimo } = useAppData(route);
|
||||
const {
|
||||
siteContent,
|
||||
siteContentError,
|
||||
siteContentLoading,
|
||||
retrySiteContent,
|
||||
games,
|
||||
gamesError,
|
||||
gamesLoading,
|
||||
retryGames,
|
||||
mhwilds,
|
||||
diablo4,
|
||||
aniimo
|
||||
} = useAppData(route);
|
||||
const [filters, setFilters] = useGameFilters();
|
||||
const [confirmModal, setConfirmModal] = useState(null);
|
||||
const [createModal, setCreateModal] = useState(null);
|
||||
|
|
@ -170,9 +182,12 @@ function App() {
|
|||
if (!siteContent) {
|
||||
return (
|
||||
<main className="startup-error">
|
||||
<section className="empty nds-empty-state">
|
||||
<section className="empty nds-empty-state" aria-busy={siteContentLoading}>
|
||||
<h1>Contenu indisponible</h1>
|
||||
<p>{siteContentError || "Chargement du contenu..."}</p>
|
||||
{siteContentError && <button className="nds-button" data-size="sm" data-variant="primary" type="button" onClick={retrySiteContent}>
|
||||
<Icon name="refresh" />Réessayer
|
||||
</button>}
|
||||
</section>
|
||||
</main>
|
||||
);
|
||||
|
|
@ -216,6 +231,8 @@ function App() {
|
|||
siteContent={siteContent}
|
||||
games={games}
|
||||
gamesError={gamesError}
|
||||
gamesLoading={gamesLoading}
|
||||
retryGames={retryGames}
|
||||
mhwilds={mhwilds}
|
||||
diablo4={diablo4}
|
||||
aniimo={aniimo}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue