Add playlist auto-sync schedule board

Add a Sync-page Auto-Sync manager with source-grouped mirrored playlists, interval columns, and drag/drop scheduling backed by playlist_pipeline automations.

Schedules created by the board are editable there, while existing custom pipeline automations are shown as locked automation-managed entries.
This commit is contained in:
Broque Thomas 2026-05-24 20:31:03 -07:00
parent 46a0999ca2
commit 854141f903
3 changed files with 586 additions and 0 deletions

View file

@ -916,6 +916,7 @@
server</p>
</div>
<div style="display:flex;gap:8px;align-items:center;">
<button class="sync-history-btn auto-sync-manager-btn" onclick="openAutoSyncScheduleModal()" title="Schedule mirrored playlists to refresh, discover, sync, and queue missing tracks">Auto-Sync</button>
<button class="sync-history-btn" onclick="openManualLibraryMatchTool()" title="Manually link source tracks to library tracks">Library Match</button>
<button class="sync-history-btn" onclick="openSyncHistoryModal()" title="View sync history">Sync History</button>
</div>

View file

@ -381,6 +381,13 @@ function importFileSubmit() {
let mirroredPlaylistsLoaded = false;
const mirroredPipelinePollers = {};
const AUTO_SYNC_BUCKETS = [1, 2, 4, 8, 12, 16, 24, 48, 72, 168];
let _autoSyncScheduleState = {
playlists: [],
automations: [],
playlistSchedules: {},
automationManaged: [],
};
/**
* Fire-and-forget helper: send parsed playlist data to be mirrored on the backend.
@ -570,6 +577,312 @@ function getMirroredSourceRef(p) {
return (p && p.source_playlist_id) ? String(p.source_playlist_id) : '';
}
function autoSyncTriggerForHours(hours) {
const h = parseInt(hours, 10) || 24;
if (h >= 24 && h % 24 === 0) {
return { interval: h / 24, unit: 'days' };
}
return { interval: h, unit: 'hours' };
}
function autoSyncHoursFromTrigger(config) {
const interval = parseInt(config?.interval, 10) || 0;
const unit = config?.unit || 'hours';
if (!interval) return null;
if (unit === 'minutes') return Math.max(1, Math.round(interval / 60));
if (unit === 'days') return interval * 24;
if (unit === 'weeks') return interval * 168;
return interval;
}
function autoSyncBucketLabel(hours) {
if (hours === 168) return 'Weekly';
if (hours >= 24) return `${hours / 24}d`;
return `${hours}h`;
}
function autoSyncSourceLabel(source) {
const labels = {
spotify: 'Spotify',
spotify_public: 'Spotify Link',
tidal: 'Tidal',
youtube: 'YouTube',
deezer: 'Deezer',
qobuz: 'Qobuz',
beatport: 'Beatport',
file: 'File Imports',
};
return labels[source] || source || 'Other';
}
function autoSyncIsPipelineAutomation(auto) {
return auto && auto.action_type === 'playlist_pipeline';
}
function autoSyncPlaylistIdFromAutomation(auto) {
if (!autoSyncIsPipelineAutomation(auto)) return null;
const cfg = auto.action_config || {};
if (cfg.all === true || cfg.all === 'true') return null;
const raw = cfg.playlist_id;
if (raw === undefined || raw === null || raw === '') return null;
const id = parseInt(raw, 10);
return Number.isFinite(id) ? id : null;
}
function autoSyncIsScheduleOwned(auto) {
const group = auto?.group_name || '';
const name = auto?.name || '';
return group === 'Playlist Auto-Sync' || name.startsWith('Auto-Sync:');
}
function buildAutoSyncScheduleState(playlists, automations) {
const playlistSchedules = {};
const automationManaged = [];
automations.filter(autoSyncIsPipelineAutomation).forEach(auto => {
const playlistId = autoSyncPlaylistIdFromAutomation(auto);
const hours = auto.trigger_type === 'schedule' ? autoSyncHoursFromTrigger(auto.trigger_config || {}) : null;
if (playlistId && hours) {
playlistSchedules[playlistId] = {
automation_id: auto.id,
automation_name: auto.name,
hours,
enabled: auto.enabled !== false && auto.enabled !== 0,
owned: autoSyncIsScheduleOwned(auto),
next_run: auto.next_run,
trigger_config: auto.trigger_config || {},
};
} else {
automationManaged.push(auto);
}
});
return { playlists, automations, playlistSchedules, automationManaged };
}
async function openAutoSyncScheduleModal() {
let overlay = document.getElementById('auto-sync-schedule-modal');
if (!overlay) {
overlay = document.createElement('div');
overlay.id = 'auto-sync-schedule-modal';
overlay.className = 'auto-sync-overlay';
document.body.appendChild(overlay);
}
overlay.innerHTML = `
<div class="auto-sync-modal">
<div class="auto-sync-header">
<div>
<h3>Auto-Sync Schedule</h3>
<p>Drop mirrored playlists onto an interval to schedule refresh, discovery, sync, and wishlist processing.</p>
</div>
<button class="auto-sync-close" onclick="closeAutoSyncScheduleModal()">&times;</button>
</div>
<div class="auto-sync-loading">Loading schedule...</div>
</div>
`;
overlay.style.display = 'flex';
overlay.addEventListener('click', e => { if (e.target === overlay) closeAutoSyncScheduleModal(); }, { once: true });
await refreshAutoSyncScheduleModal();
}
function closeAutoSyncScheduleModal() {
const overlay = document.getElementById('auto-sync-schedule-modal');
if (overlay) overlay.remove();
}
async function refreshAutoSyncScheduleModal() {
const overlay = document.getElementById('auto-sync-schedule-modal');
if (!overlay) return;
try {
const [playlistRes, automationRes] = await Promise.all([
fetch('/api/mirrored-playlists'),
fetch('/api/automations'),
]);
const playlists = await playlistRes.json();
const automations = await automationRes.json();
if (!playlistRes.ok || playlists.error) throw new Error(playlists.error || 'Failed to load mirrored playlists');
if (!automationRes.ok || automations.error) throw new Error(automations.error || 'Failed to load automations');
_autoSyncScheduleState = buildAutoSyncScheduleState(playlists, automations);
renderAutoSyncScheduleModal();
} catch (err) {
overlay.innerHTML = `
<div class="auto-sync-modal">
<div class="auto-sync-header">
<div><h3>Auto-Sync Schedule</h3><p>Could not load schedule data.</p></div>
<button class="auto-sync-close" onclick="closeAutoSyncScheduleModal()">&times;</button>
</div>
<div class="auto-sync-error">${_esc(err.message)}</div>
</div>
`;
}
}
function renderAutoSyncScheduleModal() {
const overlay = document.getElementById('auto-sync-schedule-modal');
if (!overlay) return;
const { playlists, playlistSchedules, automationManaged } = _autoSyncScheduleState;
const grouped = playlists.reduce((acc, p) => {
const key = p.source || 'other';
if (!acc[key]) acc[key] = [];
acc[key].push(p);
return acc;
}, {});
const sourceKeys = Object.keys(grouped).sort((a, b) => autoSyncSourceLabel(a).localeCompare(autoSyncSourceLabel(b)));
const sidebarHtml = sourceKeys.length ? sourceKeys.map(source => `
<div class="auto-sync-source-group">
<div class="auto-sync-source-title">${_esc(autoSyncSourceLabel(source))}</div>
${grouped[source].map(p => {
const schedule = playlistSchedules[p.id];
const assigned = schedule ? autoSyncBucketLabel(schedule.hours) : 'Unscheduled';
return `
<div class="auto-sync-playlist ${schedule ? 'scheduled' : ''}" draggable="true" data-playlist-id="${p.id}" ondragstart="autoSyncDragStart(event)">
<div class="auto-sync-playlist-name">${_esc(p.name)}</div>
<div class="auto-sync-playlist-meta">${p.track_count || 0} tracks · ${_esc(assigned)}</div>
</div>
`;
}).join('')}
</div>
`).join('') : '<div class="auto-sync-empty">No mirrored playlists yet.</div>';
const bucketHtml = AUTO_SYNC_BUCKETS.map(hours => {
const assigned = playlists.filter(p => playlistSchedules[p.id]?.hours === hours);
return `
<div class="auto-sync-column" data-hours="${hours}" ondragover="autoSyncDragOver(event)" ondrop="autoSyncDrop(event, ${hours})">
<div class="auto-sync-column-head">
<span>${autoSyncBucketLabel(hours)}</span>
<small>${assigned.length}</small>
</div>
<div class="auto-sync-column-list">
${assigned.length ? assigned.map(p => autoSyncScheduledCardHtml(p, playlistSchedules[p.id])).join('') : '<div class="auto-sync-drop-hint">Drop playlists here</div>'}
</div>
</div>
`;
}).join('');
const managedHtml = automationManaged.length ? `
<div class="auto-sync-managed">
<div class="auto-sync-managed-title">Automation-managed pipelines</div>
${automationManaged.map(a => `<span title="${_escAttr(_autoFormatTrigger(a.trigger_type, a.trigger_config || {}))}">${_esc(a.name || 'Playlist Pipeline')}</span>`).join('')}
</div>
` : '';
overlay.innerHTML = `
<div class="auto-sync-modal">
<div class="auto-sync-header">
<div>
<h3>Auto-Sync Schedule</h3>
<p>Drag mirrored playlists into an interval. Each placement creates or updates a matching playlist-pipeline automation.</p>
</div>
<button class="auto-sync-close" onclick="closeAutoSyncScheduleModal()">&times;</button>
</div>
${managedHtml}
<div class="auto-sync-body">
<aside class="auto-sync-sidebar">
<div class="auto-sync-sidebar-title">Mirrored playlists</div>
<div class="auto-sync-source-list">${sidebarHtml}</div>
</aside>
<main class="auto-sync-board">${bucketHtml}</main>
</div>
</div>
`;
}
function autoSyncScheduledCardHtml(playlist, schedule) {
const enabled = schedule?.enabled !== false;
const owned = schedule?.owned === true;
return `
<div class="auto-sync-scheduled-card ${enabled ? '' : 'disabled'}" draggable="true" data-playlist-id="${playlist.id}" ondragstart="autoSyncDragStart(event)">
<div>
<div class="auto-sync-scheduled-name">${_esc(playlist.name)}</div>
<div class="auto-sync-scheduled-meta">${_esc(autoSyncSourceLabel(playlist.source))} · ${playlist.track_count || 0} tracks${schedule?.next_run ? ` · ${autoSyncNextRunLabel(schedule.next_run)}` : ''}${owned ? '' : ' · Automations page'}</div>
</div>
${owned
? `<button onclick="event.stopPropagation(); unscheduleAutoSyncPlaylist(${playlist.id})" title="Remove this Auto-Sync schedule">&times;</button>`
: '<span class="auto-sync-lock" title="Managed from Automations page">Lock</span>'}
</div>
`;
}
function autoSyncNextRunLabel(nextRun) {
if (!nextRun) return '';
const ts = new Date(nextRun).getTime();
if (!Number.isFinite(ts)) return '';
const diff = ts - Date.now();
if (diff <= 0) return 'due now';
const mins = Math.ceil(diff / 60000);
if (mins < 60) return `next in ${mins}m`;
const hours = Math.ceil(mins / 60);
if (hours < 24) return `next in ${hours}h`;
return `next in ${Math.ceil(hours / 24)}d`;
}
function autoSyncDragStart(event) {
const playlistId = event.currentTarget?.dataset?.playlistId;
if (!playlistId) return;
event.dataTransfer.setData('text/plain', playlistId);
event.dataTransfer.effectAllowed = 'move';
}
function autoSyncDragOver(event) {
event.preventDefault();
event.dataTransfer.dropEffect = 'move';
}
async function autoSyncDrop(event, hours) {
event.preventDefault();
const playlistId = parseInt(event.dataTransfer.getData('text/plain'), 10);
if (!playlistId) return;
await saveAutoSyncPlaylistSchedule(playlistId, hours);
}
async function saveAutoSyncPlaylistSchedule(playlistId, hours) {
const playlist = _autoSyncScheduleState.playlists.find(p => parseInt(p.id, 10) === parseInt(playlistId, 10));
if (!playlist) return;
const existing = _autoSyncScheduleState.playlistSchedules[playlistId];
if (existing && !existing.owned) {
showToast('This playlist pipeline is managed from the Automations page for now.', 'info');
return;
}
const payload = {
name: `Auto-Sync: ${playlist.name}`,
trigger_type: 'schedule',
trigger_config: autoSyncTriggerForHours(hours),
action_type: 'playlist_pipeline',
action_config: { playlist_id: String(playlistId), all: false },
then_actions: [],
group_name: 'Playlist Auto-Sync',
};
try {
const res = await fetch(existing ? `/api/automations/${existing.automation_id}` : '/api/automations', {
method: existing ? 'PUT' : 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload),
});
const data = await res.json();
if (!res.ok || data.error) throw new Error(data.error || 'Failed to save Auto-Sync schedule');
showToast(`${playlist.name} scheduled every ${autoSyncBucketLabel(hours)}`, 'success');
await refreshAutoSyncScheduleModal();
} catch (err) {
showToast(`Error: ${err.message}`, 'error');
}
}
async function unscheduleAutoSyncPlaylist(playlistId) {
const schedule = _autoSyncScheduleState.playlistSchedules[playlistId];
const playlist = _autoSyncScheduleState.playlists.find(p => parseInt(p.id, 10) === parseInt(playlistId, 10));
if (!schedule) return;
if (!await showConfirmDialog({ title: 'Remove Auto-Sync', message: `Remove Auto-Sync schedule for "${playlist?.name || 'this playlist'}"?` })) return;
try {
const res = await fetch(`/api/automations/${schedule.automation_id}`, { method: 'DELETE' });
const data = await res.json();
if (!res.ok || data.error) throw new Error(data.error || 'Failed to remove Auto-Sync schedule');
showToast('Auto-Sync schedule removed', 'success');
await refreshAutoSyncScheduleModal();
} catch (err) {
showToast(`Error: ${err.message}`, 'error');
}
}
async function parseMirroredPipelineResponse(res, fallbackMessage) {
const text = await res.text();
let data = {};

View file

@ -11178,6 +11178,278 @@ body.helper-mode-active #dashboard-activity-feed:hover {
}
.sync-history-btn:hover { color: rgb(var(--accent-rgb)); background: rgba(var(--accent-rgb), 0.1); border-color: rgba(var(--accent-rgb), 0.3); }
.auto-sync-manager-btn {
color: #7dd3fc;
border-color: rgba(56, 189, 248, 0.28);
background: rgba(56, 189, 248, 0.1);
}
.auto-sync-overlay {
position: fixed;
inset: 0;
z-index: 10000;
display: none;
align-items: center;
justify-content: center;
background: rgba(0, 0, 0, 0.72);
backdrop-filter: blur(12px);
}
.auto-sync-modal {
width: min(1420px, calc(100vw - 48px));
height: min(820px, calc(100vh - 48px));
background: rgba(18, 20, 28, 0.98);
border: 1px solid rgba(255, 255, 255, 0.12);
border-radius: 8px;
box-shadow: 0 28px 80px rgba(0, 0, 0, 0.5);
display: flex;
flex-direction: column;
overflow: hidden;
}
.auto-sync-header {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 20px;
padding: 22px 24px 18px;
border-bottom: 1px solid rgba(255, 255, 255, 0.08);
}
.auto-sync-header h3 {
margin: 0 0 6px;
color: rgba(255, 255, 255, 0.92);
font-size: 21px;
font-weight: 700;
}
.auto-sync-header p {
margin: 0;
color: rgba(255, 255, 255, 0.48);
font-size: 13px;
}
.auto-sync-close {
width: 32px;
height: 32px;
border: 0;
border-radius: 6px;
background: rgba(255, 255, 255, 0.06);
color: rgba(255, 255, 255, 0.65);
cursor: pointer;
font-size: 22px;
line-height: 1;
}
.auto-sync-close:hover {
background: rgba(255, 255, 255, 0.12);
color: #fff;
}
.auto-sync-body {
min-height: 0;
flex: 1;
display: grid;
grid-template-columns: 300px 1fr;
}
.auto-sync-sidebar {
min-height: 0;
border-right: 1px solid rgba(255, 255, 255, 0.08);
background: rgba(255, 255, 255, 0.025);
display: flex;
flex-direction: column;
}
.auto-sync-sidebar-title {
padding: 16px 18px 12px;
color: rgba(255, 255, 255, 0.72);
font-size: 12px;
font-weight: 700;
text-transform: uppercase;
}
.auto-sync-source-list {
min-height: 0;
overflow-y: auto;
padding: 0 12px 16px;
}
.auto-sync-source-group {
margin-bottom: 16px;
}
.auto-sync-source-title {
padding: 8px 6px;
color: rgba(255, 255, 255, 0.42);
font-size: 12px;
font-weight: 700;
}
.auto-sync-playlist,
.auto-sync-scheduled-card {
border: 1px solid rgba(255, 255, 255, 0.08);
border-radius: 7px;
background: rgba(255, 255, 255, 0.045);
cursor: grab;
}
.auto-sync-playlist {
padding: 10px;
margin-bottom: 8px;
}
.auto-sync-playlist:hover,
.auto-sync-scheduled-card:hover {
border-color: rgba(56, 189, 248, 0.32);
background: rgba(56, 189, 248, 0.08);
}
.auto-sync-playlist.scheduled {
border-color: rgba(34, 197, 94, 0.22);
}
.auto-sync-playlist-name,
.auto-sync-scheduled-name {
color: rgba(255, 255, 255, 0.86);
font-size: 13px;
font-weight: 700;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.auto-sync-playlist-meta,
.auto-sync-scheduled-meta {
margin-top: 4px;
color: rgba(255, 255, 255, 0.42);
font-size: 11px;
}
.auto-sync-board {
min-width: 0;
overflow-x: auto;
padding: 18px;
display: grid;
grid-template-columns: repeat(10, minmax(170px, 1fr));
gap: 12px;
}
.auto-sync-column {
min-height: 0;
border: 1px solid rgba(255, 255, 255, 0.08);
border-radius: 8px;
background: rgba(255, 255, 255, 0.025);
display: flex;
flex-direction: column;
}
.auto-sync-column-head {
display: flex;
align-items: center;
justify-content: space-between;
padding: 12px;
border-bottom: 1px solid rgba(255, 255, 255, 0.07);
color: rgba(255, 255, 255, 0.86);
font-weight: 800;
}
.auto-sync-column-head small {
color: #7dd3fc;
font-size: 11px;
font-weight: 700;
}
.auto-sync-column-list {
flex: 1;
min-height: 220px;
padding: 10px;
}
.auto-sync-drop-hint,
.auto-sync-empty,
.auto-sync-loading,
.auto-sync-error {
color: rgba(255, 255, 255, 0.38);
font-size: 13px;
text-align: center;
}
.auto-sync-drop-hint {
border: 1px dashed rgba(255, 255, 255, 0.12);
border-radius: 7px;
padding: 18px 10px;
}
.auto-sync-loading,
.auto-sync-error {
padding: 48px;
}
.auto-sync-scheduled-card {
padding: 10px;
margin-bottom: 10px;
display: flex;
justify-content: space-between;
gap: 8px;
}
.auto-sync-scheduled-card.disabled {
opacity: 0.52;
}
.auto-sync-scheduled-card button {
width: 24px;
height: 24px;
border: 0;
border-radius: 5px;
background: rgba(255, 255, 255, 0.06);
color: rgba(255, 255, 255, 0.5);
cursor: pointer;
flex-shrink: 0;
}
.auto-sync-scheduled-card button:hover {
color: #ef4444;
background: rgba(239, 68, 68, 0.12);
}
.auto-sync-lock {
align-self: flex-start;
padding: 4px 6px;
border-radius: 5px;
background: rgba(255, 255, 255, 0.06);
color: rgba(255, 255, 255, 0.38);
font-size: 10px;
font-weight: 700;
text-transform: uppercase;
flex-shrink: 0;
}
.auto-sync-managed {
display: flex;
align-items: center;
gap: 8px;
padding: 10px 24px;
border-bottom: 1px solid rgba(255, 255, 255, 0.07);
color: rgba(255, 255, 255, 0.48);
font-size: 12px;
overflow-x: auto;
}
.auto-sync-managed-title {
color: rgba(255, 255, 255, 0.72);
font-weight: 700;
flex-shrink: 0;
}
.auto-sync-managed span {
padding: 4px 8px;
border-radius: 999px;
background: rgba(255, 255, 255, 0.06);
white-space: nowrap;
}
/* Enhanced Progress Bar Animation */
.progress-bar-fill {
height: 100%;