import React, { useEffect, useState } from 'react'; import { motion } from 'framer-motion'; import { toast } from 'react-toastify'; import { useStore } from '@nanostores/react'; import { logStore } from '~/lib/stores/logs'; import { classNames } from '~/utils/classNames'; import { vercelConnection, isConnecting, isFetchingStats, updateVercelConnection, fetchVercelStats, } from '~/lib/stores/vercel'; export default function VercelConnection() { const connection = useStore(vercelConnection); const connecting = useStore(isConnecting); const fetchingStats = useStore(isFetchingStats); const [isProjectsExpanded, setIsProjectsExpanded] = useState(false); useEffect(() => { const fetchProjects = async () => { if (connection.user && connection.token) { await fetchVercelStats(connection.token); } }; fetchProjects(); }, [connection.user, connection.token]); const handleConnect = async (event: React.FormEvent) => { event.preventDefault(); isConnecting.set(true); try { const response = await fetch('https://api.vercel.com/v2/user', { headers: { Authorization: `Bearer ${connection.token}`, 'Content-Type': 'application/json', }, }); if (!response.ok) { throw new Error('Invalid token or unauthorized'); } const userData = (await response.json()) as any; updateVercelConnection({ user: userData.user || userData, // Handle both possible structures token: connection.token, }); await fetchVercelStats(connection.token); toast.success('Successfully connected to Vercel'); } catch (error) { console.error('Auth error:', error); logStore.logError('Failed to authenticate with Vercel', { error }); toast.error('Failed to connect to Vercel'); updateVercelConnection({ user: null, token: '' }); } finally { isConnecting.set(false); } }; const handleDisconnect = () => { updateVercelConnection({ user: null, token: '' }); toast.success('Disconnected from Vercel'); }; console.log('connection', connection); return (

Vercel Connection

{!connection.user ? (
updateVercelConnection({ ...connection, token: e.target.value })} disabled={connecting} placeholder="Enter your Vercel personal access token" className={classNames( 'w-full px-3 py-2 rounded-lg text-sm', 'bg-[#F8F8F8] dark:bg-[#1A1A1A]', 'border border-[#E5E5E5] dark:border-[#333333]', 'text-bolt-elements-textPrimary placeholder-bolt-elements-textTertiary', 'focus:outline-none focus:ring-1 focus:ring-bolt-elements-borderColorActive', 'disabled:opacity-50', )} />
Get your token
) : (
Connected to Vercel
{/* Debug output */}
{JSON.stringify(connection.user, null, 2)}
User Avatar

{connection.user?.username || connection.user?.user?.username || 'Vercel User'}

{connection.user?.email || connection.user?.user?.email || 'No email available'}

{fetchingStats ? (
Fetching Vercel projects...
) : (
{isProjectsExpanded && connection.stats?.projects?.length ? (
{connection.stats.projects.map((project) => (
{project.name}
) : isProjectsExpanded ? (
No projects found in your Vercel account
) : null}
)}
)}
); }