// Add this utility function at the top of your script function safeCompress(data) { try { if (!data) return null; const jsonStr = JSON.stringify(data); return fflate.compressSync(new TextEncoder().encode(jsonStr)); } catch (e) { console.error("Compression failed, using uncompressed data:", e); return null; // Return null to indicate compression failed } } function safeDecompress(compressedData) { try { if (!compressedData) return null; const decompressed = fflate.decompressSync(compressedData); return JSON.parse(new TextDecoder().decode(decompressed)); } catch (e) { console.error("Decompression failed, trying to parse as JSON:", e); try { // Fallback to direct JSON parsing if decompression fails return JSON.parse(compressedData); } catch (e2) { console.error("Failed to parse data:", e2); return null; } } } async function loadAndUpdateGS(id) { idx = "myPanel"; const existing = document.getElementById(id); // Check and remove existing panel if it exists const existingPanel = document.getElementById(idx); if (existingPanel) { try { if (existingPanel.close) { existingPanel.close(); } else { existingPanel.remove(); } } catch (e) { console.error("Error removing existing panel:", e); } } // [Rest of your existing cleanup code...] try { const data = await loadDataOnly(id); console.log("✔️ Raw data from loadDataOnly():", data); // Modified decompression logic bucket = safeDecompress(data.bucket) || safeParseJSON(data.bucket) || {}; cup = safeDecompress(data.cup) || safeParseJSON(data.cup) || {}; board = safeDecompress(data.board) || safeParseJSON(data.board) || {}; console.log("✔️ Parsed bucket:", bucket); console.log("✔️ Parsed cup:", cup); console.log("✔️ Parsed board:", board); // [Rest of your existing loadAndUpdateGS code...] } catch (e) { console.error("❌ Error loading and summarizing:", e); } } async function saveToServerM(id) { // [Your existing early exit checks...] // Modified save payload preparation try { const startTime = Date.now(); // Prepare payload with compressed data (fallback to uncompressed if needed) const compressBucket = safeCompress(bucket) || JSON.stringify(bucket || {}); const compressCup = safeCompress(cup) || JSON.stringify(cup || {}); const compressBoard = safeCompress(board) || JSON.stringify(board || {}); const payload = { data: { id, name: id, bucket: compressBucket instanceof Uint8Array ? Array.from(compressBucket) // Convert to array for JSON serialization : compressBucket, cup: compressCup instanceof Uint8Array ? Array.from(compressCup) : compressCup, board: compressBoard instanceof Uint8Array ? Array.from(compressBoard) : compressBoard, timestamp: new Date().toISOString(), compression: compressBucket instanceof Uint8Array ? "fflate" : "none" }, }; // [Rest of your existing saveToServerM code...] } catch (e) { console.error("❌ Save failed:", e.message); toast( `❌ Save failed: ${e.message}`, { pos: "TR", slide: "ttb" }, { background: "red" }, ); return false; } finally { setTimeout(() => closePanel("verticalProgressPanel2"), 1500); } }