@classic-homes/offline-react 0.1.5 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +84 -20
- package/package.json +1 -6
package/dist/index.js
CHANGED
|
@@ -191,6 +191,9 @@ function usePersistedForm(options) {
|
|
|
191
191
|
const lastSubmitTimeRef = useRef(0);
|
|
192
192
|
const submittingRef = useRef(false);
|
|
193
193
|
const debounceTimerRef = useRef(null);
|
|
194
|
+
const loadPromiseRef = useRef(Promise.resolve());
|
|
195
|
+
const touchedRef = useRef({});
|
|
196
|
+
const touchedFileRefsRef = useRef({});
|
|
194
197
|
const originalValuesRef = useRef(JSON.parse(JSON.stringify(initialValues)));
|
|
195
198
|
const dataRef = useRef(data);
|
|
196
199
|
dataRef.current = data;
|
|
@@ -208,12 +211,12 @@ function usePersistedForm(options) {
|
|
|
208
211
|
debounceTimerRef.current = null;
|
|
209
212
|
}
|
|
210
213
|
}, []);
|
|
211
|
-
useEffect(() => cancelPendingSave, [cancelPendingSave]);
|
|
212
214
|
const isValid = Object.keys(errors).length === 0 && schema.safeParse(data).success;
|
|
213
215
|
const saveDraft = useCallback(async () => {
|
|
214
216
|
if (!isOfflineStorageInitialized() || !isDirtyRef.current) {
|
|
215
217
|
return;
|
|
216
218
|
}
|
|
219
|
+
await loadPromiseRef.current;
|
|
217
220
|
try {
|
|
218
221
|
const storage = getOfflineStorage();
|
|
219
222
|
const { excludeFields: excludeFields2 = [], expirationMs: expirationMs2 = DEFAULT_EXPIRATION_MS } = optionsRef.current;
|
|
@@ -223,9 +226,11 @@ function usePersistedForm(options) {
|
|
|
223
226
|
delete dataToPersist[field];
|
|
224
227
|
}
|
|
225
228
|
if (multiDraft) {
|
|
226
|
-
|
|
229
|
+
let draftId = currentDraftIdRef.current;
|
|
230
|
+
if (draftId && !await storage.draftManager.updateDraft(draftId, dataToPersist, fileRefs2)) {
|
|
231
|
+
draftId = null;
|
|
232
|
+
}
|
|
227
233
|
if (draftId) {
|
|
228
|
-
await storage.draftManager.updateDraft(draftId, dataToPersist, fileRefs2);
|
|
229
234
|
const metadata = await storage.draftManager.getDraftMetadata(draftId);
|
|
230
235
|
if (metadata) {
|
|
231
236
|
setCurrentDraftMetadata(metadata);
|
|
@@ -260,6 +265,22 @@ function usePersistedForm(options) {
|
|
|
260
265
|
void saveDraft();
|
|
261
266
|
}, debounceMs);
|
|
262
267
|
}, [saveDraft, debounceMs, cancelPendingSave]);
|
|
268
|
+
const flushPendingSave = useCallback(() => {
|
|
269
|
+
if (debounceTimerRef.current) {
|
|
270
|
+
cancelPendingSave();
|
|
271
|
+
void saveDraft();
|
|
272
|
+
}
|
|
273
|
+
}, [saveDraft, cancelPendingSave]);
|
|
274
|
+
const flushRef = useRef(flushPendingSave);
|
|
275
|
+
flushRef.current = flushPendingSave;
|
|
276
|
+
useEffect(() => {
|
|
277
|
+
const onPageHide = () => flushRef.current();
|
|
278
|
+
window.addEventListener("pagehide", onPageHide);
|
|
279
|
+
return () => {
|
|
280
|
+
window.removeEventListener("pagehide", onPageHide);
|
|
281
|
+
flushRef.current();
|
|
282
|
+
};
|
|
283
|
+
}, []);
|
|
263
284
|
useEffect(() => {
|
|
264
285
|
let cancelled = false;
|
|
265
286
|
async function loadDraft() {
|
|
@@ -275,10 +296,18 @@ function usePersistedForm(options) {
|
|
|
275
296
|
} else {
|
|
276
297
|
draft = await storage.draftManager.getCurrentDraft(storageKey);
|
|
277
298
|
}
|
|
278
|
-
if (draft && !cancelled
|
|
299
|
+
if (draft && !cancelled) {
|
|
279
300
|
const { initialValues: initialValues2, onDraftRestored: onDraftRestored2, onDraftChange: onDraftChange2 } = optionsRef.current;
|
|
280
|
-
|
|
281
|
-
|
|
301
|
+
const restored = {
|
|
302
|
+
...JSON.parse(JSON.stringify(initialValues2)),
|
|
303
|
+
...draft.data,
|
|
304
|
+
...touchedRef.current
|
|
305
|
+
};
|
|
306
|
+
const restoredFileRefs = { ...draft.fileRefs, ...touchedFileRefsRef.current };
|
|
307
|
+
dataRef.current = restored;
|
|
308
|
+
fileRefsRef.current = restoredFileRefs;
|
|
309
|
+
setData(restored);
|
|
310
|
+
setFileRefs(restoredFileRefs);
|
|
282
311
|
currentDraftIdRef.current = draft.metadata.id;
|
|
283
312
|
setCurrentDraftId(draft.metadata.id);
|
|
284
313
|
setCurrentDraftMetadata(draft.metadata);
|
|
@@ -288,10 +317,18 @@ function usePersistedForm(options) {
|
|
|
288
317
|
}
|
|
289
318
|
} else {
|
|
290
319
|
const result = await storage.drafts.loadDraft(storageKey);
|
|
291
|
-
if (result.restored && result.draft && !cancelled
|
|
320
|
+
if (result.restored && result.draft && !cancelled) {
|
|
292
321
|
const { initialValues: initialValues2, onDraftRestored: onDraftRestored2 } = optionsRef.current;
|
|
293
|
-
|
|
294
|
-
|
|
322
|
+
const restored = {
|
|
323
|
+
...JSON.parse(JSON.stringify(initialValues2)),
|
|
324
|
+
...result.draft.data,
|
|
325
|
+
...touchedRef.current
|
|
326
|
+
};
|
|
327
|
+
const restoredFileRefs = { ...result.draft.fileRefs, ...touchedFileRefsRef.current };
|
|
328
|
+
dataRef.current = restored;
|
|
329
|
+
fileRefsRef.current = restoredFileRefs;
|
|
330
|
+
setData(restored);
|
|
331
|
+
setFileRefs(restoredFileRefs);
|
|
295
332
|
setDraftRestored(true);
|
|
296
333
|
onDraftRestored2?.();
|
|
297
334
|
}
|
|
@@ -300,7 +337,7 @@ function usePersistedForm(options) {
|
|
|
300
337
|
console.warn("Failed to load form draft:", err);
|
|
301
338
|
}
|
|
302
339
|
}
|
|
303
|
-
|
|
340
|
+
loadPromiseRef.current = loadDraft();
|
|
304
341
|
return () => {
|
|
305
342
|
cancelled = true;
|
|
306
343
|
};
|
|
@@ -308,6 +345,7 @@ function usePersistedForm(options) {
|
|
|
308
345
|
const setField = useCallback(
|
|
309
346
|
(field, value) => {
|
|
310
347
|
setData((prev) => ({ ...prev, [field]: value }));
|
|
348
|
+
touchedRef.current = { ...touchedRef.current, [field]: value };
|
|
311
349
|
isDirtyRef.current = true;
|
|
312
350
|
setIsDirty(true);
|
|
313
351
|
scheduleSaveDraft();
|
|
@@ -317,6 +355,7 @@ function usePersistedForm(options) {
|
|
|
317
355
|
const setFields = useCallback(
|
|
318
356
|
(fields) => {
|
|
319
357
|
setData((prev) => ({ ...prev, ...fields }));
|
|
358
|
+
touchedRef.current = { ...touchedRef.current, ...fields };
|
|
320
359
|
isDirtyRef.current = true;
|
|
321
360
|
setIsDirty(true);
|
|
322
361
|
scheduleSaveDraft();
|
|
@@ -369,6 +408,13 @@ function usePersistedForm(options) {
|
|
|
369
408
|
for (const fieldName of fileFields) {
|
|
370
409
|
const ids = fileRefs[fieldName] || [];
|
|
371
410
|
const fileMap = await storage.files.getFilesAsFileObjects(ids);
|
|
411
|
+
const missing = ids.filter((id) => !fileMap.has(id));
|
|
412
|
+
if (missing.length > 0) {
|
|
413
|
+
console.warn(
|
|
414
|
+
`[usePersistedForm] ${missing.length} file(s) for "${fieldName}" are missing from storage (expired or deleted)`,
|
|
415
|
+
missing
|
|
416
|
+
);
|
|
417
|
+
}
|
|
372
418
|
result[fieldName] = Array.from(fileMap.values());
|
|
373
419
|
}
|
|
374
420
|
return result;
|
|
@@ -380,17 +426,19 @@ function usePersistedForm(options) {
|
|
|
380
426
|
try {
|
|
381
427
|
const storage = getOfflineStorage();
|
|
382
428
|
const draftId = currentDraftIdRef.current;
|
|
383
|
-
if (multiDraft
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
429
|
+
if (multiDraft) {
|
|
430
|
+
if (draftId) {
|
|
431
|
+
await storage.draftManager.deleteDraft(draftId, true);
|
|
432
|
+
currentDraftIdRef.current = null;
|
|
433
|
+
setCurrentDraftId(null);
|
|
434
|
+
setCurrentDraftMetadata(null);
|
|
435
|
+
optionsRef.current.onDraftChange?.(null);
|
|
436
|
+
}
|
|
389
437
|
} else {
|
|
390
438
|
await storage.drafts.clearDraft(storageKey);
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
439
|
+
for (const fieldName of fileFields) {
|
|
440
|
+
await storage.files.deleteFormFiles(storageKey, fieldName);
|
|
441
|
+
}
|
|
394
442
|
}
|
|
395
443
|
} catch (err) {
|
|
396
444
|
console.warn("Failed to clear form draft:", err);
|
|
@@ -424,13 +472,23 @@ function usePersistedForm(options) {
|
|
|
424
472
|
} catch (err) {
|
|
425
473
|
setGlobalError(err instanceof Error ? err.message : "Submission failed");
|
|
426
474
|
lastSubmitTimeRef.current = 0;
|
|
475
|
+
void saveDraft();
|
|
427
476
|
throw err;
|
|
428
477
|
} finally {
|
|
429
478
|
submittingRef.current = false;
|
|
430
479
|
setIsSubmitting(false);
|
|
431
480
|
}
|
|
432
481
|
},
|
|
433
|
-
[
|
|
482
|
+
[
|
|
483
|
+
data,
|
|
484
|
+
validate,
|
|
485
|
+
onSubmit,
|
|
486
|
+
getFilesForSubmit,
|
|
487
|
+
clearDraft,
|
|
488
|
+
submitCooldownMs,
|
|
489
|
+
cancelPendingSave,
|
|
490
|
+
saveDraft
|
|
491
|
+
]
|
|
434
492
|
);
|
|
435
493
|
const reset = useCallback(() => {
|
|
436
494
|
cancelPendingSave();
|
|
@@ -439,6 +497,8 @@ function usePersistedForm(options) {
|
|
|
439
497
|
setGlobalError(null);
|
|
440
498
|
setIsSubmitted(false);
|
|
441
499
|
isDirtyRef.current = false;
|
|
500
|
+
touchedRef.current = {};
|
|
501
|
+
touchedFileRefsRef.current = {};
|
|
442
502
|
setIsDirty(false);
|
|
443
503
|
setDraftRestored(false);
|
|
444
504
|
setFileRefs({});
|
|
@@ -454,6 +514,7 @@ function usePersistedForm(options) {
|
|
|
454
514
|
const updateFileRefs = useCallback(
|
|
455
515
|
(fieldName, fileIds) => {
|
|
456
516
|
setFileRefs((prev) => ({ ...prev, [fieldName]: fileIds }));
|
|
517
|
+
touchedFileRefsRef.current = { ...touchedFileRefsRef.current, [fieldName]: fileIds };
|
|
457
518
|
isDirtyRef.current = true;
|
|
458
519
|
setIsDirty(true);
|
|
459
520
|
scheduleSaveDraft();
|
|
@@ -504,6 +565,8 @@ function usePersistedForm(options) {
|
|
|
504
565
|
setCurrentDraftId(draft.metadata.id);
|
|
505
566
|
setCurrentDraftMetadata(draft.metadata);
|
|
506
567
|
isDirtyRef.current = false;
|
|
568
|
+
touchedRef.current = {};
|
|
569
|
+
touchedFileRefsRef.current = {};
|
|
507
570
|
setIsDirty(false);
|
|
508
571
|
setDraftRestored(true);
|
|
509
572
|
await storage.draftManager.setCurrentDraft(storageKey, draftIdToLoad);
|
|
@@ -846,6 +909,7 @@ function useDraftSync(options) {
|
|
|
846
909
|
setIsSyncing(false);
|
|
847
910
|
setLastSyncAt(result.timestamp);
|
|
848
911
|
setLastResult(result);
|
|
912
|
+
setPendingCount(service.pendingCount);
|
|
849
913
|
setConflicts(service.getConflicts());
|
|
850
914
|
setError(result.error ?? null);
|
|
851
915
|
optionsRef.current.onSyncComplete?.(result);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@classic-homes/offline-react",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "React bindings for offline persistence and caching",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -46,11 +46,6 @@
|
|
|
46
46
|
"react": "^18.0.0 || ^19.0.0",
|
|
47
47
|
"zod": "^3.23.0"
|
|
48
48
|
},
|
|
49
|
-
"peerDependenciesMeta": {
|
|
50
|
-
"zod": {
|
|
51
|
-
"optional": true
|
|
52
|
-
}
|
|
53
|
-
},
|
|
54
49
|
"dependencies": {
|
|
55
50
|
"@classic-homes/offline-core": "*"
|
|
56
51
|
},
|