mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 12:24:30 +01:00
- Rework useSidebarSections to track sections independently, open by default - Theme sonner toasts with app color tokens and the selected theme mode
284 lines
7.7 KiB
TypeScript
284 lines
7.7 KiB
TypeScript
|
|
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
|
import { createBrowserRouter, RouterProvider, Outlet, useNavigate, useLocation } from 'react-router-dom'
|
|
import { useEffect, useRef, useCallback } from 'react'
|
|
import { Toaster } from 'sonner'
|
|
import { Repos } from './pages/Repos'
|
|
import { RepoDetail } from './pages/RepoDetail'
|
|
import { SessionDetail } from './pages/SessionDetail'
|
|
import { Schedules } from './pages/Schedules'
|
|
import { GlobalSchedules } from './pages/GlobalSchedules'
|
|
import { Login } from './pages/Login'
|
|
import { Register } from './pages/Register'
|
|
import { Setup } from './pages/Setup'
|
|
import { AssistantRedirect } from './pages/AssistantRedirect'
|
|
import { SettingsDialog } from './components/settings/SettingsDialog'
|
|
import { VersionNotifier } from './components/VersionNotifier'
|
|
import { PwaUpdatePrompt } from '@/components/PwaUpdatePrompt'
|
|
import { MobileTabBar } from '@/components/navigation/MobileTabBar'
|
|
import { MobileSheetHost } from '@/components/navigation/MobileSheetHost'
|
|
import { RouteErrorBoundary } from '@/components/ui/route-error-boundary'
|
|
import { DesktopSidebar } from '@/components/navigation/DesktopSidebar'
|
|
import { useRightEdgeSwipe, useSwipeBack } from './hooks/useMobile'
|
|
import { useMobileTabBar } from '@/hooks/useMobileTabBar'
|
|
import { TTSProvider } from './contexts/TTSContext'
|
|
import { ThemeProvider, useThemeMode } from './contexts/ThemeContext'
|
|
import { AuthProvider } from './contexts/AuthContext'
|
|
import { EventProvider, usePermissions, useEventContext } from '@/contexts/EventContext'
|
|
import { SwipeNavigationProvider, useSwipeNavigation } from '@/contexts/SwipeNavigationContext'
|
|
import { PermissionRequestDialog } from './components/session/PermissionRequestDialog'
|
|
import { SSHHostKeyDialog } from './components/ssh/SSHHostKeyDialog'
|
|
import { loginLoader, setupLoader, registerLoader, protectedLoader } from './lib/auth-loaders'
|
|
import { getSwipeBackTarget } from '@/lib/navigation'
|
|
import { onNotificationClick } from '@/lib/serviceWorker'
|
|
import { useAuth } from '@/hooks/useAuth'
|
|
import { useOpenCodeFailureToast } from '@/hooks/useOpenCodeFailureToast'
|
|
import { useOpenCodeServerActions } from '@/hooks/useOpenCodeServerActions'
|
|
import { RestartServerDialog } from '@/components/settings/RestartServerDialog'
|
|
|
|
const queryClient = new QueryClient({
|
|
defaultOptions: {
|
|
queries: {
|
|
staleTime: 1000 * 10,
|
|
refetchOnWindowFocus: true,
|
|
},
|
|
},
|
|
})
|
|
|
|
function SSHHostKeyDialogWrapper() {
|
|
const { sshHostKey } = useEventContext()
|
|
return (
|
|
<SSHHostKeyDialog
|
|
request={sshHostKey.request}
|
|
onRespond={async (requestId, response) => {
|
|
await sshHostKey.respond(requestId, response === 'accept')
|
|
}}
|
|
/>
|
|
)
|
|
}
|
|
|
|
function ThemedToaster() {
|
|
const themeMode = useThemeMode()
|
|
return (
|
|
<Toaster
|
|
theme={themeMode ?? 'light'}
|
|
position="bottom-right"
|
|
expand={false}
|
|
richColors
|
|
closeButton
|
|
duration={2500}
|
|
/>
|
|
)
|
|
}
|
|
|
|
function HealthMonitor() {
|
|
const { isAuthenticated } = useAuth()
|
|
const {
|
|
restartServerMutation,
|
|
confirmOpen,
|
|
setConfirmOpen,
|
|
activeSessionCount,
|
|
requestRestart,
|
|
confirmRestart,
|
|
} = useOpenCodeServerActions()
|
|
useOpenCodeFailureToast(isAuthenticated, requestRestart)
|
|
return (
|
|
<RestartServerDialog
|
|
open={confirmOpen}
|
|
onOpenChange={setConfirmOpen}
|
|
activeSessionCount={activeSessionCount}
|
|
isRestarting={restartServerMutation.isPending}
|
|
onCancel={() => setConfirmOpen(false)}
|
|
onConfirm={confirmRestart}
|
|
/>
|
|
)
|
|
}
|
|
|
|
function PermissionDialogWrapper() {
|
|
const {
|
|
current: currentPermission,
|
|
pendingCount,
|
|
respond: replyToPermission,
|
|
showDialog,
|
|
setShowDialog,
|
|
} = usePermissions()
|
|
|
|
return (
|
|
<PermissionRequestDialog
|
|
permission={currentPermission}
|
|
pendingCount={pendingCount}
|
|
isFromDifferentSession={false}
|
|
onRespond={replyToPermission}
|
|
open={showDialog}
|
|
onOpenChange={setShowDialog}
|
|
repoDirectory={null}
|
|
/>
|
|
)
|
|
}
|
|
|
|
function AppShell() {
|
|
const navigate = useNavigate()
|
|
const location = useLocation()
|
|
const rootRef = useRef<HTMLDivElement>(null)
|
|
const { openSheet, open } = useMobileTabBar()
|
|
|
|
const swipeNav = useSwipeNavigation()
|
|
|
|
const getRouteSwipeBackTarget = useCallback(
|
|
() => getSwipeBackTarget(location.pathname, location.search),
|
|
[location.pathname, location.search]
|
|
)
|
|
|
|
const canSwipeBack = useCallback(
|
|
() => !swipeNav?.isSuspended() && getRouteSwipeBackTarget() !== null,
|
|
[swipeNav, getRouteSwipeBackTarget]
|
|
)
|
|
|
|
const handleSwipeBack = useCallback(() => {
|
|
const target = getRouteSwipeBackTarget()
|
|
if (target) navigate(target)
|
|
}, [getRouteSwipeBackTarget, navigate])
|
|
|
|
const { bind: bindRouteSwipe } = useSwipeBack(
|
|
() => {},
|
|
{
|
|
enabled: true,
|
|
suspendsRouteSwipe: false,
|
|
canBack: canSwipeBack,
|
|
onBack: handleSwipeBack,
|
|
}
|
|
)
|
|
|
|
const canOpenMoreWithSwipe = () => {
|
|
return /^\/repos\/[^/]+\/sessions\/[^/]+$/.test(location.pathname) && !openSheet
|
|
}
|
|
|
|
const { bind: bindMoreSwipe } = useRightEdgeSwipe(
|
|
() => open('more'),
|
|
{
|
|
enabled: canOpenMoreWithSwipe(),
|
|
edgeWidth: 32,
|
|
threshold: 60,
|
|
}
|
|
)
|
|
|
|
useEffect(() => {
|
|
const cleanup = bindRouteSwipe(rootRef.current)
|
|
return () => {
|
|
cleanup?.()
|
|
}
|
|
}, [bindRouteSwipe])
|
|
|
|
useEffect(() => {
|
|
const cleanup = bindMoreSwipe(rootRef.current)
|
|
return () => {
|
|
cleanup?.()
|
|
}
|
|
}, [bindMoreSwipe])
|
|
|
|
useEffect(
|
|
() =>
|
|
onNotificationClick((url) => {
|
|
if (window.location.pathname + window.location.search !== url) navigate(url)
|
|
}),
|
|
[navigate]
|
|
)
|
|
|
|
return (
|
|
<ThemeProvider>
|
|
<AuthProvider>
|
|
<EventProvider>
|
|
<div ref={rootRef} className="flex h-dvh w-full min-w-0">
|
|
<DesktopSidebar />
|
|
<main className="flex-1 min-w-0 min-h-0 flex flex-col">
|
|
<Outlet />
|
|
</main>
|
|
</div>
|
|
<MobileTabBar />
|
|
<MobileSheetHost />
|
|
<PermissionDialogWrapper />
|
|
<SSHHostKeyDialogWrapper />
|
|
<SettingsDialog />
|
|
<HealthMonitor />
|
|
<VersionNotifier />
|
|
<PwaUpdatePrompt />
|
|
<ThemedToaster />
|
|
</EventProvider>
|
|
</AuthProvider>
|
|
</ThemeProvider>
|
|
)
|
|
}
|
|
|
|
const router = createBrowserRouter([
|
|
{
|
|
element: <AppShell />,
|
|
errorElement: <RouteErrorBoundary />,
|
|
children: [
|
|
{
|
|
path: '/login',
|
|
element: <Login />,
|
|
loader: loginLoader,
|
|
},
|
|
{
|
|
path: '/register',
|
|
element: <Register />,
|
|
loader: registerLoader,
|
|
},
|
|
{
|
|
path: '/setup',
|
|
element: <Setup />,
|
|
loader: setupLoader,
|
|
},
|
|
{
|
|
path: '/',
|
|
element: <Repos />,
|
|
loader: protectedLoader,
|
|
},
|
|
{
|
|
path: '/assistant',
|
|
element: <AssistantRedirect />,
|
|
loader: protectedLoader,
|
|
},
|
|
{
|
|
path: '/repos/:id',
|
|
element: <RepoDetail />,
|
|
loader: protectedLoader,
|
|
},
|
|
{
|
|
path: '/repos/:id/assistant',
|
|
element: <AssistantRedirect />,
|
|
loader: protectedLoader,
|
|
},
|
|
{
|
|
path: '/repos/:id/sessions/:sessionId',
|
|
element: <SessionDetail />,
|
|
loader: protectedLoader,
|
|
},
|
|
{
|
|
path: '/repos/:id/schedules',
|
|
element: <Schedules />,
|
|
loader: protectedLoader,
|
|
},
|
|
{
|
|
path: '/schedules',
|
|
element: <GlobalSchedules />,
|
|
loader: protectedLoader,
|
|
},
|
|
],
|
|
},
|
|
])
|
|
|
|
function App() {
|
|
return (
|
|
<QueryClientProvider client={queryClient}>
|
|
<TTSProvider>
|
|
<SwipeNavigationProvider>
|
|
<RouterProvider router={router} />
|
|
</SwipeNavigationProvider>
|
|
</TTSProvider>
|
|
</QueryClientProvider>
|
|
)
|
|
}
|
|
|
|
export default App
|