Longevity Command Center

Hybrid strategy: optimize hard; meds only when ROI is clear Not connected Signed out
Multi-user login + cloud-synced labs (Supabase). Each user sees only their own data (with RLS).

Sign in

Passwords are managed by Supabase Auth. If signup requires email confirmation, check your inbox.

`); w.document.close(); } function initWorkoutUI(){ const buildBtn = document.getElementById("wpBuild"); const saveBtn = document.getElementById("wpSave"); const loadBtn = document.getElementById("wpLoad"); const lastSaved = document.getElementById("wpLastSaved"); const wsInput = document.getElementById("wpWeekStart"); if(wsInput && !wsInput.value) wsInput.value = nextMondayISO(); const pdfBtn = document.getElementById("wpPdf"); if(pdfBtn){ pdfBtn.addEventListener("click", ()=>{ try{ exportPlanToPDF(); }catch(e){ alert("PDF export failed: "+(e.message||e)); } }); } if(buildBtn){ buildBtn.addEventListener("click", () => { try{ setWorkoutStatus("Building…"); const mode = document.getElementById("wpMode").value; const days = document.getElementById("wpDays").value; const minutes = parseInt(document.getElementById("wpTime").value, 10); const strengthDays = document.getElementById("wpStrengthDays").value; const equip = document.getElementById("wpEquip").value; const limits = document.getElementById("wpLimits").value; const strengthGoal = document.getElementById("wpStrengthGoal").value; const age = document.getElementById("wpAge").value; const weekStart = document.getElementById("wpWeekStart").value; const vo2 = latestNumericForMetric("VO2 Max") ?? latestNumericForMetric("VO₂ Max"); const rhr = latestNumericForMetric("Resting Heart Rate") ?? latestNumericForMetric("RHR"); const plan = buildBasePlan({ mode, days, minutes, strengthDays, equip, strengthGoal, limits, vo2, rhr, age, weekStart }); CURRENT_WORKOUT_PLAN = plan; renderPlan("wpOutput", plan); renderWorkoutTracking(plan, weekStart || null); document.getElementById("progPill").textContent = "Ready to generate next 4-week block"; document.getElementById("progActiveBlock").textContent = `Current block: ${plan.block}`; setWorkoutStatus("Plan built"); }catch(e){ console.error(e); setWorkoutStatus(`Error: ${e.message || e}`); } }); } if(loadBtn){ loadBtn.addEventListener("click", async () => { try{ setWorkoutStatus("Loading…"); const plan = await loadLatestPlanFromSupabase(); CURRENT_WORKOUT_PLAN = plan; renderPlan("wpOutput", plan); renderWorkoutTracking(plan, weekStart || null); lastSaved.textContent = plan ? `Loaded block ${plan.block || 1}` : "No saved plan"; document.getElementById("progPill").textContent = plan ? "Ready to generate next 4-week block" : "Build or load a plan first"; document.getElementById("progActiveBlock").textContent = plan ? `Current block: ${plan.block || 1}` : "—"; setWorkoutStatus(plan ? "Loaded" : "No saved plan"); }catch(e){ console.error(e); setWorkoutStatus(`Load failed: ${e.message || e}`); } }); } if(saveBtn){ saveBtn.addEventListener("click", async () => { try{ if(!CURRENT_WORKOUT_PLAN) throw new Error("Build or load a plan first."); setWorkoutStatus("Saving…"); await savePlanToSupabase(CURRENT_WORKOUT_PLAN); lastSaved.textContent = `Saved block ${CURRENT_WORKOUT_PLAN.block || 1}`; setWorkoutStatus("Saved"); }catch(e){ console.error(e); setWorkoutStatus(`Save failed: ${e.message || e}`); } }); } const progGen = document.getElementById("progGenerate"); const progSave = document.getElementById("progSave"); if(progGen){ progGen.addEventListener("click", () => { try{ if(!CURRENT_WORKOUT_PLAN) throw new Error("Build or load a plan first."); setProgStatus("Generating…"); const next = progressedPlan(CURRENT_WORKOUT_PLAN); const startDate = document.getElementById("progStartDate").value; if(startDate) next.startDate = startDate; CURRENT_WORKOUT_PLAN = next; renderPlan("progOutput", next); document.getElementById("progActiveBlock").textContent = `Current block: ${next.block || 1}`; setProgStatus("Block generated"); }catch(e){ console.error(e); setProgStatus(`Error: ${e.message || e}`); } }); } if(progSave){ progSave.addEventListener("click", async () => { try{ if(!CURRENT_WORKOUT_PLAN) throw new Error("Generate a block first."); setProgStatus("Saving…"); await savePlanToSupabase(CURRENT_WORKOUT_PLAN); setProgStatus("Saved"); }catch(e){ console.error(e); setProgStatus(`Save failed: ${e.message || e}`); } }); } } function setActiveTab(tab){ document.querySelectorAll('[data-tab]').forEach(el => { el.classList.toggle('hidden', !String(el.getAttribute('data-tab')||'').split(/\s+/).includes(tab)); }); document.querySelectorAll('button[data-tabbtn]').forEach(b => { b.classList.remove('primary'); b.classList.add('ghost'); }); const active = document.querySelector(`button[data-tabbtn="${tab}"]`); if(active){ active.classList.add('primary'); active.classList.remove('ghost'); } if(tab === "trends"){ setTimeout(() => drawOfflineChart(), 50); } } document.addEventListener("click", (e) => { const btn = e.target.closest("button[data-tabbtn]"); if(!btn) return; setActiveTab(btn.getAttribute("data-tabbtn")); }); /* =========================== Auth UI + Data refresh =========================== */ async function refreshAuthUI(){ if(!supabase){ const sbm = document.getElementById("sbMissing"); if(sbm) sbm.style.display = "inline-flex"; document.getElementById("saveState").textContent = "Not connected"; document.getElementById("whoami").textContent = "Signed out"; document.getElementById("signOutBtn").classList.add("hidden"); document.getElementById("authCard").classList.remove("hidden"); document.getElementById("app").classList.add("hidden"); return; } const sbm = document.getElementById("sbMissing"); if(sbm) sbm.style.display = "none"; const { data } = await withTimeout(supabase.auth.getSession(), 8000, "Getting session"); const session = data?.session; if(!session){ document.getElementById("whoami").textContent = "Signed out"; document.getElementById("signOutBtn").classList.add("hidden"); document.getElementById("authCard").classList.remove("hidden"); document.getElementById("app").classList.add("hidden"); return; } document.getElementById("whoami").textContent = session.user.email || "Signed in"; document.getElementById("signOutBtn").classList.remove("hidden"); document.getElementById("authCard").classList.add("hidden"); document.getElementById("app").classList.remove("hidden"); await refreshLabs(); setActiveTab("dashboard"); } async function refreshLabs(){ document.getElementById("saveState").textContent = "Loading…"; const labs = await withTimeout(loadLabsFromCloud(), 12000, "Loading labs"); STATE = { labs }; document.getElementById("saveState").textContent = "Synced"; renderAll(); } async function safeOp(fn){ try { await fn(); } catch(err){ alert(err?.message || String(err)); } finally { /* keep last status unless already set */ const el=document.getElementById("saveState"); if(el && !/Signing|Creating|Loading|Saving|Deleting|Clearing/i.test(el.textContent)) el.textContent = el.textContent || "Synced"; } } /* =========================== Auth actions =========================== */ document.getElementById("signInBtn").addEventListener("click", async () => { if(!supabase) return alert("Set Supabase URL/Anon key first."); const email = document.getElementById("emailInput").value.trim(); const password = document.getElementById("passwordInput").value; if(!email || !password) return alert("Enter email + password."); const signInBtn = document.getElementById("signInBtn"); const signUpBtn = document.getElementById("signUpBtn"); signInBtn.disabled = true; signUpBtn.disabled = true; await safeOp(async ()=>{ document.getElementById("saveState").textContent = "Signing in…"; // If there's a stale local session, clear it first (common cause of "stuck signing in" on reload) const existing = await withTimeout(supabase.auth.getSession(), 8000, "Checking session"); if(existing?.data?.session){ await withTimeout(supabase.auth.signOut({ scope: "local" }), 8000, "Clearing prior session"); } const { error } = await withTimeout( supabase.auth.signInWithPassword({ email, password }), 12000, "Signing in" ); if(error) throw error; await withTimeout(refreshAuthUI(), 12000, "Refreshing app"); }); signInBtn.disabled = false; signUpBtn.disabled = false; }); document.getElementById("signUpBtn").addEventListener("click", async () => { if(!supabase) return alert("Set Supabase URL/Anon key first."); const email = document.getElementById("emailInput").value.trim(); const password = document.getElementById("passwordInput").value; if(!email || !password) return alert("Enter email + password."); await safeOp(async ()=>{ document.getElementById("saveState").textContent = "Creating…"; const { error } = await withTimeout(supabase.auth.signUp({ email, password }), 12000, "Creating account"); if(error) throw error; alert("Account created. If email confirmation is enabled, confirm from your inbox, then sign in."); }); }); document.getElementById("signOutBtn").addEventListener("click", async () => { if(!supabase) return; await safeOp(async ()=>{ document.getElementById("saveState").textContent = "Signing out…"; await withTimeout(supabase.auth.signOut({ scope: "local" }), 8000, "Signing out"); STATE = { labs: [] }; renderAll(); await refreshAuthUI(); }); }); /* =========================== Lab actions (cloud) =========================== */ document.getElementById("addLabBtn").addEventListener("click", async () => { const date = document.getElementById("labDate").value; const metric = document.getElementById("labMetric").value; const value = document.getElementById("labValue").value.trim(); if(!date || !metric || !value){ alert("Enter date, metric, value."); return; } await safeOp(async ()=>{ document.getElementById("saveState").textContent = "Saving…"; await addLabToCloud(date, metric, value); document.getElementById("labValue").value = ""; await refreshLabs(); }); }); document.getElementById("clearAllBtn").addEventListener("click", async () => { if(!confirm("Clear ALL of your labs from the cloud? This cannot be undone.")) return; await safeOp(async ()=>{ document.getElementById("saveState").textContent = "Clearing…"; await clearAllFromCloud(); await refreshLabs(); }); }); document.getElementById("chartMetric").addEventListener("change", drawOfflineChart); /* =========================== Backup / Import (still useful) =========================== */ document.getElementById("exportBtn").addEventListener("click", () => { const blob = new Blob([JSON.stringify(STATE, null, 2)], { type: "application/json" }); const url = URL.createObjectURL(blob); const a = document.createElement("a"); a.href = url; a.download = "longevity_labs_backup.json"; document.body.appendChild(a); a.click(); a.remove(); URL.revokeObjectURL(url); }); document.getElementById("importJson").addEventListener("change", async (e) => { const file = e.target.files?.[0]; if (!file) return; try { const text = await file.text(); const parsed = JSON.parse(text); if (!parsed.labs || !Array.isArray(parsed.labs)) { throw new Error("Invalid JSON format. Expected { labs: [...] }"); } const user = (await supabase.auth.getUser()).data.user; if (!user) throw new Error("Not signed in"); const rows = parsed.labs.map(l => ({ user_id: user.id, date: l.date, metric: l.metric, value: l.value })); const { error } = await supabase.from("labs").insert(rows); if (error) throw error; alert(`Imported ${rows.length} labs successfully`); await refreshLabs(); // or loadLabs() depending on your file } catch (err) { alert("Import failed: " + err.message); } finally { e.target.value = ""; } }); document.getElementById("importCsv").addEventListener("change", async (e) => { const file = e.target.files?.[0]; if(!file) return; await safeOp(async ()=>{ const text = await file.text(); const lines = text.split(/\r?\n/).map(l=>l.trim()).filter(Boolean); const start = /^\d{4}-\d{2}-\d{2}$/.test(lines[0].split(",")[0].trim()) ? 0 : 1; document.getElementById("saveState").textContent = "Importing…"; for(let i=start;i { await refreshAuthUI(); }); } initWorkoutUI(); refreshAuthUI();