diff --git a/backend/dashboard/README.md b/backend/dashboard/README.md index b87cb00..76b15e7 100644 --- a/backend/dashboard/README.md +++ b/backend/dashboard/README.md @@ -1,46 +1,30 @@ -# Getting Started with Create React App +# LMR dashboard -This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). +The management UI for the gateway. Vite + React + TypeScript, styled with +Tailwind utilities and a small set of component classes in `src/index.css`. -## Available Scripts +## Develop -In the project directory, you can run: +```bash +npm install +npm run dev # http://localhost:5173, proxies /api, /v1 and /auth to :8080 +``` -### `npm start` +Run the Go server next to it, or use `make dev` from `backend/`. -Runs the app in the development mode.\ -Open [http://localhost:3000](http://localhost:3000) to view it in the browser. +## Build -The page will reload if you make edits.\ -You will also see any lint errors in the console. +```bash +npm run build # type-checks, then writes ../internal/web/dist for go:embed +``` -### `npm test` +## Layout -Launches the test runner in the interactive watch mode.\ -See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. - -### `npm run build` - -Builds the app for production to the `build` folder.\ -It correctly bundles React in production mode and optimizes the build for the best performance. - -The build is minified and the filenames include the hashes.\ -Your app is ready to be deployed! - -See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. - -### `npm run eject` - -**Note: this is a one-way operation. Once you `eject`, you can’t go back!** - -If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project. - -Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own. - -You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it. - -## Learn More - -You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). - -To learn React, check out the [React documentation](https://reactjs.org/). +| Path | Purpose | +|---|---| +| `src/lib/api.ts` | Typed API client, token storage, OIDC re-login on 401 | +| `src/lib/format.ts` | Number, cost, time and token formatting | +| `src/components/ui.tsx` | Shared primitives: cards, buttons, inputs, badges, modal, toasts | +| `src/components/VolumeChart.tsx` | Request volume line chart with hover tooltip | +| `src/components/Layout.tsx` | Sidebar, header, health indicator | +| `src/pages/` | One file per route | diff --git a/backend/dashboard/src/App.tsx b/backend/dashboard/src/App.tsx index 954e785..3421e16 100644 --- a/backend/dashboard/src/App.tsx +++ b/backend/dashboard/src/App.tsx @@ -1,7 +1,8 @@ import { useState, useEffect } from 'react' import { BrowserRouter, Routes, Route, Navigate } from 'react-router-dom' -import { getToken, clearToken, api } from './lib/api' +import { getToken, setToken, clearToken, takeReturnPath, api } from './lib/api' import type { CurrentUser } from './lib/types' +import { ToastProvider, Spinner } from './components/ui' import Login from './pages/Login' import Layout from './components/Layout' import Overview from './pages/Overview' @@ -14,41 +15,66 @@ import Chat from './pages/Chat' import Models from './pages/Models' import Settings from './pages/Settings' +/** Pull a token out of the OIDC callback fragment, if present. */ +function consumeFragmentToken(): boolean { + if (!window.location.hash.startsWith('#')) return false + const hash = new URLSearchParams(window.location.hash.slice(1)) + const tok = hash.get('token') + if (!tok) return false + setToken(tok) + window.history.replaceState({}, '', takeReturnPath()) + return true +} + export default function App() { - const [authed, setAuthed] = useState(() => !!getToken()) + const [authed, setAuthed] = useState(() => consumeFragmentToken() || !!getToken()) const [currentUser, setCurrentUser] = useState(null) + const [authError, setAuthError] = useState('') useEffect(() => { if (!authed) return - api.me().then(setCurrentUser).catch(() => { + api.me().then(setCurrentUser).catch(e => { clearToken() setAuthed(false) + setAuthError(e instanceof Error ? e.message : 'Sign-in failed') }) }, [authed]) if (!authed) { - return setAuthed(true)} /> + return } + if (!currentUser) { + return ( +
+ Signing in… +
+ ) + } + + const admin = currentUser.is_admin + return ( - - - }> - } /> - } /> - } /> - } /> - } /> - } /> - {currentUser?.is_admin && <> - } /> - } /> - } /> - } /> - } - } /> - - - + + + + }> + } /> + } /> + } /> + } /> + } /> + } /> + {admin && <> + } /> + } /> + } /> + } /> + } + } /> + + + + ) } diff --git a/backend/dashboard/src/components/Layout.tsx b/backend/dashboard/src/components/Layout.tsx index 50a62ca..833f9a6 100644 --- a/backend/dashboard/src/components/Layout.tsx +++ b/backend/dashboard/src/components/Layout.tsx @@ -1,155 +1,133 @@ +import { useEffect, useState } from 'react' import { NavLink, Outlet, useLocation } from 'react-router-dom' -import { clearToken } from '../lib/api' +import { api, clearToken } from '../lib/api' import type { CurrentUser } from '../lib/types' +import { Dot } from './ui' const USER_NAV = [ - { to: '/overview', label: 'Overview', icon: }, - { to: '/chat', label: 'Test Chat', icon: }, - { to: '/logs', label: 'Request Logs', icon: }, - { to: '/keys', label: 'API Keys', icon: }, - { to: '/analytics',label: 'Analytics', icon: }, + { to: '/overview', label: 'Overview', icon: }, + { to: '/chat', label: 'Test Chat', icon: }, + { to: '/logs', label: 'Request Logs', icon: }, + { to: '/keys', label: 'API Keys', icon: }, + { to: '/analytics', label: 'Analytics', icon: }, ] const ADMIN_NAV = [ - { to: '/providers',label: 'Providers', icon: }, - { to: '/routing', label: 'Routing Rules', icon: }, - { to: '/models', label: 'Model Catalog', icon: }, + { to: '/providers', label: 'Providers', icon: }, + { to: '/models', label: 'Model Catalog', icon: }, + { to: '/routing', label: 'Routing', icon: }, + { to: '/settings', label: 'Settings', icon: }, ] -function navStyle(active: boolean) { - return { - display: 'flex', alignItems: 'center', gap: '8px', - padding: '7px 10px', borderRadius: '7px', cursor: 'pointer', - fontSize: '13px', fontWeight: 500, - color: active ? '#f0eeff' : 'rgba(237,237,240,0.5)', - background: active ? 'rgba(124,58,237,0.18)' : 'transparent', - transition: 'all 0.12s', - } +export function Logo({ size = 30 }: { size?: number }) { + return ( +
+ + + + + + + +
+ ) } -export default function Layout({ currentUser }: { currentUser: CurrentUser | null }) { +/** Polls the readiness endpoint so the header reflects real gateway health. */ +function useHealth(intervalMs = 30000) { + const [ok, setOk] = useState(null) + useEffect(() => { + let alive = true + const check = () => api.ready().then(r => { if (alive) setOk(r) }) + check() + const t = setInterval(check, intervalMs) + return () => { alive = false; clearInterval(t) } + }, [intervalMs]) + return ok +} + +export default function Layout({ currentUser }: { currentUser: CurrentUser }) { const loc = useLocation() - const allNav = [...USER_NAV, ...(currentUser?.is_admin ? ADMIN_NAV : [])] - const section = allNav.find(n => loc.pathname.startsWith(n.to))?.label ?? 'Settings' + const health = useHealth() + const nav = [...USER_NAV, ...(currentUser.is_admin ? ADMIN_NAV : [])] + const section = nav.find(n => loc.pathname.startsWith(n.to))?.label ?? '' + + useEffect(() => { + document.title = section ? `${section} · ${currentUser.app_name}` : currentUser.app_name + }, [section, currentUser.app_name]) + + function signOut() { + clearToken() + window.location.href = '/' + } + + const initial = (currentUser.name || currentUser.email || 'U')[0].toUpperCase() return ( -
- {/* Sidebar */} -