All files / layouts AppLayout.tsx

87.5% Statements 84/96
58.69% Branches 27/46
84.37% Functions 27/32
87.5% Lines 70/80

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253                        38x   38x 38x 38x     70x 215x 215x 215x 274x 70x 70x 70x 70x     70x     70x 70x 70x 70x 37x     70x                                                                                                                         38x 35x             38x   72x 219x 219x 72x 72x 72x                                                                                                                                                       1x                                                                                                                        
import { useEffect, useRef, useState } from 'react'
import { Outlet, useLocation } from 'react-router-dom'
import { useUiStore } from './uiStore'
import { useAuthStore } from '@/features/auth/authStore'
import { useHealthStore } from './healthStore'
import { getEnv } from '@/lib/env'
import { APP_VERSION } from '@/version'
import { SessionTimeoutModal } from './SessionTimeoutModal'
import { CommandSearch } from '@/shared/components/CommandSearch'
import { ErrorBoundary } from '@/shared/components/ErrorBoundary'
import { PageTransition } from '@/shared/components/PageTransition'
import { Button } from '@/components/ui/button'
 
if (import.meta.env.DEV) {
  window.__uiStore = useUiStore
E}
 
export function AppLayout() {
  const sidebarOpen = useUiStore((s) => s.sidebarOpen)
  const toggleSidebar = useUiStore((s) => s.toggleSidebar)
  const theme = useUiStore((s) => s.theme)
  const fetchHealth = useHealthStore((s) => s.fetchHealth)
  const intervalRef = useRef<ReturnType<typeof setInterval> | null>(null)
 
  useEffect(() => {
    document.documentElement.classList.remove('dark', 'light', 'high-contrast')
    if (theme === 'high-contrast') {
  I    document.documentElement.classList.add('dark')
    } else {
      document.documentElement.classList.add(theme)
    }
  }, [theme])
 
  useEffect(() => {
    fetchHealth()
    intervalRef.current = setInterval(fetchHealth, 30_000)
    return () => {
   E   if (intervalRef.current) clearInterval(intervalRef.current)
    }
  }, [fetchHealth])
 
  return (
    <div className="flex min-h-screen flex-col bg-background text-foreground">
      <TopBar onToggleSidebar={toggleSidebar} />
      <div className="flex flex-1">
        <Sidebar open={sidebarOpen} />
        <main className="flex-1 p-6">
          <ErrorBoundary>
            <PageTransition>
              <Outlet />
            </PageTransition>
          </ErrorBoundary>
        </main>
      </div>
      <BottomBar />
      <SessionTimeoutModal />
      <CommandSearch />
    </div>
  )
}
 
function TopBar({ onToggleSidebar }: { onToggleSidebar: () => void }) {
  const user = useAuthStore((s) => s.user)
  const clearAuth = useAuthStore((s) => s.clearAuth)
  const [menuOpen, setMenuOpen] = useState(false)
  const userEmail = user?.email ?? 'user@keskikuja.site'
 
  return (
    <header
      role="banner"
      className="sticky top-0 z-30 flex h-14 items-center justify-between border-b border-border bg-background px-4"
    >
      <div className="flex items-center gap-3">
        <Button
          variant="ghost"
          size="icon"
          onClick={onToggleSidebar}
          aria-label="Toggle sidebar"
        >
          <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
            <line x1="3" y1="6" x2="21" y2="6" />
            <line x1="3" y1="12" x2="21" y2="12" />
            <line x1="3" y1="18" x2="21" y2="18" />
          </svg>
        </Button>
        <img src="/agent-platform-logo.png" alt="Agent Platform" className="hidden h-8 w-auto sm:block" />
      </div>
      <div className="relative flex items-center gap-2 text-sm text-muted-foreground">
        <button
          onClick={() => setMenuOpen(!menuOpen)}
          className="flex items-center gap-1 rounded-md px-2 py-1 hover:bg-accent"
          aria-haspopup="true"
          aria-expanded={menuOpen}
        >
          {userEmail}
          <svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2"><polyline points="6 9 12 15 18 9" /></svg>
        </button>
        {menuOpen && (
          <>
            <div className="fixed inset-0 z-10" onClick={() => setMenuOpen(false)} />
            <div className="absolute right-0 top-full z-20 mt-1 w-48 rounded-md border border-border bg-card shadow-lg">
              <a
                href="#/settings"
                onClick={() => setMenuOpen(false)}
                className="block px-4 py-2 text-sm hover:bg-accent"
              >
                Settings
              </a>
              <a
                href={`https://api.${getEnv().DOMAIN}/api/v1/swagger`}
                target="_blank"
                rel="noopener noreferrer"
                onClick={() => setMenuOpen(false)}
                className="block px-4 py-2 text-sm hover:bg-accent"
              >
                API Docs
              </a>
              <hr className="border-border" />
              <button
                onClick={() => { clearAuth(); setMenuOpen(false) }}
                className="block w-full px-4 py-2 text-left text-sm hover:bg-accent"
              >
                Logout
              </button>
            </div>
          </>
        )}
      </div>
    </header>
  )
}
 
