Skip to content

The micro-VM for AI agents — light enough to embed on your laptop, elastic enough to power an agentic cloud.

10.7 KB
Raw Blame
/*
 * Copyright 2025 Daytona Platforms Inc.
 * Modified by BoxLite AI, 2025-2026
 * SPDX-License-Identifier: AGPL-3.0
 */

import { OrganizationGate } from './components/OrganizationExperience'
import { NotificationSocketProvider } from '@/providers/NotificationSocketProvider'
import { OrganizationsProvider } from '@/providers/OrganizationsProvider'
import { SelectedOrganizationProvider } from '@/providers/SelectedOrganizationProvider'
import { initPylon } from '@/vendor/pylon'
import { usePostHog } from 'posthog-js/react'
import React, { Suspense, useEffect } from 'react'
import { useAuth } from 'react-oidc-context'
import { Navigate, Outlet, Route, Routes, useLocation } from 'react-router-dom'
import { BannerProvider } from './components/Banner'
import LoadingFallback from './components/LoadingFallback'
import { RegistrationGate } from './components/RegistrationGate'
import { BOXLITE_DOCS_URL, BOXLITE_SLACK_URL } from './constants/ExternalLinks'
import { RoutePath, getRouteSubPath } from './enums/RoutePath'
import { useConfig } from './hooks/useConfig'
import Dashboard from './pages/Dashboard'
import LandingPage from './pages/LandingPage'
import Logout from './pages/Logout'
import NotFound from './pages/NotFound'
import Boxes from './pages/Boxes'
import Redeem from './pages/Redeem'
import { ApiProvider } from './providers/ApiProvider'
import { RegionsProvider } from './providers/RegionsProvider'
import { BoxSessionProvider } from './providers/BoxSessionProvider'

// Code-split the heavier, not-first-paint routes out of the main bundle. They
// load on demand under the dashboard <Outlet> Suspense boundary, so the initial
// /boxes paint no longer ships Admin/Billing/Settings/Keys/box-details and their
// recharts/terminal deps. Boxes + the Dashboard shell stay eager (first paint).
const CiConnectCallback = React.lazy(() => import('./pages/CiConnectCallback'))
const Keys = React.lazy(() => import('./pages/Keys'))
// Billing pulls in the plan/wallet/usage sections (and recharts) behind this one chunk.
const Billing = React.lazy(() => import('./pages/Billing'))
const BillingPlanChange = React.lazy(() => import('./pages/BillingPlanChange'))
const Volumes = React.lazy(() => import('./pages/Volumes'))
const Domains = React.lazy(() => import('./pages/Domains'))
const Apps = React.lazy(() => import('./pages/Apps'))
const AppDetails = React.lazy(() => import('./components/apps/AppDetails'))
const FirstBoxWelcome = React.lazy(() => import('./components/first-run/FirstBoxWelcome'))
const FirstBoxQuickstart = React.lazy(() =>
  import('./components/first-run/FirstBoxOnboarding').then((m) => ({ default: m.FirstBoxQuickstart })),
)
const Images = React.lazy(() => import('./pages/ImagesPage'))
const CiPage = React.lazy(() => import('./pages/CiPage'))

const Checkpoints = React.lazy(() => import('./pages/Checkpoints'))
const ImageDetails = React.lazy(() => import('./pages/ImageDetails'))
const EmailVerify = React.lazy(() => import('./pages/EmailVerify'))
const OrganizationSettings = React.lazy(() => import('@/pages/OrganizationSettings'))
const UserOrganizationInvitations = React.lazy(() => import('@/pages/UserOrganizationInvitations'))
const BoxDetails = React.lazy(() => import('./components/boxes').then((m) => ({ default: m.BoxDetails })))

const HIDDEN_DASHBOARD_ROUTES = [
  RoutePath.ROLES,
  RoutePath.AUDIT_LOGS,
  RoutePath.REGIONS,
  RoutePath.RUNNERS,
  RoutePath.EXPERIMENTAL,
  RoutePath.WEBHOOKS,
  RoutePath.WEBHOOK_ENDPOINT_DETAILS,
]

