Spaces:
Paused
Paused
Create frontend/src/components/GitPanel.tsx
Browse files
frontend/src/components/GitPanel.tsx
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import React, { useState, useEffect } from 'react';
|
| 2 |
+
import axios from 'axios';
|
| 3 |
+
import { FaCodeBranch, FaHistory, FaPen } from 'react-icons/fa';
|
| 4 |
+
|
| 5 |
+
const GitPanel: React.FC<{ projectPath: string }> = ({ projectPath }) => {
|
| 6 |
+
const [branches, setBranches] = useState<string[]>([]);
|
| 7 |
+
const [logs, setLogs] = useState<any[]>([]);
|
| 8 |
+
const [commitMsg, setCommitMsg] = useState('');
|
| 9 |
+
const [generating, setGenerating] = useState(false);
|
| 10 |
+
|
| 11 |
+
const fetchBranches = async () => {
|
| 12 |
+
try {
|
| 13 |
+
const res = await axios.post('/api/git', { action: 'branch', project_path: projectPath });
|
| 14 |
+
setBranches(res.data.branches || []);
|
| 15 |
+
} catch (e) {}
|
| 16 |
+
};
|
| 17 |
+
|
| 18 |
+
const fetchLog = async () => {
|
| 19 |
+
try {
|
| 20 |
+
const res = await axios.post('/api/git', { action: 'log', project_path: projectPath });
|
| 21 |
+
setLogs(res.data.log || []);
|
| 22 |
+
} catch (e) {}
|
| 23 |
+
};
|
| 24 |
+
|
| 25 |
+
const generateMessage = async () => {
|
| 26 |
+
setGenerating(true);
|
| 27 |
+
try {
|
| 28 |
+
// Use the chat API to generate a commit message from the diff
|
| 29 |
+
const diffRes = await axios.post('/api/git', { action: 'diff', project_path: projectPath }); // we need a diff action
|
| 30 |
+
// For simplicity, just call the chat endpoint
|
| 31 |
+
const resp = await fetch('/api/chat', {
|
| 32 |
+
method: 'POST',
|
| 33 |
+
headers: { 'Content-Type': 'application/json' },
|
| 34 |
+
body: JSON.stringify({
|
| 35 |
+
model: 'groq-llama-3.1-8b-instant',
|
| 36 |
+
messages: [{ role: 'user', content: 'Generate a concise git commit message for the following changes:\n' + (diffRes.data.diff || '') }],
|
| 37 |
+
project_path: projectPath,
|
| 38 |
+
}),
|
| 39 |
+
});
|
| 40 |
+
const reader = resp.body?.getReader();
|
| 41 |
+
let msg = '';
|
| 42 |
+
while (reader) {
|
| 43 |
+
const { done, value } = await reader.read();
|
| 44 |
+
if (done) break;
|
| 45 |
+
msg += new TextDecoder().decode(value);
|
| 46 |
+
// parse the stream (simplified)
|
| 47 |
+
}
|
| 48 |
+
setCommitMsg(msg.replace(/data: /g, '').replace(/\[DONE\]/g, '').trim());
|
| 49 |
+
} catch (e) {}
|
| 50 |
+
setGenerating(false);
|
| 51 |
+
};
|
| 52 |
+
|
| 53 |
+
const commit = async () => {
|
| 54 |
+
await axios.post('/api/git', { action: 'commit', project_path: projectPath, args: { message: commitMsg } });
|
| 55 |
+
fetchLog();
|
| 56 |
+
setCommitMsg('');
|
| 57 |
+
};
|
| 58 |
+
|
| 59 |
+
useEffect(() => {
|
| 60 |
+
fetchBranches();
|
| 61 |
+
fetchLog();
|
| 62 |
+
}, []);
|
| 63 |
+
|
| 64 |
+
return (
|
| 65 |
+
<div style={{ height: '100%', display: 'flex', flexDirection: 'column', background: 'var(--bg-primary)', padding: 12 }}>
|
| 66 |
+
<div style={{ fontWeight: 600, marginBottom: 8 }}><FaCodeBranch /> Git</div>
|
| 67 |
+
<div style={{ marginBottom: 12 }}>
|
| 68 |
+
<div style={{ fontSize: 12, color: 'var(--text-secondary)' }}>Branches</div>
|
| 69 |
+
{branches.map(b => (
|
| 70 |
+
<div key={b} style={{ padding: '2px 8px', cursor: 'pointer', background: 'var(--bg-tertiary)', borderRadius: 4, marginTop: 4, fontSize: 13 }}>
|
| 71 |
+
{b}
|
| 72 |
+
</div>
|
| 73 |
+
))}
|
| 74 |
+
</div>
|
| 75 |
+
<div style={{ flex: 1, overflowY: 'auto' }}>
|
| 76 |
+
<div style={{ fontSize: 12, color: 'var(--text-secondary)' }}>Recent commits</div>
|
| 77 |
+
{logs.map((log, i) => (
|
| 78 |
+
<div key={i} style={{ padding: '4px 0', borderBottom: '1px solid var(--border)', fontSize: 12 }}>
|
| 79 |
+
<div style={{ color: 'var(--accent)' }}>{log.hash?.substring(0,7)}</div>
|
| 80 |
+
<div>{log.message}</div>
|
| 81 |
+
</div>
|
| 82 |
+
))}
|
| 83 |
+
</div>
|
| 84 |
+
<div style={{ marginTop: 8 }}>
|
| 85 |
+
<textarea
|
| 86 |
+
style={{ width: '100%', height: 50, background: 'var(--bg-tertiary)', color: 'var(--text-primary)', border: '1px solid var(--border)', borderRadius: 4, padding: 4, fontSize: 12 }}
|
| 87 |
+
value={commitMsg}
|
| 88 |
+
onChange={e => setCommitMsg(e.target.value)}
|
| 89 |
+
placeholder="Commit message..."
|
| 90 |
+
/>
|
| 91 |
+
<div style={{ display: 'flex', gap: 8, marginTop: 4 }}>
|
| 92 |
+
<button onClick={generateMessage} disabled={generating} style={{ background: 'var(--accent)', color: 'white', padding: '4px 8px', borderRadius: 4, fontSize: 12, display: 'flex', alignItems: 'center', gap: 4 }}>
|
| 93 |
+
<FaPen /> {generating ? '...' : 'AI msg'}
|
| 94 |
+
</button>
|
| 95 |
+
<button onClick={commit} style={{ background: 'var(--success)', color: 'white', padding: '4px 8px', borderRadius: 4, fontSize: 12 }}>Commit</button>
|
| 96 |
+
</div>
|
| 97 |
+
</div>
|
| 98 |
+
</div>
|
| 99 |
+
);
|
| 100 |
+
};
|
| 101 |
+
|
| 102 |
+
export default GitPanel;
|