DocDoeAI / src /lib /api /class-session-progress.ts
asnannp's picture
deploy: sync backend to Space root (learn-lesson HF cache fix)
65d68f5
Raw History Blame Contribute Delete
1.55 kB
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<boolean> {
if (!hasLiveSession()) return false;
try {
await apiPost<ClassSessionProgressRemote, { class_session_id: string; data: ClassSessionProgressState }>(
"/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<ClassSessionProgressState | null> {
if (!hasLiveSession()) return null;
try {
const rows = await apiGet<ClassSessionProgressRemote[]>("/class-session-progress");
const row = (rows ?? []).find((item) => item.class_session_id === id);
return row?.data ?? null;
} catch {
return null;
}
}