|
1 | 1 | import React, { useEffect, useState } from "react"; |
2 | | -import { useParams } from "react-router-dom"; |
3 | | -import styled from "styled-components"; |
| 2 | +import { Navigate, useParams } from "react-router-dom"; |
4 | 3 | import { Declaration } from "../../components/Docs/api"; |
5 | 4 | import { NavBar } from "../../components/layout/NavBar"; |
6 | | -import { isGameId, GameId } from "../../games"; |
7 | | -import { loadAllSchemas } from "../data"; |
| 5 | +import { TextMessage } from "../../components/layout/Content"; |
| 6 | +import { isGameId, GameId, GAMES } from "../../games"; |
| 7 | +import { loadGameSchemas } from "../data"; |
8 | 8 | import DeclarationsPage from "../DeclarationsPage"; |
9 | 9 |
|
10 | | -const LoadingMessage = styled.div` |
11 | | - margin: auto; |
12 | | - text-align: center; |
13 | | - font-size: 24px; |
14 | | -`; |
| 10 | +const EMPTY_DECLARATIONS: Declaration[] = []; |
| 11 | +const EMPTY_OTHER_GAMES = new Map<GameId, Declaration[]>(); |
15 | 12 |
|
16 | 13 | export default function SchemasPage() { |
17 | 14 | const { game } = useParams<{ game: string }>(); |
18 | | - const [allGames, setAllGames] = useState<Map<GameId, Declaration[]> | null>(null); |
| 15 | + const [declarations, setDeclarations] = useState<Declaration[] | null>(null); |
| 16 | + const [otherGames, setOtherGames] = useState<Map<GameId, Declaration[]>>(EMPTY_OTHER_GAMES); |
19 | 17 | const [error, setError] = useState<string | null>(null); |
20 | 18 | const validGame = game && isGameId(game) ? game : null; |
21 | 19 |
|
22 | 20 | useEffect(() => { |
23 | 21 | if (!validGame) return; |
24 | | - loadAllSchemas() |
25 | | - .then(setAllGames) |
26 | | - .catch((e) => setError(e instanceof Error ? e.message : String(e))); |
| 22 | + let stale = false; |
| 23 | + |
| 24 | + setDeclarations(null); |
| 25 | + setOtherGames(EMPTY_OTHER_GAMES); |
| 26 | + setError(null); |
| 27 | + |
| 28 | + loadGameSchemas(validGame) |
| 29 | + .then((data) => { |
| 30 | + if (stale) return; |
| 31 | + setDeclarations(data); |
| 32 | + |
| 33 | + // Load other games in background for cross-game refs |
| 34 | + for (const g of GAMES) { |
| 35 | + if (g.id === validGame) continue; |
| 36 | + loadGameSchemas(g.id) |
| 37 | + .then((data) => { if (!stale) setOtherGames((prev) => new Map(prev).set(g.id, data)); }) |
| 38 | + .catch(() => {}); |
| 39 | + } |
| 40 | + }) |
| 41 | + .catch((e) => { if (!stale) setError(e instanceof Error ? e.message : String(e)); }); |
| 42 | + |
| 43 | + return () => { stale = true; }; |
27 | 44 | }, [validGame]); |
28 | 45 |
|
29 | 46 | if (!validGame) { |
30 | | - return ( |
31 | | - <> |
32 | | - <NavBar /> |
33 | | - <LoadingMessage>Unknown game: {game}</LoadingMessage> |
34 | | - </> |
35 | | - ); |
| 47 | + return <Navigate to="/cs2" replace />; |
36 | 48 | } |
37 | 49 |
|
38 | | - if (error) |
| 50 | + if (error) { |
39 | 51 | return ( |
40 | 52 | <> |
41 | 53 | <NavBar /> |
42 | | - <LoadingMessage>Failed to load schemas: {error}</LoadingMessage> |
43 | | - </> |
44 | | - ); |
45 | | - if (!allGames) |
46 | | - return ( |
47 | | - <> |
48 | | - <NavBar /> |
49 | | - <LoadingMessage>Loading schemas...</LoadingMessage> |
50 | | - </> |
51 | | - ); |
52 | | - |
53 | | - const declarations = allGames.get(validGame); |
54 | | - if (!declarations) |
55 | | - return ( |
56 | | - <> |
57 | | - <NavBar /> |
58 | | - <LoadingMessage>No data for {validGame}</LoadingMessage> |
| 54 | + <TextMessage>Failed to load schemas: {error}</TextMessage> |
59 | 55 | </> |
60 | 56 | ); |
| 57 | + } |
61 | 58 |
|
62 | 59 | return ( |
63 | 60 | <DeclarationsPage |
64 | | - context={{ game: validGame, root: `/${validGame}`, declarations, allGames }} |
| 61 | + context={{ |
| 62 | + game: validGame, |
| 63 | + root: `/${validGame}`, |
| 64 | + declarations: declarations ?? EMPTY_DECLARATIONS, |
| 65 | + otherGames, |
| 66 | + loading: !declarations, |
| 67 | + }} |
65 | 68 | /> |
66 | 69 | ); |
67 | 70 | } |
0 commit comments