`;
}
function setupAppCardListeners(app) {
const card = document.querySelector(`[data-app-id="${app.id}"]`);
const launchBtn = card.querySelector('.btn-launch');
const openBtn = card.querySelector('.btn-open');
const stopBtn = card.querySelector('.btn-stop');
launchBtn.addEventListener('click', () => launchApp(app.id));
openBtn.addEventListener('click', () => openApp(app));
if (stopBtn) {
stopBtn.addEventListener('click', () => stopApp(app.id));
}
}
async function launchApp(appId) {
const app = allApps.find(a => a.id === appId);
if (!app) return;
const launchBtn = document.querySelector(`[data-app-id="${appId}"] .btn-launch`);
launchBtn.disabled = true;
launchBtn.textContent = '⏳ Launching...';
try {
const response = await fetch(`/api/apps/${appId}/launch`, { method: 'POST' });
const data = await response.json();
appStates[appId] = true;
// Wait a moment for the app to start
await sleep(2000);
// Reload status
await loadApps();
// Show success message
showSuccess(`${app.displayName} launched! Opening in browser...`);
// Open the app in default browser
setTimeout(() => openApp(app), 1000);
} catch (error) {
console.error('Error launching app:', error);
showError(`Failed to launch ${app.displayName}`);
launchBtn.disabled = false;
launchBtn.textContent = '▶ Launch';
}
}
function openApp(app) {
// For Electron, we can use a special protocol or just open via shell
console.log('Opening app:', app.url);
// Try to open using fetch (which will be handled by the main process in a real Electron app)
// For now, we'll show the URL and ask the user to open it
// Create a simple approach - open the URL using window.open for local testing
// In production Electron app, you would use electron's shell.openExternal
const link = document.createElement('a');
link.href = app.url;
link.target = '_blank';
link.click();
}
async function stopApp(appId) {
const app = allApps.find(a => a.id === appId);
if (!app) return;
try {
const response = await fetch(`/api/apps/${appId}/stop`, { method: 'POST' });
const data = await response.json();
appStates[appId] = false;
renderApps();
showSuccess(`${app.displayName} stopped`);
} catch (error) {
console.error('Error stopping app:', error);
showError(`Failed to stop ${app.displayName}`);
}
}
async function stopAllApps() {
const runningApps = allApps.filter(app => appStates[app.id]);
if (runningApps.length === 0) {
showInfo('No running applications to stop');
return;
}
for (const app of runningApps) {
try {
await fetch(`/api/apps/${app.id}/stop`, { method: 'POST' });
appStates[app.id] = false;
} catch (e) {
console.error(`Error stopping ${app.id}:`, e);
}
}
renderApps();
showSuccess('All running applications stopped');
}
function showLoadingSpinner(show) {
const spinner = document.getElementById('loadingSpinner');
if (show) {
spinner.classList.remove('hidden');
} else {
spinner.classList.add('hidden');
}
}
function showSuccess(message) {
console.log('✓', message);
// In a real app, you might use a toast notification library
alert(message);
}
function showError(message) {
console.error('✗', message);
alert('Error: ' + message);
}
function showInfo(message) {
console.info('ℹ', message);
alert(message);
}
function escapeHtml(text) {
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
// Auto-refresh app status every 5 seconds
setInterval(async () => {
for (const app of allApps) {
try {
const response = await fetch(`/api/apps/${app.id}/status`);
const data = await response.json();
const wasRunning = appStates[app.id];
const isNowRunning = data.running;
if (wasRunning !== isNowRunning) {
appStates[app.id] = isNowRunning;
renderApps();
}
} catch (e) {
// Silent fail on status check
}
}
}, 5000);