diff --git a/src/lib/admin/autoSave.svelte.ts b/src/lib/admin/autoSave.svelte.ts index d4b50e6..7b5fe1e 100644 --- a/src/lib/admin/autoSave.svelte.ts +++ b/src/lib/admin/autoSave.svelte.ts @@ -70,6 +70,9 @@ export function createAutoSaveStore( function schedule() { if (timer) clearTimeout(timer) + if (typeof window !== 'undefined' && window.location.hostname === 'localhost') { + console.debug(`[AutoSave] Scheduled (${debounceMs}ms debounce)`) + } timer = setTimeout(() => void run(), debounceMs) } @@ -80,24 +83,44 @@ export function createAutoSaveStore( } const payload = opts.getPayload() - if (!payload) return + if (!payload) { + if (typeof window !== 'undefined' && window.location.hostname === 'localhost') { + console.debug('[AutoSave] Skipped: getPayload returned null/undefined') + } + return + } const hash = safeHash(payload) - if (lastSentHash && hash === lastSentHash) return + if (lastSentHash && hash === lastSentHash) { + if (typeof window !== 'undefined' && window.location.hostname === 'localhost') { + console.debug('[AutoSave] Skipped: payload unchanged (hash match)') + } + return + } if (controller) controller.abort() controller = new AbortController() + if (typeof window !== 'undefined' && window.location.hostname === 'localhost') { + console.debug('[AutoSave] Saving...', { hashChanged: lastSentHash !== hash }) + } + setStatus('saving') lastError = null try { const res = await opts.save(payload, { signal: controller.signal }) lastSentHash = hash setStatus('saved') + if (typeof window !== 'undefined' && window.location.hostname === 'localhost') { + console.debug('[AutoSave] Saved successfully') + } if (opts.onSaved) opts.onSaved(res, { prime }) - } catch (e: unknown) { - if (e?.name === 'AbortError') { + } catch (e) { + if (e instanceof Error && e.name === 'AbortError') { // Newer save superseded this one + if (typeof window !== 'undefined' && window.location.hostname === 'localhost') { + console.debug('[AutoSave] Aborted: superseded by newer save') + } return } if (typeof navigator !== 'undefined' && navigator.onLine === false) { @@ -105,7 +128,10 @@ export function createAutoSaveStore( } else { setStatus('error') } - lastError = e?.message || 'Auto-save failed' + lastError = e instanceof Error ? e.message : 'Auto-save failed' + if (typeof window !== 'undefined' && window.location.hostname === 'localhost') { + console.debug('[AutoSave] Error:', lastError) + } } } diff --git a/src/lib/components/admin/AutoSaveStatus.svelte b/src/lib/components/admin/AutoSaveStatus.svelte index 9269fc3..9185ce6 100644 --- a/src/lib/components/admin/AutoSaveStatus.svelte +++ b/src/lib/components/admin/AutoSaveStatus.svelte @@ -10,6 +10,7 @@ lastSavedAt?: Date | string | null showTimestamp?: boolean compact?: boolean + onclick?: () => void } let { @@ -19,7 +20,8 @@ error: errorProp, lastSavedAt, showTimestamp = true, - compact = true + compact = true, + onclick }: Props = $props() // Support both old subscription-based stores and new reactive values @@ -81,12 +83,19 @@ {#if label} -
+
+ {/if}