Add standalone desktop workflow
This commit is contained in:
+149
-106
@@ -1,16 +1,21 @@
|
||||
{{define "content"}}
|
||||
|
||||
<section class="panel">
|
||||
<h2>Disks</h2>
|
||||
<h2>Mounted Disk</h2>
|
||||
<div class="panel-body">
|
||||
<div id="diskSummary" class="text-muted">Loading disks...</div>
|
||||
<div class="path-input-row">
|
||||
<input class="form-input" type="text" id="mountPath" placeholder="/Volumes/JUKEBOX or E:\\">
|
||||
<button type="button" class="button-primary" onclick="pickMountPath()">+</button>
|
||||
<button type="button" class="button-secondary" onclick="refreshSelectedDisk()">Refresh</button>
|
||||
</div>
|
||||
<div class="form-hint">Choose the directory where the removable disk is mounted. The app works with one selected disk at a time in standalone mode.</div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<div class="disk-grid" id="diskGrid"></div>
|
||||
<div id="diskState"></div>
|
||||
|
||||
<script>
|
||||
let disks = [];
|
||||
const selectedDisk = { info: null };
|
||||
const taskState = new Map();
|
||||
const taskPollers = new Map();
|
||||
|
||||
@@ -24,16 +29,12 @@ function escapeHTML(value) {
|
||||
}[char]));
|
||||
}
|
||||
|
||||
function diskKey(disk) {
|
||||
return disk.disk_id || disk.mount_path;
|
||||
}
|
||||
|
||||
function badgeClass(state) {
|
||||
return ({ absent: 'badge-unknown', foreign: 'badge-warn', known: 'badge-ok' })[state] || 'badge-unknown';
|
||||
}
|
||||
|
||||
function badgeLabel(state) {
|
||||
return ({ absent: 'Not connected', foreign: 'Uninitialized disk', known: 'Ready' })[state] || '—';
|
||||
return ({ absent: 'Directory unavailable', foreign: 'Uninitialized disk', known: 'Ready' })[state] || '—';
|
||||
}
|
||||
|
||||
function fmtSpeed(bps) {
|
||||
@@ -69,77 +70,74 @@ function taskMeta(task) {
|
||||
return [fmtSpeed(task.speed_bps), task.eta_sec ? 'ETA: ' + fmtETA(task.eta_sec) : ''].filter(Boolean).join(' · ');
|
||||
}
|
||||
|
||||
function renderDisks() {
|
||||
const grid = document.getElementById('diskGrid');
|
||||
const summary = document.getElementById('diskSummary');
|
||||
|
||||
if (!disks.length) {
|
||||
summary.textContent = 'No disks found.';
|
||||
grid.innerHTML = '';
|
||||
function renderDisk() {
|
||||
const root = document.getElementById('diskState');
|
||||
const disk = selectedDisk.info;
|
||||
if (!disk) {
|
||||
root.innerHTML = `
|
||||
<section class="panel">
|
||||
<div class="panel-body text-muted">Choose a mounted disk directory to inspect it.</div>
|
||||
</section>
|
||||
`;
|
||||
return;
|
||||
}
|
||||
|
||||
const knownCount = disks.filter((disk) => disk.state === 'known').length;
|
||||
summary.textContent = `Disks found: ${disks.length}. Ready to copy: ${knownCount}.`;
|
||||
const activeTask = disk.active_task_id ? taskState.get(disk.active_task_id) : null;
|
||||
const progress = activeTask ? activeTask.progress : 0;
|
||||
const message = activeTask ? (activeTask.message || 'Preparing...') : '';
|
||||
const meta = activeTask ? taskMeta(activeTask) : '';
|
||||
const isKnown = disk.state === 'known';
|
||||
const isForeign = disk.state === 'foreign';
|
||||
const hasCapacity = disk.state !== 'absent';
|
||||
|
||||
grid.innerHTML = disks.map((disk) => {
|
||||
const activeTask = disk.active_task_id ? taskState.get(disk.active_task_id) : null;
|
||||
const progress = activeTask ? activeTask.progress : 0;
|
||||
const message = activeTask ? (activeTask.message || 'Preparing...') : '';
|
||||
const meta = activeTask ? taskMeta(activeTask) : '';
|
||||
const isKnown = disk.state === 'known';
|
||||
const isForeign = disk.state === 'foreign';
|
||||
const hasCapacity = disk.state !== 'absent';
|
||||
|
||||
return `
|
||||
<section class="panel disk-card">
|
||||
<h2>${escapeHTML(disk.mount_path)}</h2>
|
||||
<table class="kv-table">
|
||||
<tbody>
|
||||
<tr>
|
||||
<th>Status</th>
|
||||
<td><span class="badge ${badgeClass(disk.state)}">${badgeLabel(disk.state)}</span></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>Disk ID</th>
|
||||
<td>${disk.disk_id ? `<span class="mono">${escapeHTML(disk.disk_id)}</span>` : '<span class="text-muted">not initialized yet</span>'}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>Total capacity</th>
|
||||
<td>${hasCapacity ? fmtBytes(disk.total_bytes) : '—'}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>Free space</th>
|
||||
<td>${hasCapacity ? fmtBytes(disk.free_bytes) : '—'}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>Last copied</th>
|
||||
<td>${fmtDateTime(disk.last_copied_at)}</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
${activeTask ? `
|
||||
<div class="panel-body progress-wrap">
|
||||
<div class="progress-bar-bg">
|
||||
<div class="progress-bar-fill" style="width:${progress}%"></div>
|
||||
</div>
|
||||
<div class="progress-label">${escapeHTML(message)}</div>
|
||||
<div class="progress-label">${escapeHTML(meta)}</div>
|
||||
root.innerHTML = `
|
||||
<section class="panel disk-card">
|
||||
<h2>${escapeHTML(disk.mount_path)}</h2>
|
||||
<table class="kv-table">
|
||||
<tbody>
|
||||
<tr>
|
||||
<th>Status</th>
|
||||
<td><span class="badge ${badgeClass(disk.state)}">${badgeLabel(disk.state)}</span></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>Disk ID</th>
|
||||
<td>${disk.disk_id ? `<span class="mono">${escapeHTML(disk.disk_id)}</span>` : '<span class="text-muted">not initialized yet</span>'}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>Total capacity</th>
|
||||
<td>${hasCapacity ? fmtBytes(disk.total_bytes) : '—'}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>Free space</th>
|
||||
<td>${hasCapacity ? fmtBytes(disk.free_bytes) : '—'}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>Last copied</th>
|
||||
<td>${fmtDateTime(disk.last_copied_at)}</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
${activeTask ? `
|
||||
<div class="panel-body progress-wrap">
|
||||
<div class="progress-bar-bg">
|
||||
<div class="progress-bar-fill" style="width:${progress}%"></div>
|
||||
</div>
|
||||
` : ''}
|
||||
<div class="btn-row">
|
||||
${isKnown ? `
|
||||
<button class="button-danger" data-action="start-copy" data-mode="replace" data-disk-id="${escapeHTML(disk.disk_id)}" ${activeTask ? 'disabled' : ''}>Replace media</button>
|
||||
<button class="button-primary" data-action="start-copy" data-mode="add" data-disk-id="${escapeHTML(disk.disk_id)}" ${activeTask ? 'disabled' : ''}>Add media</button>
|
||||
<button class="button-danger ${activeTask ? '' : 'hidden'}" data-action="cancel-copy" data-disk-id="${escapeHTML(disk.disk_id)}">Cancel</button>
|
||||
` : ''}
|
||||
${isForeign ? `
|
||||
<button class="button-secondary" data-action="init-disk" data-mount-path="${escapeHTML(disk.mount_path)}">Initialize disk</button>
|
||||
` : ''}
|
||||
<div class="progress-label">${escapeHTML(message)}</div>
|
||||
<div class="progress-label">${escapeHTML(meta)}</div>
|
||||
</div>
|
||||
</section>
|
||||
`;
|
||||
}).join('');
|
||||
` : ''}
|
||||
<div class="btn-row">
|
||||
${isKnown ? `
|
||||
<button class="button-danger" data-action="start-copy" data-mode="replace" ${activeTask ? 'disabled' : ''}>Replace media</button>
|
||||
<button class="button-primary" data-action="start-copy" data-mode="add" ${activeTask ? 'disabled' : ''}>Add media</button>
|
||||
<button class="button-danger ${activeTask ? '' : 'hidden'}" data-action="cancel-copy">Cancel</button>
|
||||
` : ''}
|
||||
${isForeign ? `
|
||||
<button class="button-secondary" data-action="init-disk">Initialize disk</button>
|
||||
` : ''}
|
||||
</div>
|
||||
</section>
|
||||
`;
|
||||
}
|
||||
|
||||
function stopTaskPoll(taskID) {
|
||||
@@ -154,28 +152,63 @@ function startTaskPoll(taskID) {
|
||||
pollTask(taskID);
|
||||
}
|
||||
|
||||
async function refreshDisks() {
|
||||
try {
|
||||
const response = await fetch('/api/disks');
|
||||
if (!response.ok) return;
|
||||
const payload = await response.json();
|
||||
disks = payload.items || [];
|
||||
renderDisks();
|
||||
async function pickFolder() {
|
||||
const response = await fetch('/api/system/pick-folder', { method: 'POST' });
|
||||
const payload = await response.json();
|
||||
if (!response.ok) {
|
||||
throw new Error(payload.error || 'Failed to choose folder');
|
||||
}
|
||||
return payload.path || '';
|
||||
}
|
||||
|
||||
const activeTasks = new Set();
|
||||
for (const disk of disks) {
|
||||
if (disk.active_task_id) {
|
||||
activeTasks.add(disk.active_task_id);
|
||||
startTaskPoll(disk.active_task_id);
|
||||
}
|
||||
async function pickMountPath() {
|
||||
try {
|
||||
const path = await pickFolder();
|
||||
if (!path) return;
|
||||
document.getElementById('mountPath').value = path;
|
||||
localStorage.setItem('jukebox.selectedMountPath', path);
|
||||
await refreshSelectedDisk();
|
||||
} catch (error) {
|
||||
toast(error.message || 'Failed to choose folder', 'error');
|
||||
}
|
||||
}
|
||||
|
||||
async function refreshSelectedDisk() {
|
||||
const mountPath = document.getElementById('mountPath').value.trim();
|
||||
if (!mountPath) {
|
||||
selectedDisk.info = null;
|
||||
renderDisk();
|
||||
return;
|
||||
}
|
||||
|
||||
localStorage.setItem('jukebox.selectedMountPath', mountPath);
|
||||
try {
|
||||
const response = await fetch('/api/disks/probe?mount_path=' + encodeURIComponent(mountPath));
|
||||
const payload = await response.json();
|
||||
if (!response.ok) {
|
||||
toast(payload.error || 'Failed to inspect directory', 'error');
|
||||
return;
|
||||
}
|
||||
for (const taskID of Array.from(taskPollers.keys())) {
|
||||
if (!activeTasks.has(taskID)) {
|
||||
selectedDisk.info = payload;
|
||||
renderDisk();
|
||||
|
||||
if (payload.active_task_id) {
|
||||
for (const taskID of Array.from(taskPollers.keys())) {
|
||||
if (taskID !== payload.active_task_id) {
|
||||
stopTaskPoll(taskID);
|
||||
taskState.delete(taskID);
|
||||
}
|
||||
}
|
||||
startTaskPoll(payload.active_task_id);
|
||||
} else {
|
||||
for (const taskID of Array.from(taskPollers.keys())) {
|
||||
stopTaskPoll(taskID);
|
||||
taskState.delete(taskID);
|
||||
}
|
||||
}
|
||||
} catch (error) {}
|
||||
} catch (error) {
|
||||
toast('Network error', 'error');
|
||||
}
|
||||
}
|
||||
|
||||
async function pollTask(taskID) {
|
||||
@@ -184,7 +217,7 @@ async function pollTask(taskID) {
|
||||
if (!response.ok) return;
|
||||
const task = await response.json();
|
||||
taskState.set(taskID, task);
|
||||
renderDisks();
|
||||
renderDisk();
|
||||
|
||||
if (['success', 'failed', 'canceled'].includes(task.status)) {
|
||||
stopTaskPoll(taskID);
|
||||
@@ -192,17 +225,19 @@ async function pollTask(taskID) {
|
||||
if (task.status === 'success') toast(task.message || 'Done', 'ok');
|
||||
if (task.status === 'failed') toast('Error: ' + task.error, 'error');
|
||||
if (task.status === 'canceled') toast('Copy canceled', 'error');
|
||||
refreshDisks();
|
||||
refreshSelectedDisk();
|
||||
}
|
||||
} catch (error) {}
|
||||
}
|
||||
|
||||
async function startCopy(diskID, mode) {
|
||||
async function startCopy(mode) {
|
||||
const mountPath = document.getElementById('mountPath').value.trim();
|
||||
if (!mountPath) return;
|
||||
try {
|
||||
const response = await fetch('/api/disks/' + encodeURIComponent(diskID) + '/copy/start', {
|
||||
const response = await fetch('/api/disks/copy/start', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ mode })
|
||||
body: JSON.stringify({ mount_path: mountPath, mode })
|
||||
});
|
||||
const payload = await response.json();
|
||||
if (!response.ok) {
|
||||
@@ -210,22 +245,25 @@ async function startCopy(diskID, mode) {
|
||||
return;
|
||||
}
|
||||
startTaskPoll(payload.task_id);
|
||||
refreshDisks();
|
||||
refreshSelectedDisk();
|
||||
} catch (error) {
|
||||
toast('Network error', 'error');
|
||||
}
|
||||
}
|
||||
|
||||
async function cancelCopy(diskID) {
|
||||
async function cancelCopy() {
|
||||
if (!selectedDisk.info || !selectedDisk.info.disk_id) return;
|
||||
try {
|
||||
await fetch('/api/disks/' + encodeURIComponent(diskID) + '/copy/cancel', { method: 'POST' });
|
||||
await fetch('/api/disks/' + encodeURIComponent(selectedDisk.info.disk_id) + '/copy/cancel', { method: 'POST' });
|
||||
toast('Canceling...', 'ok');
|
||||
} catch (error) {
|
||||
toast('Network error', 'error');
|
||||
}
|
||||
}
|
||||
|
||||
async function initDisk(mountPath) {
|
||||
async function initDisk() {
|
||||
const mountPath = document.getElementById('mountPath').value.trim();
|
||||
if (!mountPath) return;
|
||||
try {
|
||||
const response = await fetch('/api/disks/init', {
|
||||
method: 'POST',
|
||||
@@ -238,23 +276,28 @@ async function initDisk(mountPath) {
|
||||
return;
|
||||
}
|
||||
toast('Disk initialized', 'ok');
|
||||
refreshDisks();
|
||||
refreshSelectedDisk();
|
||||
} catch (error) {
|
||||
toast('Network error', 'error');
|
||||
}
|
||||
}
|
||||
|
||||
document.getElementById('diskGrid').addEventListener('click', (event) => {
|
||||
document.getElementById('diskState').addEventListener('click', (event) => {
|
||||
const button = event.target.closest('button[data-action]');
|
||||
if (!button) return;
|
||||
|
||||
const action = button.dataset.action;
|
||||
if (action === 'start-copy') startCopy(button.dataset.diskId, button.dataset.mode || 'add');
|
||||
if (action === 'cancel-copy') cancelCopy(button.dataset.diskId);
|
||||
if (action === 'init-disk') initDisk(button.dataset.mountPath);
|
||||
if (action === 'start-copy') startCopy(button.dataset.mode || 'add');
|
||||
if (action === 'cancel-copy') cancelCopy();
|
||||
if (action === 'init-disk') initDisk();
|
||||
});
|
||||
|
||||
refreshDisks();
|
||||
setInterval(refreshDisks, 5000);
|
||||
const savedMountPath = localStorage.getItem('jukebox.selectedMountPath');
|
||||
if (savedMountPath) {
|
||||
document.getElementById('mountPath').value = savedMountPath;
|
||||
refreshSelectedDisk();
|
||||
} else {
|
||||
renderDisk();
|
||||
}
|
||||
</script>
|
||||
{{end}}
|
||||
|
||||
Reference in New Issue
Block a user