Skip to content

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

6.9 KB
Raw Blame
/* Copyright 2026 BoxLite AI. SPDX-License-Identifier: AGPL-3.0 */
import { createContext, useContext, useEffect, useRef, useState, type ReactNode } from 'react'
import { useQuery, useQueryClient } from '@tanstack/react-query'
import { useAuth } from 'react-oidc-context'
import { useApi } from '@/hooks/useApi'
import { useConfig } from '@/hooks/useConfig'
import { useSelectedOrganization } from '@/hooks/useSelectedOrganization'
import { useConsoleAnalytics } from '@/hooks/useConsoleAnalytics'
import { clearSignupSource, takeSignupSource } from '@/lib/signup-attribution'
import { Button } from '@/components/ui/button'
import { Switch } from '@/components/ui/switch'
import { analyticsNoticeVersion, type AnalyticsPreference } from '../../../shared/console-analytics'

type ConsolePreference = AnalyticsPreference & { enabledFrom: string | null }
type Choice = {
  allowed: boolean | null | undefined
  saving: boolean
  error: string | null
  save: (allowed: boolean) => void
  refresh: () => void
}
const Preferences = createContext<Choice | null>(null)

export function ProductAnalyticsProvider({ children }: { children: ReactNode }) {
  const { user, isAuthenticated } = useAuth()
  const { selectedOrganization } = useSelectedOrganization()
  const { posthog } = useConfig()
  const sub = isAuthenticated && user?.profile.sub
  if (!sub || !selectedOrganization?.id || !posthog?.host || !posthog.apiKey) return children
  return (
    <AccountPreference
      key={JSON.stringify([sub, selectedOrganization.id])}
      sub={sub}
      organizationId={selectedOrganization.id}
    >
      {children}
    </AccountPreference>
  )
}

function AccountPreference({
  sub,
  organizationId,
  children,
}: {
  sub: string
  organizationId: string
  children: ReactNode
}) {
  const api = useApi()
  const queryClient = useQueryClient()
  const [saving, setSaving] = useState(false)
  const [saveError, setSaveError] = useState<string | null>(null)
  const writing = useRef(false)
  const query = useQuery({
    queryKey: ['product-analytics', sub, organizationId],
    queryFn: async ({ signal }): Promise<ConsolePreference> => {
      const http = api.axiosInstance
      return (await http.get('/analytics/organizations/' + encodeURIComponent(organizationId) + '/consent', { signal }))
        .data
    },
    retry: false,
    refetchOnMount: 'always',
  })
  const allowed = query.data?.allowed
  useConsoleAnalytics(organizationId, query.isError ? null : query.data?.enabledFrom)

  useEffect(() => {
    const source = takeSignupSource(sub, query.isError ? undefined : allowed)
    if (source) void api.axiosInstance.post('/analytics/signup-source', source, { timeout: 2000 }).catch(() => {})
  }, [api, sub, allowed, query.isError])

  const save = async (next: boolean) => {
    if (writing.current) return
    writing.current = true
    setSaving(true)
    setSaveError(null)
    try {
      const { data: decision } = await api.axiosInstance.put<AnalyticsPreference>('/analytics/consent', {
        allowed: next,
        version: analyticsNoticeVersion,
      })
      const accountQueries = { queryKey: ['product-analytics', sub] }
      // Discard consent reads that started before the decision was saved.
      await queryClient.cancelQueries(accountQueries)
      if (decision.allowed === false) {
        clearSignupSource()
        queryClient.setQueriesData<ConsolePreference>(accountQueries, { ...decision, enabledFrom: null })
      } else {
        // A personal grant still needs the organization's creator grant.
        await queryClient.invalidateQueries(accountQueries)
      }
    } catch {
      setSaveError('Could not confirm your preference. Check your connection and try again.')
    } finally {
      setSaving(false)
      writing.current = false
    }
  }
  return (
    <Preferences.Provider
      value={{
        allowed,
        saving,
        save,
        error: saveError || (query.isError ? 'Could not load your preference.' : null),
        refresh: () => void query.refetch(),
      }}
    >
      {children}
    </Preferences.Provider>
  )
}

export function WelcomeAnalyticsConsent() {
  const choice = useContext(Preferences)
  if (!choice || choice.allowed === undefined) return null
  const optedIn = choice.allowed === true
  return (
    <div data-analytics-preferences className="mt-3 flex items-start gap-2.5 text-muted-foreground">
      <input
        id="welcome-product-analytics"
        type="checkbox"
        checked={optedIn}
        disabled={choice.saving || optedIn}
        onChange={() => choice.save(true)}
        className="mt-1 size-4 shrink-0 cursor-pointer accent-foreground"
      />
      <div>
        <label htmlFor="welcome-product-analytics" className="cursor-pointer text-body leading-6">
          Share Console/Feature usage with PostHog to help improve BoxLite.
        </label>
        {choice.error && (
          <p role="alert" className="mt-2 text-meta text-destructive">
            {choice.error}
          </p>
        )}
      </div>
    </div>
  )
}

export function ProductAnalyticsSettings() {
  const choice = useContext(Preferences)
  return (
    <section data-analytics-preferences aria-labelledby="privacy-preferences" className="border border-border p-5">
      <h2 id="privacy-preferences" className="text-em font-semibold">
        Privacy Preferences
      </h2>
      <div className="mt-4 flex items-start justify-between gap-4">
        <div>
          <label htmlFor="product-analytics" className="text-body font-semibold">
            Product analytics
          </label>
          <p id="analytics-scope" className="mt-1 max-w-lg text-meta text-muted-foreground">
            Share usage with PostHog for your account and the organizations you created. Other members must also agree
            before their activity is shared.
          </p>
        </div>
        <div className="flex items-center gap-2">
          <span aria-live="polite" className="text-meta">
            {!choice
              ? 'Unavailable'
              : choice.saving
                ? 'Saving…'
                : choice.allowed === undefined
                  ? choice.error
                    ? 'Unavailable'
                    : 'Loading…'
                  : choice.allowed
                    ? 'On'
                    : 'Off'}
          </span>
          <Switch
            id="product-analytics"
            aria-describedby="analytics-scope"
            checked={choice?.allowed === true}
            disabled={!choice || choice.saving || choice.allowed === undefined}
            onCheckedChange={(value) => choice?.save(value)}
          />
        </div>
      </div>
      {choice?.error && (
        <div className="mt-2 flex items-center gap-2">
          <p role="alert" className="text-meta text-destructive">
            {choice.error}
          </p>
          <Button variant="outline" size="sm" onClick={choice.refresh}>
            Refresh
          </Button>
        </div>
      )}
    </section>
  )
}