Skip to content

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

2.1 KB
Raw Blame
/* Copyright BoxLite AI, 2026 · SPDX-License-Identifier: AGPL-3.0 */
import { RoutePath } from '@/enums/RoutePath'

export const ORGANIZATION_SESSION_KEY = 'boxlite-organization-session'
export const ORGANIZATION_SESSION_EVENT = 'boxlite-organization-selected'

export function readOrganizationSession(userId: string | undefined): string | null {
  try {
    const saved = JSON.parse(sessionStorage.getItem(ORGANIZATION_SESSION_KEY) || 'null')
    return userId && saved?.userId === userId && typeof saved.organizationId === 'string' ? saved.organizationId : null
  } catch {
    return null
  }
}

export function readPreferredOrganization(userId: string | undefined): string | null {
  if (!userId) return null
  try {
    return localStorage.getItem(`boxlite-preferred-organization:${userId}`)
  } catch {
    return null
  }
}

export function saveOrganizationSession(userId: string, organizationId: string) {
  // Storage must succeed before navigation; otherwise reload would restore the previous context.
  sessionStorage.setItem(ORGANIZATION_SESSION_KEY, JSON.stringify({ userId, organizationId }))
  try {
    localStorage.setItem(`boxlite-preferred-organization:${userId}`, organizationId)
  } catch {
    // Remembering a preference is optional; the active tab session is already saved.
  }
}

export function organizationSwitchPath(pathname: string, search = ''): string {
  if (pathname === RoutePath.BILLING) {
    const params = new URLSearchParams(search)
    const code = params.get('code')
    if (params.get('tab') === 'wallet' && code) {
      return `${pathname}?${new URLSearchParams({ tab: 'wallet', code })}`
    }
  }
  const lists = [
    RoutePath.BOXES,
    RoutePath.IMAGES,
    RoutePath.CHECKPOINTS,
    RoutePath.VOLUMES,
    RoutePath.DOMAINS,
    RoutePath.CI,
    RoutePath.CI_CONNECTS,
    RoutePath.APPS,
    RoutePath.IMAGE_REGISTRIES,
    RoutePath.KEYS,
  ]
  if (lists.includes(pathname as RoutePath)) return pathname
  if (pathname.startsWith(RoutePath.IMAGES + '/')) return RoutePath.IMAGES
  if (pathname.startsWith(RoutePath.CI + '/')) return RoutePath.CI
  if (pathname.startsWith(RoutePath.APPS + '/')) return RoutePath.APPS
  return RoutePath.BOXES
}