"use client"; import { useEffect, useState, useCallback } from 'react'; import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'; import { Button } from '@/components/ui/button'; import { auth, db } from '@/lib/firebase/config'; import { collection, query, where, getDocs, orderBy, limit } from 'firebase/firestore'; import { RefreshCw, CheckCircle2, AlertCircle, Link as LinkIcon } from 'lucide-react'; interface SessionDebugInfo { id: string; projectId?: string; workspacePath?: string; workspaceName?: string; needsProjectAssociation: boolean; model?: string; tokensUsed?: number; cost?: number; createdAt: any; } export default function DebugSessionsPage() { const [sessions, setSessions] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [userId, setUserId] = useState(''); const loadSessions = useCallback(async () => { setLoading(true); setError(null); try { const user = auth.currentUser; if (!user) { setError('Not authenticated'); setLoading(false); return; } setUserId(user.uid); const sessionsRef = collection(db, 'sessions'); // Remove orderBy to avoid index issues - just get recent sessions const sessionsQuery = query( sessionsRef, where('userId', '==', user.uid), limit(50) ); const snapshot = await getDocs(sessionsQuery); const sessionsData = snapshot.docs.map(doc => { const data = doc.data(); return { id: doc.id, projectId: data.projectId || null, workspacePath: data.workspacePath || null, workspaceName: data.workspaceName || null, needsProjectAssociation: data.needsProjectAssociation || false, model: data.model, tokensUsed: data.tokensUsed, cost: data.cost, createdAt: data.createdAt, }; }); console.log('DEBUG: All sessions from Firebase:', sessionsData); setSessions(sessionsData); } catch (err: any) { console.error('Error loading sessions:', err); setError(err.message); } finally { setLoading(false); } }, []); useEffect(() => { let mounted = true; const unsubscribe = auth.onAuthStateChanged((user) => { if (!mounted) return; if (user) { loadSessions(); } else { setError('Please sign in'); setLoading(false); } }); return () => { mounted = false; unsubscribe(); }; }, [loadSessions]); const unassociatedSessions = sessions.filter(s => s.needsProjectAssociation); const associatedSessions = sessions.filter(s => !s.needsProjectAssociation); // Group unassociated sessions by workspace path const sessionsByWorkspace = unassociatedSessions.reduce((acc, session) => { const path = session.workspacePath || 'No workspace path'; if (!acc[path]) acc[path] = []; acc[path].push(session); return acc; }, {} as Record); return (

🔍 Sessions Debug Page

View all your chat sessions and their workspace paths

{userId && (

User ID: {userId}

)}
{error && (

Error: {error}

)} {loading && !error && (

Loading sessions...

)} {!loading && !error && ( <> {/* Summary */} Summary

Total Sessions

{sessions.length}

Linked to Projects

{associatedSessions.length}

Unassociated (Available)

{unassociatedSessions.length}

{/* Unassociated Sessions by Workspace */} {unassociatedSessions.length > 0 && ( Unassociated Sessions (Available to Link) {Object.entries(sessionsByWorkspace).map(([path, workspaceSessions]) => { const folderName = path !== 'No workspace path' ? path.split('/').pop() : null; return (

📁 {folderName || 'Unknown folder'}

{path}

{workspaceSessions.length}

sessions

{workspaceSessions.slice(0, 3).map((session) => (
{session.id.substring(0, 12)}... {session.model || 'unknown'}
{session.tokensUsed?.toLocaleString()} tokens • ${session.cost?.toFixed(4)}
))} {workspaceSessions.length > 3 && (

+ {workspaceSessions.length - 3} more sessions...

)}

💡 To link these sessions:

  1. Create a project with workspace path: {path}
  2. OR connect GitHub to a project that already has this workspace path set

Folder name: {folderName}

); })}
)} {/* Associated Sessions */} {associatedSessions.length > 0 && ( Linked Sessions

These sessions are already linked to projects

{associatedSessions.slice(0, 5).map((session) => (
{session.id.substring(0, 12)}...

{session.workspaceName || 'No workspace'}

Project: {session.projectId?.substring(0, 8)}...

))}
)} {sessions.length === 0 && (

No sessions found. Start coding with Cursor to track sessions!

)} )}
); }