Refactor commit pagination and logic in renderer.js
This commit is contained in:
127
renderer.js
127
renderer.js
@@ -493,15 +493,32 @@ async function startLiveCountdown(folderObj, msLeft) {
|
|||||||
if (idx === -1) return 1; // fallback: erste Seite
|
if (idx === -1) return 1; // fallback: erste Seite
|
||||||
return Math.floor(idx / pageSize) + 1;
|
return Math.floor(idx / pageSize) + 1;
|
||||||
}
|
}
|
||||||
|
async function getPageForCurrentCommit(folderObj, pageSize = 50) { //should probably be fused with the function above
|
||||||
|
// Holt alle Commits (nur die Hashes, schnell genug für moderne Maschinen)
|
||||||
|
const { head, total } = await window.electronAPI.getCommits(folderObj, 1, 100000); // Großer Wert, um alle zu holen (du kannst auch einen eigenen IPC machen, der NUR die Hashes returned)
|
||||||
|
const { commits } = await window.electronAPI.getCommits(folderObj, 1, total);
|
||||||
|
const idx = commits.findIndex(c => c.hash === head);
|
||||||
|
if (idx === -1) return 1;
|
||||||
|
return Math.floor(idx / pageSize) + 1;
|
||||||
|
}
|
||||||
|
|
||||||
//let commitPage = 1; // Merker für die aktuelle Seite
|
//let commitPage = 1; // Merker für die aktuelle Seite
|
||||||
const PAGE_SIZE = 50;
|
const PAGE_SIZE = 50;
|
||||||
|
|
||||||
// UI-Element für Pagination (in deinem Template anlegen, z.B. unter contentList)
|
|
||||||
const paginationEl = document.createElement('div');
|
const paginationEl = document.createElement('div');
|
||||||
paginationEl.className = 'pagination flex justify-center items-center my-2 space-x-2';
|
paginationEl.className = 'pagination flex justify-center items-center my-2 space-x-2';
|
||||||
|
contentList.parentElement.insertBefore(paginationEl, contentList); // nur einmal beim Initialisieren
|
||||||
|
|
||||||
contentList.parentElement.insertBefore(paginationEl, contentList); // einmalig nach DOM load
|
// Speichere zuletzt angezeigten Folder/Seite
|
||||||
|
let lastFolderPath = null;
|
||||||
|
let lastPage = null;
|
||||||
|
|
||||||
|
// Helper: gibt die Seite für einen Commit-Hash zurück
|
||||||
|
async function getCommitPageForHash(folderObj, hash, pageSize) {
|
||||||
|
const allHashes = await window.electronAPI.getAllCommitHashes(folderObj); // Muss in Main implementiert sein!
|
||||||
|
const idx = allHashes.findIndex(h => h.startsWith(hash));
|
||||||
|
return idx === -1 ? 1 : Math.floor(idx / pageSize) + 1;
|
||||||
|
}
|
||||||
|
|
||||||
async function renderContent(folderObj, page) {
|
async function renderContent(folderObj, page) {
|
||||||
closeDropdown();
|
closeDropdown();
|
||||||
@@ -509,42 +526,40 @@ async function startLiveCountdown(folderObj, msLeft) {
|
|||||||
await updateInteractionBar(folderObj);
|
await updateInteractionBar(folderObj);
|
||||||
titleEl.textContent = folder;
|
titleEl.textContent = folder;
|
||||||
|
|
||||||
// --- Neuer Block: Ermittle die Seite des aktuellen Commits ---
|
// --- Seitenwahl beim Ordnerwechsel ---
|
||||||
let usePage = page;
|
let usePage = page;
|
||||||
if (!usePage) {
|
if (!usePage || folder !== lastFolderPath) {
|
||||||
// Wenn keine Seite angegeben, gehe auf die Seite des aktuellen HEAD
|
// Wenn keine Seite gegeben oder Ordner gewechselt: Seite für aktuellen HEAD suchen
|
||||||
const { head } = await window.electronAPI.getCommits(folderObj, 1, 1); // head holen
|
const { head } = await window.electronAPI.getCommits(folderObj, 1, 1); // nur für den Hash!
|
||||||
usePage = await getCommitPageForHash(folderObj, head, PAGE_SIZE);
|
usePage = await getCommitPageForHash(folderObj, head, PAGE_SIZE);
|
||||||
}
|
}
|
||||||
// Holt die paginierten Commits!
|
lastFolderPath = folder;
|
||||||
const { head, commits, total, page: currentPage, pageSize, pages } =
|
lastPage = usePage;
|
||||||
await window.electronAPI.getCommits(folderObj, page, PAGE_SIZE);
|
|
||||||
|
|
||||||
commitPage = currentPage; // speichere aktuelle Seite
|
// Paginierte Commits holen
|
||||||
|
const { head, commits, total, page: currentPage, pageSize, pages } =
|
||||||
|
await window.electronAPI.getCommits(folderObj, usePage, PAGE_SIZE);
|
||||||
|
|
||||||
if (!commits || !commits.length) {
|
if (!commits || !commits.length) {
|
||||||
contentList.innerHTML = '<div class="p-6 text-gray-500">No commits found.</div>';
|
contentList.innerHTML = '<div class="p-6 text-gray-500">No commits found.</div>';
|
||||||
paginationEl.innerHTML = '';
|
paginationEl.innerHTML = '';
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
contentList.innerHTML = commits.map(c => {
|
|
||||||
// Prüfe, ob der Commit in der Rewrite-Queue ist:
|
// Commit-Liste rendern
|
||||||
const isQueued = folderObj.llmCandidates && folderObj.llmCandidates.some(fullHash =>
|
contentList.innerHTML = commits.map(c => {
|
||||||
fullHash.startsWith(c.hash)
|
const isQueued = folderObj.llmCandidates && folderObj.llmCandidates.some(fullHash =>
|
||||||
);
|
fullHash.startsWith(c.hash)
|
||||||
if(isQueued) console.log("hi");
|
);
|
||||||
// Für random-Winkel (zwischen -10 und +15 Grad, z.B.)
|
return `
|
||||||
return `
|
<li style="position:relative;" class="w-full p-3 mb-2 bg-white border border-gray-200 rounded shadow-sm
|
||||||
<li style="position:relative;" class="w-full p-3 mb-2 bg-white border border-gray-200 rounded shadow-sm
|
${c.hash === head ? 'current-commit' : ''}">
|
||||||
${c.hash === head ? 'current-commit' : ''}">
|
|
||||||
<div class="flex justify-between text-sm text-gray-600 mb-1">
|
<div class="flex justify-between text-sm text-gray-600 mb-1">
|
||||||
<span>${c.hash}</span>
|
<span>${c.hash}</span>
|
||||||
<span>${new Date(c.date).toLocaleString()}
|
<span>${new Date(c.date).toLocaleString()}</span>
|
||||||
</span>
|
|
||||||
</div>
|
</div>
|
||||||
<div class="text-gray-800 mb-2">${c.message}</div>
|
<div class="text-gray-800 mb-2">${c.message}</div>
|
||||||
<div class="flex space-x-2 mb-2">
|
<div class="flex space-x-2 mb-2">
|
||||||
<!-- Changes-Button -->
|
|
||||||
<button class="diff-btn flex items-center px-2 py-1 text-xs border rounded hover:bg-gray-100" data-hash="${c.hash}">
|
<button class="diff-btn flex items-center px-2 py-1 text-xs border rounded hover:bg-gray-100" data-hash="${c.hash}">
|
||||||
<svg xmlns="http://www.w3.org/2000/svg" class="h-4 w-4 mr-1 rotate" viewBox="0 0 24 24"
|
<svg xmlns="http://www.w3.org/2000/svg" class="h-4 w-4 mr-1 rotate" viewBox="0 0 24 24"
|
||||||
stroke="currentColor" fill="none">
|
stroke="currentColor" fill="none">
|
||||||
@@ -553,7 +568,6 @@ async function startLiveCountdown(folderObj, msLeft) {
|
|||||||
</svg>
|
</svg>
|
||||||
Changes
|
Changes
|
||||||
</button>
|
</button>
|
||||||
<!-- Snapshot-Button -->
|
|
||||||
<button class="snapshot-btn flex items-center px-2 py-1 text-xs border rounded hover:bg-gray-100" data-hash="${c.hash}">
|
<button class="snapshot-btn flex items-center px-2 py-1 text-xs border rounded hover:bg-gray-100" data-hash="${c.hash}">
|
||||||
<svg xmlns="http://www.w3.org/2000/svg" class="h-4 w-4 mr-1" fill="none" viewBox="0 0 24 24" stroke="currentColor">
|
<svg xmlns="http://www.w3.org/2000/svg" class="h-4 w-4 mr-1" fill="none" viewBox="0 0 24 24" stroke="currentColor">
|
||||||
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
|
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
|
||||||
@@ -561,12 +575,10 @@ async function startLiveCountdown(folderObj, msLeft) {
|
|||||||
</svg>
|
</svg>
|
||||||
Snapshot
|
Snapshot
|
||||||
</button>
|
</button>
|
||||||
<!-- Checkout-Button -->
|
|
||||||
<button
|
<button
|
||||||
class="checkout-btn flex items-center px-2 py-1 text-xs border rounded ${c.hash === head ? 'disabled' : 'hover:bg-gray-100'}"
|
class="checkout-btn flex items-center px-2 py-1 text-xs border rounded ${c.hash === head ? 'disabled' : 'hover:bg-gray-100'}"
|
||||||
data-hash="${c.hash}"
|
data-hash="${c.hash}"
|
||||||
${c.hash === head ? 'disabled' : ''}
|
${c.hash === head ? 'disabled' : ''}>
|
||||||
>
|
|
||||||
<svg xmlns="http://www.w3.org/2000/svg" class="h-4 w-4 mr-1" fill="none" viewBox="0 0 24 24" stroke="currentColor">
|
<svg xmlns="http://www.w3.org/2000/svg" class="h-4 w-4 mr-1" fill="none" viewBox="0 0 24 24" stroke="currentColor">
|
||||||
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
|
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
|
||||||
d="M15 12H3m12 0l-4-4m4 4l-4 4"/>
|
d="M15 12H3m12 0l-4-4m4 4l-4 4"/>
|
||||||
@@ -594,28 +606,27 @@ async function startLiveCountdown(folderObj, msLeft) {
|
|||||||
style="pointer-events: none; z-index:10;">`
|
style="pointer-events: none; z-index:10;">`
|
||||||
: ''
|
: ''
|
||||||
}
|
}
|
||||||
</li>`;}).join('');
|
</li>`;
|
||||||
|
}).join('');
|
||||||
|
|
||||||
// --- PAGINATION ---
|
// --- PAGINATION ---
|
||||||
if (pages > 1) {
|
if (pages > 1) {
|
||||||
paginationEl.innerHTML = `
|
paginationEl.innerHTML = `
|
||||||
<button id="page-prev" class="px-2 py-1 border rounded" ${currentPage === 1 ? 'disabled' : ''}>«</button>
|
<button id="page-prev" class="px-2 py-1 border rounded" ${currentPage === 1 ? 'disabled' : ''}>«</button>
|
||||||
<span class="mx-2 text-sm">Seite ${currentPage} / ${pages}</span>
|
<span class="mx-2 text-sm">Seite ${currentPage} / ${pages}</span>
|
||||||
<button id="page-next" class="px-2 py-1 border rounded" ${currentPage === pages ? 'disabled' : ''}>»</button>
|
<button id="page-next" class="px-2 py-1 border rounded" ${currentPage === pages ? 'disabled' : ''}>»</button>
|
||||||
`;
|
`;
|
||||||
|
paginationEl.querySelector('#page-prev').onclick = () => renderContent(folderObj, currentPage - 1);
|
||||||
paginationEl.querySelector('#page-prev').onclick = () => renderContent(folderObj, currentPage - 1);
|
paginationEl.querySelector('#page-next').onclick = () => renderContent(folderObj, currentPage + 1);
|
||||||
paginationEl.querySelector('#page-next').onclick = () => renderContent(folderObj, currentPage + 1);
|
paginationEl.style.display = 'flex';
|
||||||
paginationEl.style.display = 'flex';
|
} else {
|
||||||
} else {
|
paginationEl.innerHTML = '';
|
||||||
paginationEl.innerHTML = '';
|
paginationEl.style.display = 'none';
|
||||||
paginationEl.style.display = 'none';
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Diff-Buttons prüfen und ggf. deaktivieren
|
// Diff-Buttons prüfen und ggf. deaktivieren
|
||||||
contentList.querySelectorAll('.diff-btn').forEach(async btn => {
|
contentList.querySelectorAll('.diff-btn').forEach(async btn => {
|
||||||
const hash = btn.dataset.hash;
|
const hash = btn.dataset.hash;
|
||||||
const diffText = await window.electronAPI.diffCommit(folderObj, hash);
|
const diffText = await window.electronAPI.diffCommit(folderObj, hash);
|
||||||
if (!diffText.trim()) {
|
if (!diffText.trim()) {
|
||||||
btn.disabled = true;
|
btn.disabled = true;
|
||||||
@@ -626,14 +637,13 @@ async function startLiveCountdown(folderObj, msLeft) {
|
|||||||
// Diff-Toggle & Highlighting
|
// Diff-Toggle & Highlighting
|
||||||
contentList.querySelectorAll('.diff-btn').forEach(btn => {
|
contentList.querySelectorAll('.diff-btn').forEach(btn => {
|
||||||
btn.addEventListener('click', async () => {
|
btn.addEventListener('click', async () => {
|
||||||
const li = btn.closest('li');
|
const li = btn.closest('li');
|
||||||
const hash = btn.dataset.hash;
|
const hash = btn.dataset.hash;
|
||||||
const svg = btn.querySelector('svg');
|
const svg = btn.querySelector('svg');
|
||||||
const container = li.querySelector('.diff-container');
|
const container = li.querySelector('.diff-container');
|
||||||
const pre = container.querySelector('pre');
|
const pre = container.querySelector('pre');
|
||||||
|
|
||||||
if (!pre.innerHTML.trim()) {
|
if (!pre.innerHTML.trim()) {
|
||||||
// fetch und HTML-Snippet bauen
|
|
||||||
const diff = await window.electronAPI.diffCommit(folderObj, hash);
|
const diff = await window.electronAPI.diffCommit(folderObj, hash);
|
||||||
const escaped = diff
|
const escaped = diff
|
||||||
.replace(/&/g, '&')
|
.replace(/&/g, '&')
|
||||||
@@ -653,7 +663,7 @@ async function startLiveCountdown(folderObj, msLeft) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const isOpen = container.classList.toggle('open');
|
const isOpen = container.classList.toggle('open');
|
||||||
if (isOpen) {
|
if (isOpen) {
|
||||||
container.style.maxHeight = container.scrollHeight + 'px';
|
container.style.maxHeight = container.scrollHeight + 'px';
|
||||||
} else {
|
} else {
|
||||||
container.style.maxHeight = '0';
|
container.style.maxHeight = '0';
|
||||||
@@ -678,23 +688,21 @@ async function startLiveCountdown(folderObj, msLeft) {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// Checkout-Button
|
// Checkout-Button: Seite neu bestimmen!
|
||||||
contentList.querySelectorAll('.checkout-btn').forEach(btn => {
|
contentList.querySelectorAll('.checkout-btn').forEach(btn => {
|
||||||
btn.addEventListener('click', async () => {
|
btn.addEventListener('click', async () => {
|
||||||
const hash = btn.dataset.hash;
|
const hash = btn.dataset.hash;
|
||||||
await window.electronAPI.checkoutCommit(folderObj, hash);
|
await window.electronAPI.checkoutCommit(folderObj, hash);
|
||||||
// Finde die Seite des neuen HEADs (das ist jetzt der aktuelle Commit)
|
const page = await getCommitPageForHash(folderObj, hash, PAGE_SIZE);
|
||||||
const headCommit = hash;
|
|
||||||
const page = await getCommitPageForHash(folderObj, headCommit, PAGE_SIZE);
|
|
||||||
await renderContent(folderObj, page);
|
await renderContent(folderObj, page);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// Copy-Diff-Button
|
// Copy-Diff-Button
|
||||||
contentList.querySelectorAll('.diff-container').forEach(container => {
|
contentList.querySelectorAll('.diff-container').forEach(container => {
|
||||||
const btn = container.querySelector('.copy-diff-btn');
|
const btn = container.querySelector('.copy-diff-btn');
|
||||||
const pre = container.querySelector('pre');
|
const pre = container.querySelector('pre');
|
||||||
const originalSVG = btn.innerHTML;
|
const originalSVG = btn.innerHTML;
|
||||||
const checkSVG = `
|
const checkSVG = `
|
||||||
<svg xmlns="http://www.w3.org/2000/svg"
|
<svg xmlns="http://www.w3.org/2000/svg"
|
||||||
class="h-4 w-4 text-green-600"
|
class="h-4 w-4 text-green-600"
|
||||||
@@ -717,10 +725,11 @@ async function startLiveCountdown(folderObj, msLeft) {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// --- Aktuellen Commit in die Mitte scrollen (falls vorhanden) ---
|
||||||
const currentEl = contentList.querySelector('li.current-commit');
|
const currentEl = contentList.querySelector('li.current-commit');
|
||||||
if (currentEl) {
|
if (currentEl) {
|
||||||
currentEl.scrollIntoView({ behavior: 'smooth', block: 'center' });
|
currentEl.scrollIntoView({ behavior: 'smooth', block: 'center' });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
await renderSidebar();
|
await renderSidebar();
|
||||||
|
|||||||
Reference in New Issue
Block a user