• Saves sync report to storage for later reference; maintains history of 10 most recent reports.

    Parameters

    • report: SyncReport

      The sync report to save (timestamp should be ISO 8601 string).

    Returns void

    export function saveSyncReportToHistory(report: SyncReport): void {
    try {
    // Get existing history from storage
    const existingHistoryJson = storage.getItem(STORAGE_KEYS.SYNC_HISTORY);

    // Parse existing history with fallback to empty array on invalid JSON
    let existingHistory: SyncReport[] = [];
    if (existingHistoryJson) {
    try {
    const parsed = JSON.parse(existingHistoryJson);
    // Ensure parsed value is an array
    if (Array.isArray(parsed)) {
    // Filter to include only valid SyncReport objects with required fields
    existingHistory = parsed.filter(
    (item): item is SyncReport =>
    typeof item === "object" &&
    item !== null &&
    "timestamp" in item &&
    "totalEntries" in item &&
    "successfulUpdates" in item &&
    "failedUpdates" in item,
    );
    }
    } catch {
    // If JSON parsing fails, log and reset to empty array
    console.warn(
    "[Export] Failed to parse existing sync history, starting fresh",
    );
    existingHistory = [];
    }
    }

    // Create validated report with capped errors array (max 200 entries to bound size)
    const validatedReport: SyncReport = {
    timestamp: report.timestamp,
    totalEntries: report.totalEntries,
    successfulUpdates: report.successfulUpdates,
    failedUpdates: report.failedUpdates,
    skippedEntries: report.skippedEntries,
    errors: report.errors.slice(0, 200),
    };

    // Add new report to history (limit to most recent 10)
    const updatedHistory = [validatedReport, ...existingHistory].slice(0, 10);

    // Save back to storage
    storage.setItem(STORAGE_KEYS.SYNC_HISTORY, JSON.stringify(updatedHistory));

    console.debug("[Export] Sync report saved to history");
    } catch (error) {
    console.error("[Export] Failed to save sync report to history:", error);
    }
    }