feat: add in-app admin navigation for whitelisted users

Detect admin access after login and expose a header button that opens /admin via client-side routing so the session stays unlocked when returning to the app.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-06-05 10:03:46 +02:00
parent e1cb2754c4
commit 43cf589613
9 changed files with 79 additions and 9 deletions
+27 -2
View File
@@ -50,6 +50,8 @@ import { Ship, LogOut, ChevronLeft, Users, FileText, Settings, Wifi, WifiOff, La
import DisclaimerHeaderButton from './components/DisclaimerHeaderButton.tsx'
import FeedbackHeaderButton from './components/FeedbackHeaderButton.tsx'
import ProfileHeaderButton from './components/ProfileHeaderButton.tsx'
import AdminHeaderButton from './components/AdminHeaderButton.tsx'
import { checkAdminAccess } from './services/adminApi.js'
import { useTranslation } from 'react-i18next'
import { cycleAppLanguage } from './utils/i18nLanguages.js'
import {
@@ -94,6 +96,7 @@ function App() {
// Public demo mode (no account required)
const [isDemoMode, setIsDemoMode] = useState(() => window.location.pathname === '/demo')
const [isAdminRoute, setIsAdminRoute] = useState(() => window.location.pathname.startsWith('/admin'))
const [isAdminUser, setIsAdminUser] = useState(false)
const syncQueueCount = useLiveQuery(
() => activeLogbookId ? db.syncQueue.where({ logbookId: activeLogbookId }).count() : db.syncQueue.count(),
@@ -162,14 +165,23 @@ function App() {
})
}, [])
const refreshAdminAccess = useCallback(async () => {
const isAdmin = await checkAdminAccess()
setIsAdminUser(isAdmin)
}, [])
useEffect(() => {
if (!isAuthenticated) return
if (!isAuthenticated) {
setIsAdminUser(false)
return
}
const userId = localStorage.getItem('active_userid')
if (!userId) return
void syncAppearancePrefs(userId)
void migrateLegacyCrewToPoolIfNeeded().then(() => syncPersonPool())
void migrateLegacyYachtsToPoolIfNeeded().then(() => syncVesselPool())
}, [isAuthenticated])
void refreshAdminAccess()
}, [isAuthenticated, refreshAdminAccess])
useEffect(() => {
const handleOnline = () => {
@@ -249,6 +261,7 @@ function App() {
const clearAuthenticatedAppState = useCallback(() => {
setIsAuthenticated(false)
setIsAdminUser(false)
setActiveLogbookId(null)
setActiveLogbookTitle(null)
setShowUserProfile(false)
@@ -343,6 +356,14 @@ function App() {
setIsAcceptingInvite(false)
}, [])
const openAdmin = useCallback(() => {
window.history.pushState({}, document.title, '/admin')
setIsAdminRoute(true)
setIsDemoMode(false)
setIsViewerMode(false)
setIsAcceptingInvite(false)
}, [])
const selectLogbook = useCallback((id: string, title: string) => {
setActiveLogbookId(id)
setActiveLogbookTitle(title)
@@ -507,6 +528,7 @@ function App() {
if (!(await confirmLeave())) return
void logoutUser()
setIsAuthenticated(false)
setIsAdminUser(false)
setActiveLogbookId(null)
setActiveLogbookTitle(null)
setShowUserProfile(false)
@@ -621,6 +643,7 @@ function App() {
onSelectLogbook={selectLogbook}
onLogout={handleLogout}
onOpenProfile={() => setShowUserProfile(true)}
onOpenAdmin={isAdminUser ? openAdmin : undefined}
/>
</div>
)
@@ -671,6 +694,8 @@ function App() {
<Languages size={18} />
</button>
{isAdminUser && <AdminHeaderButton onClick={openAdmin} />}
<ProfileHeaderButton onClick={() => setShowUserProfile(true)} />
<DisclaimerHeaderButton />