// Simple redirection components for external URLs
const DocsRedirect = () => {
  React.useEffect(() => {
    window.open(BOXLITE_DOCS_URL, '_blank')
    window.location.href = RoutePath.DASHBOARD
  }, [])
  return null
}

const SlackRedirect = () => {
  React.useEffect(() => {
    window.open(BOXLITE_SLACK_URL, '_blank')
    window.location.href = RoutePath.DASHBOARD
  }, [])
  return null
}

// Same-origin OIDC silent-renew iframes are legitimate, so frame refusal
// belongs in deployment headers. The terminal Paste action also refuses to
// read clipboard when the dashboard itself is framed.

function App() {
  const config = useConfig()
  const location = useLocation()
  const posthog = usePostHog()
  const { error: authError, isAuthenticated, user } = useAuth()
  const boxesRedirect = `${RoutePath.BOXES}${location.search}`

  useEffect(() => {
    if (isAuthenticated && user && posthog?.get_distinct_id() !== user.profile.sub) {
      posthog?.identify(user.profile.sub)
    }
    if (import.meta.env.PROD && config.pylonAppId && isAuthenticated && user) {
      initPylon(config.pylonAppId, {
        chat_settings: {
          app_id: config.pylonAppId,
          email: user.profile.email || '',
          name: user.profile.name || '',
          avatar_url: user.profile.picture,
          email_hash: user.profile?.email_hash as string | undefined,
        },
      })
    }
  }, [isAuthenticated, user, posthog, config.pylonAppId])

  if (authError && location.pathname !== RoutePath.LOGOUT) return <LandingPage />

  return (
    <Routes>
      <Route path={RoutePath.LANDING} element={<LandingPage />} />
      <Route path="/lander" element={<Navigate to={RoutePath.LANDING} replace />} />
      <Route
        path="/settings/ci/callback"
        element={
          <Suspense fallback={<LoadingFallback />}>
            <ApiProvider>
              <CiConnectCallback />
            </ApiProvider>
          </Suspense>
        }
      />
      <Route path={RoutePath.LOGOUT} element={<Logout />} />
      <Route path={RoutePath.REDEEM} element={<Redeem />} />
      <Route path={RoutePath.DOCS} element={<DocsRedirect />} />
      <Route path={RoutePath.SLACK} element={<SlackRedirect />} />
      <Route
        path={RoutePath.DASHBOARD}
        element={
          <Suspense fallback={<LoadingFallback />}>
            <ApiProvider>
              <RegistrationGate key={user?.profile.sub}>
                <OrganizationsProvider>
                  <OrganizationGate>
                    <SelectedOrganizationProvider>
                      <RegionsProvider>
                        <NotificationSocketProvider>
                          <BannerProvider>
                            <Dashboard />
                          </BannerProvider>
                        </NotificationSocketProvider>
                      </RegionsProvider>
                    </SelectedOrganizationProvider>
                  </OrganizationGate>
                </OrganizationsProvider>
              </RegistrationGate>
            </ApiProvider>
          </Suspense>
        }
      >
        <Route index element={<Navigate to={boxesRedirect} replace />} />
        <Route path={getRouteSubPath(RoutePath.KEYS)} element={<Keys />} />
        <Route path={getRouteSubPath(RoutePath.BOXES)} element={<Boxes />} />
        <Route path={getRouteSubPath(RoutePath.APPS)} element={<Apps />} />
        <Route path={getRouteSubPath(RoutePath.APP_DETAILS)} element={<AppDetails />} />
        <Route path={getRouteSubPath(RoutePath.WELCOME)} element={<FirstBoxWelcome />} />
        <Route path={getRouteSubPath(RoutePath.QUICKSTART)} element={<FirstBoxQuickstart />} />
        <Route path={getRouteSubPath(RoutePath.VOLUMES)} element={<Volumes />} />
        <Route path={getRouteSubPath(RoutePath.DOMAINS)} element={<Domains />} />
        <Route path={getRouteSubPath(RoutePath.IMAGES)} element={<Images tab="images" />} />
        <Route path={getRouteSubPath(RoutePath.CI)} element={<CiPage tab="runners" />} />
        <Route path={getRouteSubPath(RoutePath.CI_CONNECTS)} element={<CiPage tab="connects" />} />

        <Route path={getRouteSubPath(RoutePath.CHECKPOINTS)} element={<Checkpoints />} />
        {/* The static segment is declared before `:idOrRef`, as the API orders its routes. */}
        <Route path={getRouteSubPath(RoutePath.IMAGE_REGISTRIES)} element={<Images tab="registries" />} />
        <Route path={getRouteSubPath(RoutePath.IMAGE_DETAILS)} element={<ImageDetails />} />
        <Route
          path={getRouteSubPath(RoutePath.REGISTRIES)}
          element={<Navigate to={RoutePath.IMAGE_REGISTRIES} replace />}
        />
        {/* Plan, wallet and usage are sections of the one Billing page. The old
            per-surface paths stay as redirects so existing links keep working.
            The route is open to any member: the wallet/plan data is owner-scoped by
            hooks/queries/billingQueries.ts, and the plan-switching surface is gated
            on the owner role inside components/billing/PlanSection.tsx. */}
        <Route path={getRouteSubPath(RoutePath.BILLING)} element={<Billing />} />
        <Route path={getRouteSubPath(RoutePath.BILLING_PLAN_CHANGE)} element={<BillingPlanChange />} />
        {/* Without this the bare path falls past the dashboard shell to the
            top-level NotFound instead of the plan grid it is missing an id for. */}
        <Route path={getRouteSubPath(RoutePath.BILLING_PLAN)} element={<Navigate to={RoutePath.BILLING} replace />} />
        <Route
          path={getRouteSubPath(RoutePath.BILLING_SPENDING)}
          element={<Navigate to={RoutePath.BILLING} replace />}
        />
        <Route path={getRouteSubPath(RoutePath.BILLING_WALLET)} element={<Navigate to={RoutePath.BILLING} replace />} />
        <Route path={getRouteSubPath(RoutePath.LIMITS)} element={<Navigate to={RoutePath.BILLING} replace />} />
        <Route path={getRouteSubPath(RoutePath.PRICING)} element={<Navigate to={RoutePath.BILLING} replace />} />
        {/* Keep the same BoxDetails component and box-scoped activation state across both views.
            Shell lifetime belongs to the Terminal page and ends when it unmounts. */}
        <Route
          element={
            <BoxSessionProvider>
              <Outlet />
            </BoxSessionProvider>
          }
        >
          <Route path={getRouteSubPath(RoutePath.BOX_TERMINAL)} element={<BoxDetails page="terminal" />} />
          <Route path={getRouteSubPath(RoutePath.BOX_DETAILS)} element={<BoxDetails />} />
        </Route>
        {HIDDEN_DASHBOARD_ROUTES.map((path) => (
          <Route key={path} path={getRouteSubPath(path)} element={<Navigate to={boxesRedirect} replace />} />
        ))}
        <Route path={getRouteSubPath(RoutePath.EMAIL_VERIFY)} element={<EmailVerify />} />
        <Route path={getRouteSubPath(RoutePath.SETTINGS)} element={<OrganizationSettings />} />
        <Route
          path={getRouteSubPath(RoutePath.MEMBERS)}
          element={<Navigate to={`${RoutePath.SETTINGS}?tab=member`} replace />}
        />
        <Route path={getRouteSubPath(RoutePath.USER_INVITATIONS)} element={<UserOrganizationInvitations />} />
        <Route path={getRouteSubPath(RoutePath.ONBOARDING)} element={<Navigate to={RoutePath.QUICKSTART} replace />} />
      </Route>
      <Route path="*" element={<NotFound />} />
    </Routes>
  )
}

export default App