feat: implement robust offline-first sync with Dexie.js
- Migrated to Dexie.js for IndexedDB management - Added optimistic UI for tasting notes with 'Wartet auf Sync' badge - Implemented background caching for tags and buddies - Unified scanning and tasting sync in a global UploadQueue
This commit is contained in:
44
src/hooks/useCacheSync.ts
Normal file
44
src/hooks/useCacheSync.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import { useEffect } from 'react';
|
||||
import { createClientComponentClient } from '@supabase/auth-helpers-nextjs';
|
||||
import { db } from '@/lib/db';
|
||||
import { getAllSystemTags } from '@/services/tags';
|
||||
|
||||
export function useCacheSync() {
|
||||
const supabase = createClientComponentClient();
|
||||
|
||||
useEffect(() => {
|
||||
const syncCache = async () => {
|
||||
if (!navigator.onLine) return;
|
||||
|
||||
try {
|
||||
// 1. Sync Tags
|
||||
const tags = await getAllSystemTags();
|
||||
if (tags.length > 0) {
|
||||
await db.cache_tags.bulkPut(tags.map(t => ({
|
||||
id: t.id,
|
||||
name: t.name,
|
||||
category: t.category,
|
||||
is_system_default: t.is_system_default,
|
||||
popularity_score: t.popularity_score || 0
|
||||
})));
|
||||
}
|
||||
|
||||
// 2. Sync Buddies
|
||||
const { data: buddies } = await supabase
|
||||
.from('buddies')
|
||||
.select('id, name')
|
||||
.order('name');
|
||||
|
||||
if (buddies && buddies.length > 0) {
|
||||
await db.cache_buddies.bulkPut(buddies);
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Failed to sync offline cache:', err);
|
||||
}
|
||||
};
|
||||
|
||||
syncCache();
|
||||
window.addEventListener('online', syncCache);
|
||||
return () => window.removeEventListener('online', syncCache);
|
||||
}, [supabase]);
|
||||
}
|
||||
Reference in New Issue
Block a user