Vanderson da Silva Quintino commited on
Commit
468499f
·
1 Parent(s): 161bcba

feat: background generation

Browse files
Files changed (1) hide show
  1. src/ClipForge.Api/wwwroot/index.html +32 -29
src/ClipForge.Api/wwwroot/index.html CHANGED
@@ -216,52 +216,54 @@
216
 
217
  btn.disabled = true;
218
  status.className = 'status loading';
219
-
220
- const steps = [
221
- '🧠 Gerando ideia viral...',
222
- '📝 Escrevendo roteiro...',
223
- '🎙️ Gerando narração...',
224
- '🎬 Baixando clips do YouTube...',
225
- '🎞️ Montando vídeo...',
226
- '💬 Adicionando legendas...',
227
- '✨ Finalizando...'
228
- ];
229
-
230
- let stepIndex = 0;
231
- status.innerHTML = `<span class="spinner"></span> ${steps[0]}`;
232
-
233
- const stepInterval = setInterval(() => {
234
- stepIndex++;
235
- if (stepIndex < steps.length) {
236
- status.innerHTML = `<span class="spinner"></span> ${steps[stepIndex]}`;
237
- }
238
- }, 30000); // Avança etapa a cada 30s
239
 
240
  try {
241
- const res = await fetch(`${API}/api/pipeline/run-sync`, {
 
242
  method: 'POST',
243
  headers: { 'Content-Type': 'application/json' },
244
- body: JSON.stringify({ niche, platform, tone: 'engaging', language: 'pt-BR', voiceId: voice })
245
  });
246
 
247
- clearInterval(stepInterval);
248
-
249
  if (res.ok) {
250
- status.className = 'status success';
251
- status.textContent = '✅ Vídeo gerado com sucesso!';
252
- loadVideos();
 
253
  } else {
254
  const err = await res.text();
255
  status.className = 'status error';
256
  status.textContent = '❌ Erro: ' + err.substring(0, 150);
 
257
  }
258
  } catch (e) {
259
- clearInterval(stepInterval);
260
  status.className = 'status error';
261
  status.textContent = '❌ Erro de conexão: ' + e.message;
 
262
  }
 
 
 
 
263
 
264
- btn.disabled = false;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
265
  }
266
 
267
  async function loadVideos() {
@@ -269,6 +271,7 @@
269
  const res = await fetch(`${API}/api/videos`);
270
  const videos = await res.json();
271
  const list = document.getElementById('videosList');
 
272
 
273
  if (videos.length === 0) {
274
  list.innerHTML = '<div class="empty">Nenhum vídeo gerado ainda</div>';
 
216
 
217
  btn.disabled = true;
218
  status.className = 'status loading';
219
+ status.innerHTML = '<span class="spinner"></span> 🚀 Enviando para geração...';
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
220
 
221
  try {
222
+ // Dispara em background - não espera terminar
223
+ const res = await fetch(`${API}/api/pipeline/run`, {
224
  method: 'POST',
225
  headers: { 'Content-Type': 'application/json' },
226
+ body: JSON.stringify({ niche, platform })
227
  });
228
 
 
 
229
  if (res.ok) {
230
+ status.className = 'status loading';
231
+ status.innerHTML = '<span class="spinner"></span> ⏳ Vídeo sendo gerado em segundo plano... pode fechar o app e voltar depois!';
232
+ // Fica checando a cada 15s se apareceu vídeo novo
233
+ startPolling();
234
  } else {
235
  const err = await res.text();
236
  status.className = 'status error';
237
  status.textContent = '❌ Erro: ' + err.substring(0, 150);
238
+ btn.disabled = false;
239
  }
240
  } catch (e) {
 
241
  status.className = 'status error';
242
  status.textContent = '❌ Erro de conexão: ' + e.message;
243
+ btn.disabled = false;
244
  }
245
+ }
246
+
247
+ let pollingInterval = null;
248
+ let lastVideoCount = 0;
249
 
250
+ function startPolling() {
251
+ if (pollingInterval) clearInterval(pollingInterval);
252
+ pollingInterval = setInterval(async () => {
253
+ try {
254
+ const res = await fetch(`${API}/api/videos`);
255
+ const videos = await res.json();
256
+ if (videos.length > lastVideoCount) {
257
+ lastVideoCount = videos.length;
258
+ clearInterval(pollingInterval);
259
+ pollingInterval = null;
260
+ document.getElementById('status').className = 'status success';
261
+ document.getElementById('status').textContent = '✅ Vídeo gerado com sucesso!';
262
+ document.getElementById('generateBtn').disabled = false;
263
+ loadVideos();
264
+ }
265
+ } catch (e) { /* ignore */ }
266
+ }, 15000);
267
  }
268
 
269
  async function loadVideos() {
 
271
  const res = await fetch(`${API}/api/videos`);
272
  const videos = await res.json();
273
  const list = document.getElementById('videosList');
274
+ lastVideoCount = videos.length;
275
 
276
  if (videos.length === 0) {
277
  list.innerHTML = '<div class="empty">Nenhum vídeo gerado ainda</div>';