Files
homebrewery-oidc/client/fork/git/GitConnectionsPanel.jsx
T

92 lines
4.5 KiB
React

import './git.less';
import React, { useEffect, useState } from 'react';
import { gitApi } from './gitApi.js';
// Verbindungen verwalten (ADR 0003 §7): anlegen, testen, löschen, Brews aus dem Repo importieren.
// Das Token-Feld ist reines Eingabefeld und wird nie vorbefüllt; der Server gibt den Token nie zurück.
const EMPTY = { label: '', repoUrl: '', authUsername: 'git', token: '' };
const GitConnectionsPanel = ()=>{
const [state, setState] = useState('loading'); // loading | off | ready
const [conns, setConns] = useState([]);
const [form, setForm] = useState(EMPTY);
const [msg, setMsg] = useState({}); // pro Verbindung: { ok, text }
const [formError, setFormError] = useState('');
const [files, setFiles] = useState({}); // pro Verbindung: { branch, files }
const reload = ()=>gitApi.listConnections().then((list)=>{ setConns(list); setState('ready'); })
.catch((err)=>{ if([403, 503].includes(err.status)) setState('off'); else { setFormError(err.message); setState('ready'); } });
useEffect(()=>{ reload(); }, []);
if(state !== 'ready') return null; // Git-Backend deaktiviert oder kein OIDC-Konto
const set = (id, ok, text)=>setMsg((m)=>({ ...m, [id]: { ok, text } }));
const create = async (e)=>{
e.preventDefault();
setFormError('');
try {
await gitApi.createConnection(form);
setForm(EMPTY);
await reload();
} catch (err) { setFormError(err.message); }
};
const test = (c)=>gitApi.testConnection(c.id)
.then((r)=>{ set(c.id, true, `Verbindung ok (Standard-Branch ${r.defaultBranch ?? '?'}, ${r.branchCount} Branches)`); reload(); })
.catch((err)=>set(c.id, false, `${err.HBErrorCode ?? ''} ${err.message}`));
const remove = async (c)=>{
if(!window.confirm(`Verbindung „${c.label}“ löschen? Verknüpfte Brews bleiben als normale Brews erhalten.`)) return;
await gitApi.deleteConnection(c.id).catch((err)=>set(c.id, false, err.message));
reload();
};
const showFiles = (c)=>gitApi.listFiles(c.id, c.defaultBranch, c.brewDir)
.then((r)=>setFiles((f)=>({ ...f, [c.id]: { branch: r.branch, files: r.files } })))
.catch((err)=>set(c.id, false, `${err.HBErrorCode ?? ''} ${err.message}`));
const importFile = (c, branch, path)=>gitApi.importBrew({ connectionId: c.id, branch, path })
.then((brew)=>{ window.location.href = `/edit/${brew.editId}`; })
.catch((err)=>set(c.id, false, `${err.HBErrorCode ?? ''} ${err.message}`));
return (
<div className='dataGroup gitConnections'>
<h3>Git-Verbindungen <i className='fab fa-git-alt'></i></h3>
<p>Brews in einem beliebigen Git-Repository speichern (HTTPS + Personal Access Token). Der Token wird verschlüsselt gespeichert und nie angezeigt.</p>
{conns.map((c)=>(
<div className='gitConn' key={c.id}>
<div className='label'>{c.label}</div>
<div className='url'>{c.repoUrl}</div>
<div className='meta'>Standard-Branch {c.defaultBranch}, Ordner {c.brewDir || '/'}{c.lastVerifiedAt ? '' : ' · noch nicht getestet'}</div>
<div className='actions'>
<button onClick={()=>test(c)}>Testen</button>
<button onClick={()=>showFiles(c)}>Brews aus Repo importieren</button>
<button onClick={()=>remove(c)}>Löschen</button>
</div>
{msg[c.id] && <div className={msg[c.id].ok ? 'ok' : 'error'}>{msg[c.id].text}</div>}
{files[c.id] && (
<ul className='files'>
{files[c.id].files.length === 0 && <li>Keine .md-Dateien in {c.brewDir || '/'} auf {files[c.id].branch}.</li>}
{files[c.id].files.map((f)=>(
<li key={f}>{f} <button onClick={()=>importFile(c, files[c.id].branch, f)}>Importieren</button></li>
))}
</ul>
)}
</div>
))}
<form onSubmit={create}>
<strong>Neue Verbindung</strong>
<input placeholder='Bezeichnung' value={form.label} required maxLength={80} onChange={(e)=>setForm({ ...form, label: e.target.value })} />
<input placeholder='https://git.example.org/nutzer/repo.git' value={form.repoUrl} required onChange={(e)=>setForm({ ...form, repoUrl: e.target.value })} />
<input placeholder='Benutzername (Standard: git)' value={form.authUsername} onChange={(e)=>setForm({ ...form, authUsername: e.target.value })} />
<input type='password' autoComplete='off' placeholder='Personal Access Token' value={form.token} required onChange={(e)=>setForm({ ...form, token: e.target.value })} />
<button type='submit'>Verbindung anlegen</button>
{formError && <div className='error'>{formError}</div>}
</form>
</div>
);
};
export default GitConnectionsPanel;