interface SidebarProps {
  open: boolean
}
 
function Sidebar({ open }: SidebarProps) {
  const location = useLocation()
  const navItems = [
    { label: 'Dashboard', to: '/' },
    { label: 'Projects', to: '/projects' },
    { label: 'Runs', to: '/runs' },
    { label: 'Repositories', to: '/repositories' },
  ]
 
  function isActive(to: string): boolean {
    if (to === '/') return location.pathname === '/' || location.pathname === '/dashboard'
    return location.pathname.startsWith(to)
  }
 
  return (
    <nav
      role="navigation"
      data-open={open}
      className={
        open
          ? 'sticky top-14 h-[calc(100vh-6rem)] w-56 shrink-0 overflow-y-auto border-r border-border bg-muted/30 p-4 max-sm:fixed max-sm:inset-y-14 max-sm:left-0 max-sm:z-40 max-sm:h-auto max-sm:w-56 max-sm:bg-background max-sm:shadow-lg'
          : 'hidden'
      }
    >
      <ul className="flex flex-col gap-1">
        {navItems.map((item) => {
          const active = isActive(item.to)
          return (
            <li key={item.to}>
              <a
                href={`#${item.to}`}
                className={`block rounded-md px-3 py-2 text-sm ${
                  active
                    ? 'bg-accent font-medium text-accent-foreground'
                    : 'hover:bg-accent/50'
                }`}
              >
                {item.label}
              </a>
            </li>
          )
        })}
      </ul>
      <div className="mt-6 border-t border-border pt-4">
        <p className="mb-2 px-3 text-xs text-muted-foreground">External</p>
        <ul className="flex flex-col gap-1">
          <li>
            <a
              href={`https://langfuse.${getEnv().DOMAIN}`}
              target="_blank"
              rel="noopener noreferrer"
              className="block rounded-md px-3 py-2 text-sm hover:bg-accent/50"
            >
              LangFuse
            </a>
          </li>
          {getEnv().VIKUNJA_ENABLED === 'true' && (
            <li>
              <a
                href={`https://vikunja.${getEnv().DOMAIN}`}
                target="_blank"
                rel="noopener noreferrer"
                className="block rounded-md px-3 py-2 text-sm hover:bg-accent/50"
              >
                Vikunja
              </a>
            </li>
          )}
        </ul>
      </div>
    </nav>
  )
}
 
function BottomBar() {
  const { status, responseTime, lastChecked } = useHealthStore()
  const [, setTick] = useState(0)
 
  useEffect(() => {
    const timer = setInterval(() => setTick((t) => t + 1), 1000)
    return () => clearInterval(timer)
  }, [])
 
  const secondsSinceCheck = lastChecked
    ? Math.floor((Date.now() - lastChecked) / 1000)
    : null
 
  const statusIcon =
    status === 'healthy' ? '\u25CF' : status === 'degraded' ? '\u25B2' : '\u2717'
  const statusColor =
    status === 'healthy'
      ? 'text-green-500'
      : status === 'degraded'
        ? 'text-yellow-500'
        : 'text-red-500'
 
  return (
    <footer
      role="contentinfo"
      className="sticky bottom-0 z-10 flex h-10 items-center justify-between border-t border-border bg-background px-4 text-xs text-muted-foreground"
    >
      <span className={`flex items-center gap-1 ${statusColor}`}>
        <span>{statusIcon}</span>
        <span className="first-letter:capitalize">{status}</span>
        {responseTime !== null && <span>| {responseTime}ms</span>}
        {secondsSinceCheck !== null && <span>| Checked {secondsSinceCheck}s ago</span>}
      </span>
      <span className="flex items-center gap-3">
        <a href={`https://api.${getEnv().DOMAIN}/api/v1/swagger`} target="_blank" rel="noopener noreferrer" className="hover:text-foreground">
          API Docs
        </a>
        <span>v{APP_VERSION}</span>
      </span>
    </footer>
  )
}