"use client"; import { useState, useEffect } from 'react'; import { Card, CardContent, CardHeader, CardTitle, CardDescription } from '@/components/ui/card'; import { Button } from '@/components/ui/button'; import { auth, db } from '@/lib/firebase/config'; import { doc, getDoc } from 'firebase/firestore'; import { toast } from 'sonner'; import { Loader2, Link as LinkIcon, CheckCircle2 } from 'lucide-react'; import { useParams } from 'next/navigation'; interface Project { id: string; productName: string; githubRepo?: string; workspacePath?: string; } export default function AssociateSessionsPage() { const params = useParams(); const projectId = params.projectId as string; const [project, setProject] = useState(null); const [loading, setLoading] = useState(true); const [associating, setAssociating] = useState(false); const [result, setResult] = useState(null); useEffect(() => { loadProject(); }, [projectId]); const loadProject = async () => { try { const projectDoc = await getDoc(doc(db, 'projects', projectId)); if (projectDoc.exists()) { setProject({ id: projectDoc.id, ...projectDoc.data() } as Project); } } catch (error) { console.error('Error loading project:', error); toast.error('Failed to load project'); } finally { setLoading(false); } }; const handleAssociateSessions = async () => { if (!project?.githubRepo) { toast.error('Project does not have a GitHub repository connected'); return; } setAssociating(true); setResult(null); try { const user = auth.currentUser; if (!user) { toast.error('Please sign in'); return; } const token = await user.getIdToken(); const response = await fetch(`/api/projects/${projectId}/associate-github-sessions`, { method: 'POST', headers: { 'Authorization': `Bearer ${token}`, 'Content-Type': 'application/json', }, body: JSON.stringify({ githubRepo: project.githubRepo, }), }); if (response.ok) { const data = await response.json(); setResult(data); if (data.sessionsAssociated > 0) { toast.success(`Success!`, { description: `Linked ${data.sessionsAssociated} existing chat sessions to this project`, }); } else { toast.info('No unassociated sessions found for this repository'); } } else { const error = await response.json(); toast.error(error.error || 'Failed to associate sessions'); } } catch (error) { console.error('Error:', error); toast.error('An error occurred'); } finally { setAssociating(false); } }; if (loading) { return (
); } return (

Associate Existing Sessions

Find and link chat sessions from this GitHub repository

Project Details Current project configuration

Product Name

{project?.productName}

{project?.githubRepo && (

GitHub Repository

{project.githubRepo}

)} {project?.workspacePath && (

Workspace Path

{project.workspacePath}

)}
Find Matching Sessions Search your database for chat sessions that match this project's GitHub repository

How it works:

  • Searches for sessions with matching GitHub repository
  • Also checks sessions from matching workspace paths
  • Only links sessions that aren't already assigned to a project
  • Updates all matched sessions to link to this project
{!project?.githubRepo && (

Connect a GitHub repository first to use this feature

)}
{result && ( Results

Sessions Linked

{result.sessionsAssociated}

{result.details && ( <>

Exact GitHub Matches

{result.details.exactMatches}

Path Matches

{result.details.pathMatches}

)}

{result.message}

)}
); }