The micro-VM for AI agents — light enough to embed on your laptop, elastic enough to power an agentic cloud.
/* Copyright 2026 BoxLite AI. SPDX-License-Identifier: AGPL-3.0 */
import { hasAuthParams } from 'react-oidc-context'
import {
campaignLabel,
signupSourceLifetimeMs,
validSource,
type SignupSource,
} from '../../../shared/signup-attribution'
const key = 'boxlite.signup-source'
type Entry = { value: SignupSource; subject?: string }
let expiryTimer: ReturnType<typeof setTimeout> | undefined
export function clearSignupSource() {
clearTimeout(expiryTimer)
expiryTimer = undefined
try {
window.sessionStorage.removeItem(key)
} catch {
// Browsers can disable tab storage.
}
}
function readEntry(): Entry | undefined {
const raw = window.sessionStorage.getItem(key)
if (!raw) return
const entry: Entry = JSON.parse(raw)
const value = entry?.value
const age = Date.now() - Date.parse(value?.enteredAt)
if (
!value ||
!Number.isFinite(age) ||
age < 0 ||
age >= signupSourceLifetimeMs ||
!validSource(value.source, value.kind)
) {
clearSignupSource()
return
}
clearTimeout(expiryTimer)
expiryTimer = setTimeout(clearSignupSource, signupSourceLifetimeMs - age)
return entry
}
function referrerHostname(forwarded?: string): string | undefined {
if (forwarded && validSource(forwarded, 'referrer')) return forwarded
if (!document.referrer || hasAuthParams()) return
try {
const { protocol, hostname } = new URL(document.referrer)
if (protocol !== 'https:' && protocol !== 'http:') return
if (hostname !== window.location.hostname && validSource(hostname, 'referrer')) return hostname
} catch {
// An unparseable referrer provides no signup source.
}
}
// Called before auth or the router reads the URL. Keep OAuth parameters and unrelated anchors intact.
export function captureSignupEntry() {
const url = new URL(window.location.href)
if (!['/', '/dashboard', '/dashboard/boxes'].includes(url.pathname)) return
const label = (key: string) => {
const value = url.searchParams.get(key)
return value && campaignLabel.test(value) ? value.toLowerCase() : undefined
}
const utm = label('utm_source')
const ref = label('ref')
const fragment = /^#bl_referrer=([a-z0-9.-]+)$/.exec(url.hash)
const kind = utm ? 'utm' : ref ? 'ref' : 'referrer'
const source = utm || ref || referrerHostname(fragment?.[1])
const medium = label('utm_medium')
const campaign = label('utm_campaign')
const content = label('utm_content')
const before = url.href
for (const name of [
'utm_source',
'utm_medium',
'utm_campaign',
'utm_content',
'utm_term',
'ref',
'gclid',
'fbclid',
'msclkid',
'twclid',
'li_fat_id',
]) {
if (url.searchParams.has(name)) url.searchParams.delete(name)
}
if (url.hash.startsWith('#bl_referrer=')) url.hash = ''
try {
if (before !== url.href) window.history.replaceState(window.history.state, '', url)
if (readEntry() || !source) return
const value: SignupSource = { source, kind, enteredAt: new Date().toISOString(), medium, campaign, content }
window.sessionStorage.setItem(key, JSON.stringify({ value }))
expiryTimer = setTimeout(clearSignupSource, signupSourceLifetimeMs)
} catch {
clearSignupSource()
}
}
// Bind a pending tab to its first authenticated account. Consume once, only after a saved grant.
export function takeSignupSource(subject: string, allowed: boolean | null | undefined): SignupSource | undefined {
try {
const entry = readEntry()
if (!entry) return
if (allowed === false || (entry.subject && entry.subject !== subject)) {
clearSignupSource()
return
}
if (!allowed) {
if (!entry.subject) window.sessionStorage.setItem(key, JSON.stringify({ ...entry, subject }))
return
}
clearSignupSource()
return entry.value
} catch {
clearSignupSource()
}
}