Spaces:
Running on Zero
Running on Zero
Download frontend/src/useDraft.js from ManasT2485/readloops: direct link, hf CLI and curl.
- Browser
- Download file 5.73 kB
-
https://huggingface.co/spaces/ManasT2485/readloops/resolve/main/frontend/src/useDraft.js
- Command line
-
hf download hf://spaces/ManasT2485/readloops/frontend/src/useDraft.js
-
curl -L -o useDraft.js https://huggingface.co/spaces/ManasT2485/readloops/resolve/main/frontend/src/useDraft.js
5.73 kB
| import { useEffect, useRef, useState } from 'react' | |
| import { api } from './api.js' | |
| const keyFor = id => `readloops:draft:${id}` | |
| const signatureOf = value => JSON.stringify({ step: value.step ?? 0, transcript: value.transcript ?? '', source: value.source ?? 'manual', compared: Boolean(value.compared), transcript_reviewed: Boolean(value.transcript_reviewed), dismissed: value.dismissed ?? [] }) | |
| const acknowledged = (session, local) => Boolean(session.last_write_id && (local?.pending_write?.write_id || local?.write_id) === session.last_write_id) | |
| function cached(id, storage = sessionStorage) { | |
| try { return JSON.parse(storage.getItem(keyFor(id)) || 'null') } catch { return null } | |
| } | |
| export function initialDraft(session, storage = sessionStorage) { | |
| const local = cached(session.id, storage) | |
| if (session.status === 'active' && !session.decision && local?.draft && (local.revision === session.revision || acknowledged(session, local))) return local.draft | |
| return session.draft | |
| } | |
| export function clearDraft(id, storage = sessionStorage) { | |
| try { storage.removeItem(keyFor(id)) } catch { /* Server data is still authoritative. */ } | |
| } | |
| function recoveryConflict(session, storage) { | |
| const local = cached(session.id, storage) | |
| return local?.draft && local.revision !== session.revision && !acknowledged(session, local) && signatureOf(local.draft) !== signatureOf(session.draft) ? local.draft : null | |
| } | |
| export function useDraft(session, draft, enabled, storage = sessionStorage) { | |
| const latest = useRef(draft) | |
| latest.current = draft | |
| const revision = useRef(session.revision) | |
| const committed = useRef(signatureOf(session.draft)) | |
| const pending = useRef(cached(session.id, storage)) | |
| const unacknowledged = useRef(acknowledged(session, pending.current) ? null : (pending.current?.pending_write ?? (pending.current && !('pending_write' in pending.current) ? pending.current : null))) | |
| const flight = useRef(null) | |
| const timer = useRef(null) | |
| const alive = useRef(true) | |
| const [status, setStatus] = useState('Saved for this demo visit') | |
| const [error, setError] = useState('') | |
| const [conflict, setConflict] = useState(() => recoveryConflict(session, storage)) | |
| function retain(value) { | |
| const same = pending.current && signatureOf(pending.current.draft) === signatureOf(value) && pending.current.revision === revision.current | |
| const next = { revision: revision.current, write_id: same ? pending.current.write_id : crypto.randomUUID(), draft: value, pending_write: unacknowledged.current } | |
| pending.current = next | |
| try { storage.setItem(keyFor(session.id), JSON.stringify(next)) } | |
| catch { if (alive.current) setError('Browser recovery storage is unavailable. Keep this tab open until the server save succeeds.') } | |
| return next | |
| } | |
| async function flush(override) { | |
| if (conflict) throw new Error('Resolve the older browser draft before saving.') | |
| clearTimeout(timer.current) | |
| if (override) latest.current = override | |
| if (flight.current) await flight.current | |
| if (!unacknowledged.current && signatureOf(latest.current) === committed.current) return | |
| flight.current = (async () => { | |
| while (unacknowledged.current || signatureOf(latest.current) !== committed.current) { | |
| const retained = retain(latest.current) | |
| const change = unacknowledged.current || { revision: retained.revision, write_id: retained.write_id, draft: retained.draft } | |
| unacknowledged.current = change | |
| retain(latest.current) | |
| if (alive.current) setStatus('Saving...') | |
| const saved = await api(`/sessions/${session.id}`, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(change) }) | |
| if (signatureOf(saved.draft) !== signatureOf(change.draft)) { | |
| throw new Error('The backend saved a different draft. Restart the backend and reopen this session before retrying.') | |
| } | |
| revision.current = saved.revision | |
| committed.current = signatureOf(saved.draft) | |
| unacknowledged.current = null | |
| if (signatureOf(latest.current) === committed.current) { clearDraft(session.id, storage); pending.current = null } | |
| else retain(latest.current) | |
| } | |
| if (alive.current) { setStatus('Saved for this demo visit'); setError('') } | |
| })() | |
| try { await flight.current } | |
| catch (failure) { | |
| if (alive.current) { setStatus('Save pending'); setError(`${failure.message} ${storage === sessionStorage ? 'Your pending draft is kept only in this tab. Save successfully before closing it.' : 'Your draft is retained in this browser when storage is available.'}`) } | |
| throw failure | |
| } finally { flight.current = null } | |
| } | |
| function adopt(saved) { | |
| revision.current = saved.revision | |
| committed.current = signatureOf(saved.draft) | |
| latest.current = saved.draft | |
| clearDraft(session.id, storage) | |
| pending.current = null | |
| unacknowledged.current = null | |
| if (alive.current) { setStatus('Saved for this demo visit'); setError('') } | |
| } | |
| const signature = signatureOf(draft) | |
| useEffect(() => { | |
| if (!enabled || conflict || (signature === committed.current && !unacknowledged.current)) return | |
| retain(draft) | |
| setStatus('Save pending') | |
| timer.current = setTimeout(() => { flush().catch(() => {}) }, 600) | |
| return () => clearTimeout(timer.current) | |
| }, [signature, enabled, conflict]) | |
| useEffect(() => { | |
| alive.current = true | |
| return () => { alive.current = false; clearTimeout(timer.current) } | |
| }, []) | |
| function useSaved() { clearDraft(session.id, storage); pending.current = null; unacknowledged.current = null; setConflict(null) } | |
| return { status, error, flush, adopt, conflict, useSaved, getRevision: () => revision.current } | |
| } | |