Project Browsers 1.0

This commit is contained in:
Jakub Zych
2026-03-07 23:51:53 +01:00
commit 3f4920a68f
31 changed files with 1433 additions and 0 deletions

341
public/app.js Normal file
View File

@@ -0,0 +1,341 @@
(() => {
const grid = document.getElementById('grid');
const searchInput = document.getElementById('search');
const statsEl = document.getElementById('stats');
const screenshotAllBtn = document.getElementById('screenshot-all');
const logsModal = document.getElementById('logs-modal');
const logsTitle = document.getElementById('logs-title');
const logsBody = document.getElementById('logs-body');
const logsClose = document.getElementById('logs-close');
let projects = [];
let filter = 'all';
let searchTerm = '';
// -- Helpers --
function slugColor(slug) {
let hash = 0;
for (let i = 0; i < slug.length; i++) hash = slug.charCodeAt(i) + ((hash << 5) - hash);
return `hsl(${Math.abs(hash) % 360}, 45%, 25%)`;
}
function projectHash(p) {
return `${p.status || 'stopped'}|${p.apiStatus || 'stopped'}|${p.frontendStatus || ''}|${p._hasScreenshot ? 1 : 0}|${p._screenshotTs || 0}|${p.hasDocs ? 1 : 0}`;
}
function shouldShow(p) {
const status = p.status || 'stopped';
if (filter === 'running' && status !== 'running') return false;
if (filter === 'stopped' && status !== 'stopped') return false;
if (searchTerm && !p.name.toLowerCase().includes(searchTerm) && !p.slug.toLowerCase().includes(searchTerm)) return false;
return true;
}
// -- Card rendering --
function cardHTML(p) {
const status = p.status || 'stopped';
const isRunning = status === 'running';
const isBusy = status === 'starting' || status === 'stopping';
const screenshotSrc = `/screenshots/${p.slug}.png?t=${p._screenshotTs || 0}`;
const hasFrontend = !!p.frontend;
const imgInner = p._hasScreenshot
? `<img class="card-img" src="${screenshotSrc}" alt="${p.name}" loading="lazy">`
: `<div class="card-placeholder" style="background:${slugColor(p.slug)}">${p.name[0]}</div>`;
const imgHtml = `<div class="card-visual">
${imgInner}
<button class="btn-screenshot-icon" onclick="actions.screenshot('${p.slug}')" ${!isRunning ? 'disabled' : ''} title="Take screenshot">📷</button>
</div>`;
// Status dots: show API + Frontend for frontend projects
let statusDots;
if (hasFrontend) {
const apiSt = p.apiStatus || 'stopped';
const feSt = p.frontendStatus || 'stopped';
statusDots = `<div class="status-dots">
<div class="status-dot ${apiSt}" title="API: ${apiSt}"></div>
<div class="status-dot ${feSt}" title="Frontend: ${feSt}"></div>
</div>`;
} else {
statusDots = `<div class="status-dot ${status}"></div>`;
}
// Open link: frontend port for frontend projects, API port otherwise
const openPort = hasFrontend ? p.frontend.port : p.port;
// DB badge with relative path
const dbBadge = p.dbType ? `<span class="badge db">${p.dbType}${p.dbName ? ` (${p.dbName})` : ''}</span>` : '';
return `
${imgHtml}
<div class="card-body">
<div class="card-header">
${statusDots}
<div class="card-name" title="${p.slug}">${p.name}</div>
</div>
<div class="badges-wrap">
<div class="badges">
<span class="badge">:${p.port}</span>
${dbBadge}
${!p.hasEnv ? '<span class="badge" style="color:var(--red)">no .env</span>' : ''}
</div>
${hasFrontend ? `<div class="badges">
<span class="badge fe">:${p.frontend.port}</span>
<span class="badge fe">${p.frontend.dir}</span>
</div>` : ''}
</div>
<div class="card-path" title="${p.path}">${p.path}</div>
<div class="card-actions">
${isRunning
? `<button class="btn-stop" onclick="actions.stop('${p.slug}')" ${isBusy ? 'disabled' : ''}>Stop</button>`
: `<button class="btn-start" onclick="actions.start('${p.slug}')" ${isBusy ? 'disabled' : ''}>Start</button>`
}
${p.hasDocs ? `<a class="btn-docs" href="/docs/${p.slug}/" target="_blank">Docs</a>` : ''}
${isRunning
? `<a class="btn-open" href="http://${location.hostname}:${openPort}" target="_blank">Open</a>`
: `<button class="btn-open" onclick="actions.logs('${p.slug}')">Logs</button>`
}
</div>
</div>`;
}
// -- Smart DOM update --
const cardHashes = new Map();
function render() {
const running = projects.filter(p => p.status === 'running').length;
statsEl.textContent = `(${projects.length} projects, ${running} running)`;
const visible = projects.filter(shouldShow);
const visibleSlugs = new Set(visible.map(p => p.slug));
// Remove cards no longer visible
for (const card of [...grid.children]) {
if (!visibleSlugs.has(card.dataset.slug)) {
card.remove();
cardHashes.delete(card.dataset.slug);
}
}
// Add or update visible cards in order
let prevCard = null;
for (const p of visible) {
const hash = projectHash(p);
let card = grid.querySelector(`[data-slug="${p.slug}"]`);
if (!card) {
// New card
card = document.createElement('div');
card.className = 'card';
card.dataset.slug = p.slug;
card.innerHTML = cardHTML(p);
cardHashes.set(p.slug, hash);
if (prevCard && prevCard.nextSibling) {
grid.insertBefore(card, prevCard.nextSibling);
} else if (prevCard) {
grid.appendChild(card);
} else if (grid.firstChild) {
grid.insertBefore(card, grid.firstChild);
} else {
grid.appendChild(card);
}
} else if (cardHashes.get(p.slug) !== hash) {
// Changed - update innerHTML only for this card
card.innerHTML = cardHTML(p);
cardHashes.set(p.slug, hash);
}
prevCard = card;
}
}
// -- Data fetching --
async function fetchProjects() {
try {
const res = await fetch('/api/projects');
const data = await res.json();
// Probe screenshots for new projects
const checks = data.map(async (p) => {
const old = projects.find(o => o.slug === p.slug);
if (old) {
p._hasScreenshot = old._hasScreenshot;
p._screenshotTs = old._screenshotTs || 0;
} else {
p._hasScreenshot = await imageExists(`/screenshots/${p.slug}.png`);
p._screenshotTs = p._hasScreenshot ? Date.now() : 0;
}
});
await Promise.all(checks);
projects = data;
render();
} catch (err) {
console.error('Failed to fetch projects:', err);
}
}
function imageExists(url) {
return new Promise(resolve => {
const img = new Image();
img.onload = () => resolve(true);
img.onerror = () => resolve(false);
img.src = url;
});
}
// -- Centrifugo real-time updates --
async function connectCentrifugo() {
try {
const res = await fetch('/api/centrifugo/token');
const { token } = await res.json();
const client = new Centrifuge(`ws://${location.hostname}:8787/connection/websocket`, {
token,
});
const sub = client.newSubscription('projects-browser');
sub.on('publication', (ctx) => {
const msg = ctx.data;
if (msg.type === 'status') {
const p = projects.find(p => p.slug === msg.slug);
if (p) {
if (msg.subtype === 'api') {
p.apiStatus = msg.status;
} else if (msg.subtype === 'frontend') {
p.frontendStatus = msg.status;
}
// Recompute combined status
const hasFe = !!p.frontend;
if (!hasFe) {
p.status = p.apiStatus || msg.status;
} else {
const apiSt = p.apiStatus || 'stopped';
const feSt = p.frontendStatus || 'stopped';
if (apiSt === 'running' && feSt === 'running') p.status = 'running';
else if (apiSt === 'error' || feSt === 'error') p.status = 'error';
else if (apiSt === 'starting' || feSt === 'starting') p.status = 'starting';
else if (apiSt === 'stopping' || feSt === 'stopping') p.status = 'stopping';
else p.status = 'stopped';
}
render();
}
}
if (msg.type === 'screenshot') {
const p = projects.find(p => p.slug === msg.slug);
if (p) {
p._hasScreenshot = true;
p._screenshotTs = Date.now();
render();
}
}
});
sub.subscribe();
client.on('connected', () => {
console.log('Centrifugo connected');
});
client.on('disconnected', (ctx) => {
console.log('Centrifugo disconnected:', ctx.reason);
});
client.connect();
} catch (err) {
console.error('Centrifugo setup failed, falling back to polling:', err);
setInterval(fetchProjects, 5000);
}
}
// -- Actions --
window.actions = {
async start(slug) {
await fetch(`/api/projects/${slug}/start`, { method: 'POST' });
// Status updates come via Centrifugo
},
async stop(slug) {
await fetch(`/api/projects/${slug}/stop`, { method: 'POST' });
},
async screenshot(slug) {
const btn = grid.querySelector(`[data-slug="${slug}"] .btn-screenshot-icon`);
if (btn) { btn.disabled = true; btn.textContent = '⏳'; }
try {
await fetch(`/api/projects/${slug}/screenshot`, { method: 'POST' });
// Screenshot update comes via Centrifugo
} catch (err) {
console.error('Screenshot failed:', err);
}
},
async logs(slug) {
const res = await fetch(`/api/projects/${slug}/logs`);
const data = await res.json();
logsTitle.textContent = `Logs: ${slug}`;
// Show both API and frontend logs
let text = '';
if (data.api?.length) {
text += '--- API ---\n' + data.api.join('\n');
}
if (data.frontend?.length) {
if (text) text += '\n\n';
text += '--- Frontend ---\n' + data.frontend.join('\n');
}
logsBody.textContent = text || '(no logs)';
logsModal.classList.add('open');
}
};
// -- Filter buttons --
document.querySelectorAll('.filter-btn').forEach(btn => {
btn.addEventListener('click', () => {
document.querySelectorAll('.filter-btn').forEach(b => b.classList.remove('active'));
btn.classList.add('active');
filter = btn.dataset.filter;
render();
});
});
// Search
searchInput.addEventListener('input', () => {
searchTerm = searchInput.value.toLowerCase().trim();
render();
});
// Screenshot all
screenshotAllBtn.addEventListener('click', async () => {
screenshotAllBtn.disabled = true;
screenshotAllBtn.textContent = 'Taking screenshots...';
try {
await fetch('/api/screenshot-all', { method: 'POST' });
// Updates come via Centrifugo
} finally {
screenshotAllBtn.disabled = false;
screenshotAllBtn.textContent = 'Screenshot All';
}
});
// Logs modal close
logsClose.addEventListener('click', () => logsModal.classList.remove('open'));
logsModal.addEventListener('click', (e) => {
if (e.target === logsModal) logsModal.classList.remove('open');
});
// -- Init --
fetchProjects().then(connectCentrifugo);
})();

