Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
81 lines
3.7 KiB
React
81 lines
3.7 KiB
React
import './git.less';
|
|
import React, { useEffect, useState } from 'react';
|
|
import Nav from '@navbar/nav.jsx';
|
|
import { gitApi } from './gitApi.js';
|
|
|
|
// Git-Status im Editor-Navbar (ADR 0003 §7): Branch, Pfad, kurzer SHA, Fehler; Pull; Verknüpfen/Lösen.
|
|
const short = (sha)=>sha ? sha.slice(0, 7) : '';
|
|
|
|
const GitNavItem = ({ brew, lastSavedTime })=>{
|
|
const editId = brew?.editId;
|
|
const [status, setStatus] = useState(null); // null: unbekannt/aus
|
|
const [conns, setConns] = useState(null);
|
|
const [form, setForm] = useState({ connectionId: '', branch: '', path: '' });
|
|
const [busy, setBusy] = useState(false);
|
|
const [error, setError] = useState('');
|
|
|
|
const refresh = ()=>gitApi.status(editId).then(setStatus).catch(()=>setStatus(null));
|
|
useEffect(()=>{ if(editId) refresh(); }, [editId, lastSavedTime]);
|
|
|
|
// Verbindungen nur laden, wenn verknüpft werden soll (403/503: Git nicht verfügbar → Item bleibt unsichtbar)
|
|
useEffect(()=>{
|
|
if(status && !status.linked && conns === null) {
|
|
gitApi.listConnections().then((list)=>{ setConns(list); setForm((f)=>({ ...f, connectionId: list[0]?.id ?? '' })); }).catch(()=>setConns(false));
|
|
}
|
|
}, [status]);
|
|
|
|
if(!editId || !status) return null;
|
|
|
|
const run = async (fn)=>{
|
|
setBusy(true); setError('');
|
|
try { await fn(); await refresh(); } catch (err) { setError(`${err.HBErrorCode ?? ''} ${err.message}`.trim()); }
|
|
setBusy(false);
|
|
};
|
|
|
|
if(status.linked) {
|
|
return (
|
|
<Nav.dropdown className='gitNav'>
|
|
<Nav.item icon={status.lastError ? 'fas fa-exclamation-triangle' : 'fab fa-git-alt'} className={status.lastError ? 'error' : ''}>
|
|
{status.branch}
|
|
</Nav.item>
|
|
<div className='gitDropdown'>
|
|
<div className='meta'>{status.connectionLabel}: {status.repoUrl}</div>
|
|
<div className='meta'>{status.branch} · {status.path}{status.lastCommitSha ? ` · ${short(status.lastCommitSha)}` : ''}</div>
|
|
{status.lastError === 'G04' && <div className='error'>Git nicht erreichbar, angezeigt wird der zuletzt gespeicherte Stand.</div>}
|
|
{status.lastError && status.lastError !== 'G04' && <div className='error'>Letzter Fehler: {status.lastError}</div>}
|
|
{error && <div className='error'>{error}</div>}
|
|
<button disabled={busy} onClick={()=>run(async ()=>{
|
|
if(!window.confirm('Remote-Stand laden? Ungespeicherte Änderungen im Editor gehen verloren.')) return;
|
|
await gitApi.pull(editId);
|
|
window.location.reload();
|
|
})}>Pull</button>
|
|
{status.isConnectionOwner && <button disabled={busy} onClick={()=>run(async ()=>{
|
|
if(window.confirm('Verknüpfung lösen? Der Brew bleibt erhalten, die Datei im Repository auch.')) await gitApi.unlinkBrew(editId);
|
|
})}>Verknüpfung lösen</button>}
|
|
</div>
|
|
</Nav.dropdown>
|
|
);
|
|
}
|
|
|
|
if(!conns || conns.length === 0) return null; // nichts zu verknüpfen (kein OIDC-Konto oder keine Verbindung)
|
|
|
|
return (
|
|
<Nav.dropdown className='gitNav'>
|
|
<Nav.item icon='fab fa-git-alt'>Git</Nav.item>
|
|
<div className='gitDropdown'>
|
|
<select value={form.connectionId} onChange={(e)=>setForm({ ...form, connectionId: e.target.value })}>
|
|
{conns.map((c)=><option key={c.id} value={c.id}>{c.label}</option>)}
|
|
</select>
|
|
<input placeholder='Branch (Standard der Verbindung)' value={form.branch} onChange={(e)=>setForm({ ...form, branch: e.target.value })} />
|
|
<input placeholder='Pfad, z. B. brews/mein-brew.md' value={form.path} onChange={(e)=>setForm({ ...form, path: e.target.value })} />
|
|
{error && <div className='error'>{error}</div>}
|
|
<button disabled={busy || !form.connectionId} onClick={()=>run(()=>gitApi.linkBrew(editId, { connectionId: form.connectionId, branch: form.branch || undefined, path: form.path || undefined }))}>
|
|
In Git speichern
|
|
</button>
|
|
</div>
|
|
</Nav.dropdown>
|
|
);
|
|
};
|
|
|
|
export default GitNavItem;
|