Strava sync improvements

- Fix first sync finding 0 activities: remove last_sync_at stamp at
    connect time so the first sync checks all Strava history (existence
    check skips already-extracted files without fetching streams)
  - Add POST /api/strava/reset with soft/hard modes: soft sets last_sync_at
    to the most recent activity already on disk; hard clears it entirely
  - Surface error_count in sync response and status message
  - Add Reset / Hard reset buttons below Sync now in the upload modal
  - Reload on bfcache restore so client:only components re-mount after
    back navigation
This commit is contained in:
Davide Scaini
2026-04-08 14:23:52 +02:00
parent 083c67d018
commit 36a91362d9
2 changed files with 94 additions and 5 deletions
+47 -2
View File
@@ -204,6 +204,18 @@ const baseUrl = import.meta.env.BASE_URL ?? '/';
id="strava-sync-btn"
class="w-full py-2 px-4 rounded-lg font-medium text-sm bg-zinc-700 hover:bg-zinc-600 text-white transition-colors mt-2"
>Sync now</button>
<div class="flex gap-2 mt-1">
<button
id="strava-reset-soft-btn"
class="flex-1 py-1.5 px-3 rounded-lg text-xs bg-zinc-800 hover:bg-zinc-700 text-zinc-400 hover:text-zinc-200 transition-colors"
title="Set sync point to your most recent activity — next sync only fetches newer ones"
>Reset</button>
<button
id="strava-reset-hard-btn"
class="flex-1 py-1.5 px-3 rounded-lg text-xs bg-zinc-800 hover:bg-zinc-700 text-zinc-400 hover:text-zinc-200 transition-colors"
title="Clear sync point — next sync re-checks all Strava activities"
>Hard reset</button>
</div>
</div>
<p id="strava-status" class="mt-3 text-xs text-center" style="min-height: 1.25rem"></p>
</div>
@@ -262,7 +274,9 @@ const baseUrl = import.meta.env.BASE_URL ?? '/';
const stravaConnect = document.getElementById('strava-connect-area');
const stravaSync = document.getElementById('strava-sync-area');
const stravaConnBtn = document.getElementById('strava-connect-btn');
const stravaSyncBtn = document.getElementById('strava-sync-btn');
const stravaSyncBtn = document.getElementById('strava-sync-btn');
const stravaResetSoftBtn = document.getElementById('strava-reset-soft-btn');
const stravaResetHardBtn = document.getElementById('strava-reset-hard-btn');
const stravaLastSync = document.getElementById('strava-last-sync');
const stravaChooseSub = document.getElementById('strava-choose-sub');
@@ -406,7 +420,8 @@ const baseUrl = import.meta.env.BASE_URL ?? '/';
if (!r.ok) throw new Error(await r.text());
const d = await r.json();
stravaLastSync.textContent = new Date().toLocaleString();
stravaStatus.textContent = `Done — ${d.imported} imported, ${d.skipped} already up to date.`;
const errNote = d.error_count ? `, ${d.error_count} errors` : '';
stravaStatus.textContent = `Done — ${d.imported} imported, ${d.skipped} already up to date${errNote}.`;
stravaStatus.style.color = '#4ade80';
if (d.imported > 0) setTimeout(() => window.location.reload(), 1500);
} catch (e) {
@@ -418,6 +433,36 @@ const baseUrl = import.meta.env.BASE_URL ?? '/';
}
});
async function stravaReset(mode) {
const btn = mode === 'soft' ? stravaResetSoftBtn : stravaResetHardBtn;
btn.disabled = true;
stravaStatus.textContent = '';
try {
const r = await fetch(`${editUrl}/api/strava/reset`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ mode }),
});
if (!r.ok) throw new Error(await r.text());
const d = await r.json();
if (mode === 'hard') {
stravaStatus.textContent = 'Hard reset done — next sync will re-check all activities.';
} else {
const date = d.last_sync_at ? new Date(d.last_sync_at * 1000).toLocaleDateString('en-GB', { day: 'numeric', month: 'short', year: 'numeric' }) : 'none';
stravaStatus.textContent = `Reset to ${date} — next sync fetches only newer activities.`;
}
stravaStatus.style.color = '#a1a1aa';
} catch (e) {
stravaStatus.textContent = 'Error: ' + e.message;
stravaStatus.style.color = '#f87171';
} finally {
btn.disabled = false;
}
}
stravaResetSoftBtn.addEventListener('click', () => stravaReset('soft'));
stravaResetHardBtn.addEventListener('click', () => stravaReset('hard'));
// Handle ?strava= param set by the callback redirect (popup scenario)
const sp = new URLSearchParams(window.location.search);
if (sp.has('strava')) {