blob: b43504720cb32f7705cb4f46b17d76674206c417 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
/**
* Main React app logic
*/
import React, { useState, useEffect, useCallback } from "react";
import Detail from "./Detail.js";
import Legend from "./Legend.js";
import buildGraph from "../graph.js";
export default function App() {
const [db, setDb] = useState(null);
const [node, setNode] = useState(null);
const [graph, setGraph] = useState(null);
const [selectedCategory, setSelectedCategory] = useState(null);
const [detailVisible, setDetailVisible] = useState(null);
/** Load the database */
useEffect(async () => {
const newDb = await loadDB();
setDb(newDb);
setGraph(
buildGraph(newDb, {
click: handleClick,
})
);
}, []);
/** Click to open a node */
const handleClick = useCallback((node) => {
setNode(node);
setDetailVisible(true);
});
/** Click to close the media modal */
const handleClose = useCallback((node) => {
setDetailVisible(false);
});
/** Select or clear the category */
const handleSelect = useCallback((category) => {
if (category === selectedCategory) {
setSelectedCategory(null);
graph.onSelect(null);
} else {
setSelectedCategory(category);
graph.onSelect(category);
}
});
return (
<div>
<Detail node={node} visible={detailVisible} onClose={handleClose} />
<Legend
visible={!detailVisible}
selected={selectedCategory}
onSelect={handleSelect}
/>
</div>
);
}
async function loadDB() {
const request = await fetch("/assets/db.json");
return await request.json();
}
|