Skip to content

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

1.4 KB
Raw Blame
/* Copyright 2026 BoxLite AI · SPDX-License-Identifier: AGPL-3.0 */
import { createElement, useCallback } from 'react'
import { toast } from 'sonner'
import { clearPendingPlanUpgrade } from '@/lib/pending-plan-upgrade'
import { useFetchOwnerCheckoutUrlQuery, useIsOwnerCheckoutUrlFetching } from './queries/billingQueries'

export function useConnectCard() {
  const fetchCheckoutUrl = useFetchOwnerCheckoutUrlQuery()
  const isPending = useIsOwnerCheckoutUrlFetching()

  const connectCard = useCallback(async () => {
    clearPendingPlanUpgrade()
    const newWindow = window.open('', '_blank')
    try {
      const url = await fetchCheckoutUrl()
      if (newWindow) newWindow.location.href = url
      else {
        toast.info('Continue on Stripe', {
          description: 'Your browser blocked the new tab.',
          action: createElement(
            'a',
            {
              href: url,
              target: '_blank',
              rel: 'noopener noreferrer',
              className: 'font-medium underline underline-offset-4',
            },
            'Open Stripe',
          ),
          duration: Infinity,
          closeButton: true,
        })
      }
    } catch (error) {
      newWindow?.close()
      toast.error('Failed to fetch checkout url', {
        description: String(error),
      })
    }
  }, [fetchCheckoutUrl])

  return { connectCard, isPending }
}