|
1 | 1 | import React, { useCallback, useMemo, useState } from "react"; |
2 | 2 | import { styled } from "@linaria/react"; |
3 | | -import { |
4 | | - DeclarationsContext, |
5 | | - DeclarationsContextType, |
6 | | - ReferenceEntry, |
7 | | - declarationKey, |
8 | | -} from "./schema/DeclarationsContext"; |
9 | | -import { Declaration, SchemaClass, SchemaFieldType } from "../data/types"; |
10 | | -import { GameId } from "../games-list"; |
| 3 | +import { DeclarationsContext, DeclarationsContextType } from "./schema/DeclarationsContext"; |
11 | 4 | import { DeclarationsSidebar } from "./DeclarationsSidebar"; |
12 | 5 | import { ContentList } from "./schema/ContentList"; |
13 | 6 | import { SidebarFilterContext } from "./layout/SidebarFilterContext"; |
14 | 7 | import { SearchContext } from "./search/SearchContext"; |
15 | 8 | import { useHashParam } from "../utils/filtering"; |
16 | 9 | import { NavBar } from "./layout/NavBar"; |
17 | 10 |
|
18 | | -function collectTypeKeys(type: SchemaFieldType, out: Set<string>) { |
19 | | - switch (type.category) { |
20 | | - case "declared_class": |
21 | | - case "declared_enum": |
22 | | - out.add(declarationKey(type.module, type.name)); |
23 | | - break; |
24 | | - case "ptr": |
25 | | - case "fixed_array": |
26 | | - collectTypeKeys(type.inner, out); |
27 | | - break; |
28 | | - case "atomic": |
29 | | - if (type.inner) collectTypeKeys(type.inner, out); |
30 | | - if (type.inner2) collectTypeKeys(type.inner2, out); |
31 | | - break; |
32 | | - } |
33 | | -} |
34 | | - |
35 | | -function buildReferences(declarations: Declaration[]): Map<string, ReferenceEntry[]> { |
36 | | - const refs = new Map<string, ReferenceEntry[]>(); |
37 | | - |
38 | | - function addRef(target: string, entry: ReferenceEntry) { |
39 | | - let list = refs.get(target); |
40 | | - if (!list) { |
41 | | - list = []; |
42 | | - refs.set(target, list); |
43 | | - } |
44 | | - list.push(entry); |
45 | | - } |
46 | | - |
47 | | - for (const decl of declarations) { |
48 | | - if (decl.kind === "class") { |
49 | | - for (const parent of decl.parents) { |
50 | | - addRef(declarationKey(parent.module, parent.name), { |
51 | | - declarationName: decl.name, |
52 | | - declarationModule: decl.module, |
53 | | - relation: "class", |
54 | | - }); |
55 | | - } |
56 | | - for (const field of decl.fields) { |
57 | | - const keys = new Set<string>(); |
58 | | - collectTypeKeys(field.type, keys); |
59 | | - const declKey = declarationKey(decl.module, decl.name); |
60 | | - for (const key of keys) { |
61 | | - if (key !== declKey) { |
62 | | - addRef(key, { |
63 | | - declarationName: decl.name, |
64 | | - declarationModule: decl.module, |
65 | | - fieldName: field.name, |
66 | | - relation: "field", |
67 | | - }); |
68 | | - } |
69 | | - } |
70 | | - } |
71 | | - } |
72 | | - } |
73 | | - |
74 | | - return refs; |
75 | | -} |
76 | | - |
77 | | -export default function DeclarationsPage({ |
78 | | - context, |
79 | | -}: { |
80 | | - context: Omit<DeclarationsContextType, "references" | "otherGamesLookup" | "classesByKey"> & { |
81 | | - otherGames: Map<GameId, Declaration[]>; |
82 | | - }; |
83 | | -}) { |
| 11 | +export default function DeclarationsPage({ context }: { context: DeclarationsContextType }) { |
84 | 12 | const [filter, setFilter] = useState(""); |
85 | 13 | const [sidebarOpen, setSidebarOpen] = useState(false); |
86 | 14 | const search = useHashParam("search") ?? ""; |
87 | 15 |
|
88 | | - const references = useMemo(() => buildReferences(context.declarations), [context.declarations]); |
89 | | - |
90 | | - const classesByKey = useMemo(() => { |
91 | | - const map = new Map<string, SchemaClass>(); |
92 | | - for (const d of context.declarations) { |
93 | | - if (d.kind === "class") map.set(declarationKey(d.module, d.name), d); |
94 | | - } |
95 | | - return map; |
96 | | - }, [context.declarations]); |
97 | | - |
98 | | - const otherGamesLookup = useMemo(() => { |
99 | | - const lookup = new Map<GameId, Map<string, Declaration>>(); |
100 | | - for (const [gameId, decls] of context.otherGames) { |
101 | | - const map = new Map<string, Declaration>(); |
102 | | - for (const d of decls) map.set(d.name, d); |
103 | | - lookup.set(gameId, map); |
104 | | - } |
105 | | - return lookup; |
106 | | - }, [context.otherGames]); |
107 | | - |
108 | | - const fullContext = useMemo(() => { |
109 | | - const { otherGames: _, ...rest } = context; |
110 | | - return { ...rest, references, classesByKey, otherGamesLookup }; |
111 | | - }, [context, references, classesByKey, otherGamesLookup]); |
112 | | - |
113 | 16 | const searchCtx = useMemo(() => ({ search }), [search]); |
114 | 17 | const filterCtx = useMemo(() => ({ filter, setFilter }), [filter]); |
115 | 18 |
|
116 | 19 | const closeSidebar = useCallback(() => setSidebarOpen(false), []); |
117 | 20 | const openSidebar = useCallback(() => setSidebarOpen(true), []); |
118 | 21 |
|
119 | 22 | return ( |
120 | | - <DeclarationsContext.Provider value={fullContext}> |
| 23 | + <DeclarationsContext.Provider value={context}> |
121 | 24 | <SearchContext.Provider value={searchCtx}> |
122 | 25 | <SidebarFilterContext.Provider value={filterCtx}> |
123 | 26 | <PageGrid> |
|
0 commit comments