()
| 5 | |
| 6 | |
| 7 | function Navigation() { |
| 8 | const pagesIndexPath = '/generated/index/pages.json'; |
| 9 | const piecesIndexPath = '/generated/index/pieces.json'; |
| 10 | const [pages, setPages] = React.useState([]); |
| 11 | const [pieces, setPieces] = React.useState([]); |
| 12 | const [config, setConfig] = React.useState(null); |
| 13 | const navigate = useNavigate(); |
| 14 | |
| 15 | React.useEffect(() => { |
| 16 | Promise.all([ |
| 17 | fetch(pagesIndexPath).then(r => r.json()), |
| 18 | fetch(piecesIndexPath).then(r => r.json()), |
| 19 | loadConfig() |
| 20 | ]) |
| 21 | .then(([pagesData, piecesData, configData]) => { |
| 22 | setPages(pagesData); |
| 23 | setPieces(piecesData); |
| 24 | setConfig(configData); |
| 25 | }) |
| 26 | .catch(error => console.error('[nav]: error loading data:', error)); |
| 27 | }, []); |
| 28 | |
| 29 | const handleRandomPiece = () => { |
| 30 | if (pieces.length > 0) { |
| 31 | const randomIndex = Math.floor(Math.random() * pieces.length); |
| 32 | const randomPiece = pieces[randomIndex]; |
| 33 | navigate(`/${randomPiece.slug}`); |
| 34 | } |
| 35 | }; |
| 36 | |
| 37 | return ( |
| 38 | <nav> |
| 39 | <NavLink key="home" to="/"> |
| 40 | {config?.ui?.labels?.home || 'Home'} |
| 41 | </NavLink> |
| 42 | {pages.map((page) => ( |
| 43 | <NavLink key={page.slug} to={`/${page.slug}`}> |
| 44 | {page.title} |
| 45 | </NavLink> |
| 46 | ))} |
| 47 | <button onClick={handleRandomPiece} className="random-piece-button"> |
| 48 | {config?.ui?.labels?.randomPiece || 'Random Piece'} |
| 49 | </button> |
| 50 | <a href="/generated/feed.xml" target="_blank" rel="noopener noreferrer" className="rss-link"> |
| 51 | {config?.ui?.labels?.rss || 'RSS'} |
| 52 | </a> |
| 53 | </nav> |
| 54 | ); |
| 55 | } |
| 56 | |
| 57 | export default Navigation; |
nothing calls this directly
no test coverage detected