BIN
public/favicon-32.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

BIN
public/favicon.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 66 KiB

38
public/index.html Normal file
View File

@@ -0,0 +1,38 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Projects Browser</title>
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32.png">
<link rel="icon" type="image/x-icon" href="/favicon.ico">
<link rel="stylesheet" href="/style.css">
</head>
<body>
<header>
<h1>Projects Browser <span id="stats"></span></h1>
<div class="controls">
<input type="text" class="search-input" id="search" placeholder="Search projects...">
<button class="filter-btn active" data-filter="all">All</button>
<button class="filter-btn" data-filter="running">Running</button>
<button class="filter-btn" data-filter="stopped">Stopped</button>
<button class="action-btn" id="screenshot-all">Screenshot All</button>
</div>
</header>
<div class="grid" id="grid"></div>
<div class="modal-overlay" id="logs-modal">
<div class="modal">
<div class="modal-header">
<span id="logs-title">Logs</span>
<button class="modal-close" id="logs-close">&times;</button>
</div>
<div class="modal-body" id="logs-body"></div>
</div>
</div>
<script src="https://unpkg.com/centrifuge@5/dist/centrifuge.js"></script>
<script src="/app.js"></script>
</body>
</html>

Binary file not shown.

After

Width:  |  Height:  |  Size: 483 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 350 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 621 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 329 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 632 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 998 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 404 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 115 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 93 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 183 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 105 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 45 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 338 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 94 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 42 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 916 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 99 KiB

