diff --git a/backend/main.py b/backend/main.py index 2a9684b..d6ec289 100644 --- a/backend/main.py +++ b/backend/main.py @@ -1,4 +1,6 @@ import logging +import os +import time from contextlib import asynccontextmanager from fastapi import FastAPI @@ -57,10 +59,11 @@ app = FastAPI( version="1.0.0", lifespan=lifespan, ) +STARTED_AT = str(int(time.time() * 1000)) app.include_router(kds_api.router, prefix="/api/kds", tags=["KDS"]) @app.get("/health") async def health_check(): - return {"status": "healthy"} + return {"status": "healthy", "version": os.environ.get("BUILD_VERSION", STARTED_AT)} diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index 2304af3..f58c8ad 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -1,5 +1,7 @@ import { Routes, Route, Navigate } from 'react-router-dom' import AuthGate from './components/AuthGate' +import { UpdateBanner } from './components/UpdateBanner' +import { useVersionCheck } from './hooks/useVersionCheck' // Re-export for any archive imports that use `import { useAuth } from '../App'` export { useAuth } from './components/AuthGate' @@ -7,12 +9,16 @@ export { useAuth } from './components/AuthGate' import KDSApp from './pages/KDSApp' export default function App() { + const updateAvailable = useVersionCheck('/kds/health') return ( - - - } /> - } /> - - + <> + + + } /> + } /> + + + + ) } diff --git a/frontend/src/components/UpdateBanner.tsx b/frontend/src/components/UpdateBanner.tsx new file mode 100644 index 0000000..b492c67 --- /dev/null +++ b/frontend/src/components/UpdateBanner.tsx @@ -0,0 +1,44 @@ +import { RefreshCw } from 'lucide-react' + +export function UpdateBanner({ visible }: { visible: boolean }) { + if (!visible) return null + return ( +
+ A new version is available. + +
+ ) +} diff --git a/frontend/src/hooks/useVersionCheck.ts b/frontend/src/hooks/useVersionCheck.ts new file mode 100644 index 0000000..e4fed28 --- /dev/null +++ b/frontend/src/hooks/useVersionCheck.ts @@ -0,0 +1,43 @@ +import { useEffect, useState } from 'react' + +const POLL_MS = 2 * 60 * 1000 + +export function useVersionCheck(healthUrl: string) { + const [updateAvailable, setUpdateAvailable] = useState(false) + + useEffect(() => { + let seenVersion: string | null = null + + async function check() { + try { + const res = await fetch(healthUrl, { cache: 'no-store' }) + if (!res.ok) return + const data = await res.json() + const v: string | undefined = data.version + if (!v) return + if (seenVersion === null) { + seenVersion = v + } else if (v !== seenVersion) { + setUpdateAvailable(true) + } + } catch { + // network error — skip silently + } + } + + check() + const interval = setInterval(check, POLL_MS) + + function onVisible() { + if (document.visibilityState === 'visible') check() + } + document.addEventListener('visibilitychange', onVisible) + + return () => { + clearInterval(interval) + document.removeEventListener('visibilitychange', onVisible) + } + }, [healthUrl]) + + return updateAvailable +}