summaryrefslogtreecommitdiff
path: root/player-server/web/js/views/admin-status.js
blob: 9a1d2f93a7be600a3a84e7fa63175c06ce2d15a3 (plain)
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
import { API } from '../api.js';
import { toast } from '../utils.js';

let scanProgressTimer = null;

export function showAdmin() {
  const btn = document.getElementById('admin-toggle');
  if (btn) btn.classList.remove('hidden');
  startScanProgressPolling();
}

export async function triggerRescan() {
  try {
    await API.rescan();
    await refreshScanProgress();
    toast('Rescan triggered');
  } catch (err) {
    toast(err.message || 'Rescan failed', 'error');
  }
}

export function startScanProgressPolling() {
  if (scanProgressTimer) return;
  refreshScanProgress();
  scanProgressTimer = setInterval(refreshScanProgress, 2000);
}

export async function refreshScanProgress() {
  try {
    renderScanProgress(await API.scanProgress());
  } catch {}
}

export function renderScanProgress(progress) {
  const indicator = document.getElementById('scan-indicator');
  const text = document.getElementById('scan-indicator-text');
  if (!indicator || !text || !progress) return;

  if (!progress.running) {
    indicator.classList.add('hidden');
    return;
  }

  const setPart = progress.current_set ? ` ${progress.current_set}` : '';
  const setsTotal = progress.sets_total || 0;
  const setsPart = setsTotal ? ` ${progress.sets_done || 0}/${setsTotal} sets` : '';
  const filesTotal = progress.files_total || 0;
  const filesDone = progress.files_done || 0;
  const filesPart = filesTotal ? `, ${filesDone}/${filesTotal} files` : (filesDone ? `, ${filesDone} files` : '');
  text.textContent = `Scanning${setPart}${setsPart}${filesPart}`;
  indicator.classList.remove('hidden');
}