import React, { useState, useEffect } from 'react'; import axios from 'axios'; import { FaCodeBranch, FaHistory, FaPen, FaFileAlt } from 'react-icons/fa'; import ReactMarkdown from 'react-markdown'; const GitPanel: React.FC<{ projectPath: string }> = ({ projectPath }) => { const [branches, setBranches] = useState([]); const [logs, setLogs] = useState([]); const [commitMsg, setCommitMsg] = useState(''); const [generating, setGenerating] = useState(false); const [prSummary, setPrSummary] = useState(''); const fetchData = async () => { try { const [branchesRes, logsRes] = await Promise.all([ axios.post('/api/git', { action: 'branch', project_path: projectPath }), axios.post('/api/git', { action: 'log', project_path: projectPath }) ]); setBranches(branchesRes.data.branches || []); setLogs(logsRes.data.log || []); } catch (e) {} }; useEffect(() => { fetchData(); }, []); const generateCommitMessage = async () => { setGenerating(true); try { const diffRes = await axios.post('/api/git', { action: 'diff', project_path: projectPath }); const diff = diffRes.data.diff || ''; const resp = await fetch('/api/chat', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ model: 'groq-llama-3.1-8b-instant', messages: [{ role: 'user', content: `Generate a concise git commit message for the following diff:\n${diff}` }], project_path: projectPath, }), }); const reader = resp.body?.getReader(); let msg = ''; while (reader) { const { done, value } = await reader.read(); if (done) break; const text = new TextDecoder().decode(value); const lines = text.split('\n').filter(l => l.startsWith('data: ')).map(l => l.slice(6)); for (const line of lines) { if (line === '[DONE]') break; try { const { token } = JSON.parse(line); msg += token; } catch (e) {} } } setCommitMsg(msg.replace(/ERROR:.*/, '').trim()); } catch (e) {} setGenerating(false); }; const generatePRSummary = async () => { try { const diffRes = await axios.post('/api/git', { action: 'diff', project_path: projectPath }); const diff = diffRes.data.diff || ''; const resp = await fetch('/api/chat', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ model: 'groq-llama-3.3-70b-versatile', messages: [{ role: 'user', content: `Review the following diff and provide a PR summary with suggestions:\n${diff}` }], project_path: projectPath, }), }); const reader = resp.body?.getReader(); let summary = ''; while (reader) { const { done, value } = await reader.read(); if (done) break; const text = new TextDecoder().decode(value); const lines = text.split('\n').filter(l => l.startsWith('data: ')).map(l => l.slice(6)); for (const line of lines) { if (line === '[DONE]') break; try { const { token } = JSON.parse(line); summary += token; } catch (e) {} } } setPrSummary(summary); } catch (e) {} }; const commit = async () => { await axios.post('/api/git', { action: 'commit', project_path: projectPath, args: { message: commitMsg } }); fetchData(); setCommitMsg(''); }; return (
Git
Branches
{branches.map(b => (
{b}
))}
Recent Commits
{logs.map((log, i) => (
{log.hash?.substring(0,7)} {log.message}
))}