|
1 | 1 | import { useEffect, useCallback } from "react"; |
2 | 2 | import { BrowserRouter as Router, Routes, Route, Navigate, NavLink } from "react-router-dom"; |
3 | 3 | import { useTheme } from "./contexts/ThemeProvider"; |
4 | | -import { APIProvider } from "./contexts/APIProvider"; |
| 4 | +import { useAPI } from "./contexts/APIProvider"; |
5 | 5 | import LogViewerPage from "./pages/LogViewer"; |
6 | 6 | import ModelPage from "./pages/Models"; |
7 | 7 | import ActivityPage from "./pages/Activity"; |
8 | | -import ConnectionStatus from "./components/ConnectionStatus"; |
| 8 | +import ConnectionStatusIcon from "./components/ConnectionStatus"; |
9 | 9 | import { RiSunFill, RiMoonFill } from "react-icons/ri"; |
10 | | -import { usePersistentState } from "./hooks/usePersistentState"; |
11 | 10 |
|
12 | 11 | function App() { |
13 | | - const { isNarrow, toggleTheme, isDarkMode } = useTheme(); |
14 | | - const [appTitle, setAppTitle] = usePersistentState("app-title", "llama-swap"); |
15 | | - |
| 12 | + const { isNarrow, toggleTheme, isDarkMode, appTitle, setAppTitle, setConnectionState } = useTheme(); |
16 | 13 | const handleTitleChange = useCallback( |
17 | 14 | (newTitle: string) => { |
18 | | - setAppTitle(newTitle); |
19 | | - document.title = newTitle; |
| 15 | + setAppTitle(newTitle.replace(/\n/g, "").trim().substring(0, 64) || "llama-swap"); |
20 | 16 | }, |
21 | 17 | [setAppTitle] |
22 | 18 | ); |
23 | 19 |
|
| 20 | + const { connectionStatus } = useAPI(); |
| 21 | + |
| 22 | + // Synchronize the window.title connections state with the actual connection state |
24 | 23 | useEffect(() => { |
25 | | - document.title = appTitle; // Set initial title |
26 | | - }, [appTitle]); |
| 24 | + setConnectionState(connectionStatus); |
| 25 | + }, [connectionStatus]); |
27 | 26 |
|
28 | 27 | return ( |
29 | 28 | <Router basename="/ui/"> |
30 | | - <APIProvider> |
31 | | - <div className="flex flex-col h-screen"> |
32 | | - <nav className="bg-surface border-b border-border p-2 h-[75px]"> |
33 | | - <div className="flex items-center justify-between mx-auto px-4 h-full"> |
34 | | - {!isNarrow && ( |
35 | | - <h1 |
36 | | - contentEditable |
37 | | - suppressContentEditableWarning |
38 | | - className="flex items-center p-0 outline-none hover:bg-gray-100 dark:hover:bg-gray-700 rounded px-1" |
39 | | - onBlur={(e) => |
40 | | - handleTitleChange(e.currentTarget.textContent?.replace(/\n/g, "").trim() || "llama-swap") |
| 29 | + <div className="flex flex-col h-screen"> |
| 30 | + <nav className="bg-surface border-b border-border p-2 h-[75px]"> |
| 31 | + <div className="flex items-center justify-between mx-auto px-4 h-full"> |
| 32 | + {!isNarrow && ( |
| 33 | + <h1 |
| 34 | + contentEditable |
| 35 | + suppressContentEditableWarning |
| 36 | + className="flex items-center p-0 outline-none hover:bg-gray-100 dark:hover:bg-gray-700 rounded px-1" |
| 37 | + onBlur={(e) => handleTitleChange(e.currentTarget.textContent || "(set title)")} |
| 38 | + onKeyDown={(e) => { |
| 39 | + if (e.key === "Enter") { |
| 40 | + e.preventDefault(); |
| 41 | + handleTitleChange(e.currentTarget.textContent || "(set title)"); |
| 42 | + e.currentTarget.blur(); |
41 | 43 | } |
42 | | - onKeyDown={(e) => { |
43 | | - if (e.key === "Enter") { |
44 | | - e.preventDefault(); |
45 | | - const sanitizedText = |
46 | | - e.currentTarget.textContent?.replace(/\n/g, "").trim().substring(0, 25) || "llama-swap"; |
47 | | - handleTitleChange(sanitizedText); |
48 | | - e.currentTarget.textContent = sanitizedText; |
49 | | - e.currentTarget.blur(); |
50 | | - } |
51 | | - }} |
52 | | - > |
53 | | - {appTitle} |
54 | | - </h1> |
55 | | - )} |
56 | | - <div className="flex items-center space-x-4"> |
57 | | - <NavLink to="/" className={({ isActive }) => (isActive ? "navlink active" : "navlink")}> |
58 | | - Logs |
59 | | - </NavLink> |
60 | | - |
61 | | - <NavLink to="/models" className={({ isActive }) => (isActive ? "navlink active" : "navlink")}> |
62 | | - Models |
63 | | - </NavLink> |
64 | | - |
65 | | - <NavLink to="/activity" className={({ isActive }) => (isActive ? "navlink active" : "navlink")}> |
66 | | - Activity |
67 | | - </NavLink> |
68 | | - <button className="" onClick={toggleTheme}> |
69 | | - {isDarkMode ? <RiMoonFill /> : <RiSunFill />} |
70 | | - </button> |
71 | | - <ConnectionStatus /> |
72 | | - </div> |
| 44 | + }} |
| 45 | + > |
| 46 | + {appTitle} |
| 47 | + </h1> |
| 48 | + )} |
| 49 | + <div className="flex items-center space-x-4"> |
| 50 | + <NavLink to="/" className={({ isActive }) => (isActive ? "navlink active" : "navlink")}> |
| 51 | + Logs |
| 52 | + </NavLink> |
| 53 | + <NavLink to="/models" className={({ isActive }) => (isActive ? "navlink active" : "navlink")}> |
| 54 | + Models |
| 55 | + </NavLink> |
| 56 | + <NavLink to="/activity" className={({ isActive }) => (isActive ? "navlink active" : "navlink")}> |
| 57 | + Activity |
| 58 | + </NavLink> |
| 59 | + <button className="" onClick={toggleTheme}> |
| 60 | + {isDarkMode ? <RiMoonFill /> : <RiSunFill />} |
| 61 | + </button> |
| 62 | + <ConnectionStatusIcon /> |
73 | 63 | </div> |
74 | | - </nav> |
| 64 | + </div> |
| 65 | + </nav> |
75 | 66 |
|
76 | | - <main className="flex-1 overflow-auto p-4"> |
77 | | - <Routes> |
78 | | - <Route path="/" element={<LogViewerPage />} /> |
79 | | - <Route path="/models" element={<ModelPage />} /> |
80 | | - <Route path="/activity" element={<ActivityPage />} /> |
81 | | - <Route path="*" element={<Navigate to="/" replace />} /> |
82 | | - </Routes> |
83 | | - </main> |
84 | | - </div> |
85 | | - </APIProvider> |
| 67 | + <main className="flex-1 overflow-auto p-4"> |
| 68 | + <Routes> |
| 69 | + <Route path="/" element={<LogViewerPage />} /> |
| 70 | + <Route path="/models" element={<ModelPage />} /> |
| 71 | + <Route path="/activity" element={<ActivityPage />} /> |
| 72 | + <Route path="*" element={<Navigate to="/" replace />} /> |
| 73 | + </Routes> |
| 74 | + </main> |
| 75 | + </div> |
86 | 76 | </Router> |
87 | 77 | ); |
88 | 78 | } |
|
0 commit comments