|
1 | 1 | import React, { useEffect } from "react"; |
| 2 | +import { createRoot } from "react-dom/client"; |
2 | 3 | import NavbarIcon from "./NavbarIcon"; |
3 | | - |
4 | | -const iconMap = [ |
5 | | - { id: "nav-docs", name: "Docs" }, |
6 | | - { id: "nav-showcase", name: "Showcase" }, |
7 | | - { id: "nav-dashboard", name: "Dashboard" }, |
8 | | - { id: "nav-donate", name: "Donate" }, |
9 | | - { id: "nav-devfolio", name: "Devfolio" }, |
10 | | - { id: "nav-blogs", name: "Blogs" }, |
11 | | - { id: "nav-more", name: "More" }, |
12 | | - // Sub nav items |
13 | | - { id: "nav-github", name: "GitHub" }, |
14 | | - { id: "nav-badges", name: "Badges" }, |
15 | | - { id: "nav-ebooks", name: "Ebooks" }, |
16 | | - { id: "nav-roadmap", name: "Roadmap" }, |
17 | | - { id: "nav-community", name: "Community" }, |
18 | | - { id: "nav-broadcast", name: "Broadcast" }, |
19 | | - { id: "nav-podcast", name: "Podcast" }, |
20 | | - { id: "nav-technical", name: "Technical" }, |
21 | | - { id: "nav-behavioral", name: "Behavioral" }, |
22 | | -]; |
| 4 | +import { NAVBAR_ICONS, type NavbarIconName } from "../../constants/navbarConfig"; |
23 | 5 |
|
24 | 6 | export default function NavbarIconInjector() { |
25 | 7 | useEffect(() => { |
26 | | - iconMap.forEach(({ id, name }) => { |
27 | | - const el = document.getElementById(id); |
28 | | - if (el && el.childNodes.length === 0) { |
29 | | - import("react-dom").then((ReactDOM) => { |
30 | | - (ReactDOM.default as any).render(<NavbarIcon name={name} />, el); |
31 | | - }); |
| 8 | + const roots = new Map<string, any>(); |
| 9 | + |
| 10 | + NAVBAR_ICONS.forEach((name: NavbarIconName) => { |
| 11 | + const id = `nav-${name.toLowerCase()}`; |
| 12 | + try { |
| 13 | + const el = document.getElementById(id); |
| 14 | + if (el && !roots.has(id)) { |
| 15 | + const root = createRoot(el); |
| 16 | + root.render(<NavbarIcon name={name} />); |
| 17 | + roots.set(id, root); |
| 18 | + } |
| 19 | + } catch (error) { |
| 20 | + console.warn(`Failed to inject navbar icon for ${name}:`, error); |
32 | 21 | } |
33 | 22 | }); |
| 23 | + |
| 24 | + return () => { |
| 25 | + roots.forEach((root, id) => { |
| 26 | + try { |
| 27 | + root.unmount(); |
| 28 | + } catch (error) { |
| 29 | + console.warn(`Failed to unmount navbar icon for ${id}:`, error); |
| 30 | + } |
| 31 | + }); |
| 32 | + }; |
34 | 33 | }, []); |
| 34 | + |
35 | 35 | return null; |
36 | 36 | } |
0 commit comments