File size: 7,168 Bytes
0e0fe0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
let allApps = [];
let appStates = {};

document.addEventListener('DOMContentLoaded', () => {
  setupEventListeners();
  loadApps();
});

function setupEventListeners() {
  const refreshBtn = document.getElementById('refreshBtn');
  const stopAllBtn = document.getElementById('stopAllBtn');

  refreshBtn.addEventListener('click', loadApps);
  stopAllBtn.addEventListener('click', stopAllApps);
}

async function loadApps() {
  showLoadingSpinner(true);
  try {
    const response = await fetch('/api/apps');
    allApps = await response.json();
    
    // Load status for each app
    for (const app of allApps) {
      try {
        const statusResponse = await fetch(`/api/apps/${app.id}/status`);
        const statusData = await statusResponse.json();
        appStates[app.id] = statusData.running;
      } catch (e) {
        appStates[app.id] = false;
      }
    }
    
    renderApps();
  } catch (error) {
    console.error('Error loading apps:', error);
    showError('Failed to load applications');
  } finally {
    showLoadingSpinner(false);
  }
}

function renderApps() {
  const container = document.getElementById('appsContainer');
  const emptyState = document.getElementById('emptyState');
  
  if (allApps.length === 0) {
    container.innerHTML = '';
    emptyState.classList.remove('hidden');
    return;
  }
  
  emptyState.classList.add('hidden');
  container.innerHTML = allApps.map(app => createAppCard(app)).join('');
  
  // Add event listeners to the cards
  allApps.forEach(app => {
    setupAppCardListeners(app);
  });
}

function createAppCard(app) {
  const isRunning = appStates[app.id];
  const statusClass = isRunning ? 'running' : 'stopped';
  const statusText = isRunning ? '● Running' : '● Stopped';
  const launchButtonClass = isRunning ? 'running' : '';
  const launchButtonText = isRunning ? '✓ Running' : '▶ Launch';
  
  return `

    <div class="app-card" data-app-id="${app.id}">

      <div class="app-icon">🚀</div>

      <h3 class="app-name">${escapeHtml(app.displayName)}</h3>

      <p class="app-description">${escapeHtml(app.description)}</p>

      <div class="app-url">${escapeHtml(app.url)}</div>

      <div class="app-status">

        <span class="status-badge ${statusClass}"></span>

        <span>${statusText}</span>

      </div>

      <div class="app-actions">

        <button class="btn btn-small btn-launch ${launchButtonClass}" data-app-id="${app.id}">

          ${launchButtonText}

        </button>

        <button class="btn btn-small btn-open" data-app-id="${app.id}">

          🌐 Open

        </button>

        ${isRunning ? `<button class="btn btn-small btn-stop" data-app-id="${app.id}">⏹ Stop</button>` : ''}

      </div>

    </div>

  `;
}

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);