import { apiGet, apiPost } from "@/lib/api/client"; import { hasLiveSession } from "@/lib/api/mode"; import type { ClassSessionProgressState } from "@/components/tuition/chapter-dashboard/classSessionProgress"; /** * Backend sync for tuition class session progress. * * localStorage (classSessionProgress.ts) gives instant refresh-restore on the * same device; this makes progress survive a device change / re-login. Both * helpers are best-effort and no-op when logged out. */ type ClassSessionProgressRemote = { class_session_id: string; data: ClassSessionProgressState; updated_at: string; }; /** Returns true when the progress was persisted to the backend. */ export async function saveClassSessionProgressRemote( id: string, state: ClassSessionProgressState, ): Promise { if (!hasLiveSession()) return false; try { await apiPost( "/class-session-progress", { class_session_id: id, data: state }, ); return true; } catch { return false; } } /** Returns the saved progress for one class session, or null. */ export async function getClassSessionProgressRemote(id: string): Promise { if (!hasLiveSession()) return null; try { const rows = await apiGet("/class-session-progress"); const row = (rows ?? []).find((item) => item.class_session_id === id); return row?.data ?? null; } catch { return null; } }