350
public/style.css Normal file
View File

@@ -0,0 +1,350 @@
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
:root {
--bg: #0f1117;
--surface: #1a1d27;
--surface-hover: #222632;
--border: #2a2e3a;
--text: #e4e6ed;
--text-dim: #8b8fa3;
--accent: #6c8cff;
--green: #34d399;
--amber: #fbbf24;
--red: #f87171;
--radius: 10px;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
background: var(--bg);
color: var(--text);
min-height: 100vh;
}
header {
padding: 24px 32px;
display: flex;
align-items: center;
justify-content: space-between;
border-bottom: 1px solid var(--border);
flex-wrap: wrap;
gap: 16px;
}
header h1 {
font-size: 20px;
font-weight: 600;
letter-spacing: -0.02em;
}
header h1 span {
color: var(--text-dim);
font-weight: 400;
}
.controls {
display: flex;
gap: 10px;
align-items: center;
flex-wrap: wrap;
}
.search-input {
background: var(--surface);
border: 1px solid var(--border);
border-radius: 6px;
padding: 7px 12px;
color: var(--text);
font-size: 14px;
width: 200px;
outline: none;
transition: border-color .2s;
}
.search-input:focus { border-color: var(--accent); }
.filter-btn, .action-btn {
background: var(--surface);
border: 1px solid var(--border);
border-radius: 6px;
padding: 7px 14px;
color: var(--text-dim);
font-size: 13px;
cursor: pointer;
transition: all .15s;
}
.filter-btn:hover, .action-btn:hover {
background: var(--surface-hover);
color: var(--text);
}
.filter-btn.active {
background: var(--accent);
color: #fff;
border-color: var(--accent);
}
.action-btn {
background: var(--accent);
color: #fff;
border-color: var(--accent);
font-weight: 500;
}
.action-btn:hover { opacity: .85; }
.grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
gap: 20px;
padding: 24px 32px;
}
.card {
background: var(--surface);
border: 1px solid var(--border);
border-radius: var(--radius);
overflow: hidden;
transition: transform .15s, box-shadow .15s;
display: flex;
flex-direction: column;
}
.card:hover {
transform: translateY(-2px);
box-shadow: 0 8px 24px rgba(0,0,0,.3);
}
.card-img {
width: 100%;
height: 180px;
object-fit: cover;
object-position: top;
display: block;
background: var(--border);
}
.card-placeholder {
width: 100%;
height: 180px;
display: flex;
align-items: center;
justify-content: center;
font-size: 48px;
font-weight: 700;
color: rgba(255,255,255,.15);
}
/* Screenshot hover icon */
.card-visual {
position: relative;
}
.btn-screenshot-icon {
position: absolute;
top: 8px;
right: 8px;
width: 28px;
height: 28px;
border: none;
border-radius: 6px;
background: rgba(0,0,0,.55);
color: #fff;
font-size: 14px;
cursor: pointer;
opacity: 0;
transition: opacity .15s, background .15s;
display: flex;
align-items: center;
justify-content: center;
padding: 0;
line-height: 1;
}
.card:hover .btn-screenshot-icon { opacity: 1; }
.btn-screenshot-icon:hover { background: rgba(0,0,0,.8); }
.btn-screenshot-icon:disabled { opacity: 0 !important; cursor: not-allowed; }
.card:hover .btn-screenshot-icon:disabled { opacity: .3 !important; }
.card-body {
padding: 16px;
display: flex;
flex-direction: column;
flex: 1;
}
.card-header {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 10px;
}
.status-dots {
display: flex;
gap: 4px;
flex-shrink: 0;
}
.status-dot {
width: 8px;
height: 8px;
border-radius: 50%;
flex-shrink: 0;
}
.status-dot.running { background: var(--green); box-shadow: 0 0 6px var(--green); }
.status-dot.stopped { background: var(--text-dim); }
.status-dot.starting { background: var(--amber); animation: pulse 1s infinite; }
.status-dot.stopping { background: var(--amber); }
.status-dot.error { background: var(--red); }
@keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: .4; } }
.card-name {
font-size: 15px;
font-weight: 600;
flex: 1;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.badges-wrap {
display: flex;
flex-direction: column;
gap: 4px;
margin-bottom: 12px;
}
.badges {
display: flex;
gap: 6px;
flex-wrap: wrap;
}
.badge {
font-size: 11px;
padding: 2px 8px;
border-radius: 4px;
background: rgba(108,140,255,.15);
color: var(--accent);
font-weight: 500;
}
.badge.db { background: rgba(52,211,153,.12); color: var(--green); }
.badge.fe { background: rgba(251,191,36,.12); color: var(--amber); }
.card-path {
font-size: 11px;
color: var(--text-dim);
margin-bottom: 12px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.card-actions {
display: flex;
gap: 8px;
margin-top: auto;
}
.card-actions button, .card-actions a {
flex: 1;
padding: 7px 0;
border: 1px solid var(--border);
border-radius: 6px;
background: transparent;
color: var(--text-dim);
font-size: 12px;
font-weight: 500;
cursor: pointer;
text-align: center;
text-decoration: none;
transition: all .15s;
display: inline-block;
}
.card-actions button:hover, .card-actions a:hover {
background: var(--surface-hover);
color: var(--text);
}
.card-actions .btn-start { color: var(--green); border-color: rgba(52,211,153,.3); }
.card-actions .btn-start:hover { background: rgba(52,211,153,.1); }
.card-actions .btn-stop { color: var(--red); border-color: rgba(248,113,113,.3); }
.card-actions .btn-stop:hover { background: rgba(248,113,113,.1); }
.card-actions .btn-docs { color: var(--accent); border-color: rgba(108,140,255,.3); }
.card-actions .btn-docs:hover { background: rgba(108,140,255,.1); }
.card-actions .btn-open { color: var(--amber); border-color: rgba(251,191,36,.3); }
.card-actions .btn-open:hover { background: rgba(251,191,36,.1); }
.card-actions button:disabled {
opacity: .4;
cursor: not-allowed;
}
.stats {
color: var(--text-dim);
font-size: 13px;
}
/* Logs modal */
.modal-overlay {
display: none;
position: fixed;
inset: 0;
background: rgba(0,0,0,.6);
z-index: 100;
align-items: center;
justify-content: center;
}
.modal-overlay.open { display: flex; }
.modal {
background: var(--surface);
border: 1px solid var(--border);
border-radius: var(--radius);
width: 90%;
max-width: 700px;
max-height: 80vh;
display: flex;
flex-direction: column;
}
.modal-header {
padding: 16px;
border-bottom: 1px solid var(--border);
display: flex;
justify-content: space-between;
align-items: center;
font-weight: 600;
}
.modal-close {
background: none;
border: none;
color: var(--text-dim);
font-size: 20px;
cursor: pointer;
}
.modal-body {
padding: 16px;
overflow-y: auto;
font-family: 'SF Mono', 'Fira Code', monospace;
font-size: 12px;
line-height: 1.6;
color: var(--text-dim);
white-space: pre-wrap;
word-break: break-all;
}
@media (max-width: 640px) {
header { padding: 16px; }
.grid { padding: 16px; gap: 14px; }
.grid { grid-template-columns: 1fr; }
}