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:
2025-12-19 13:40:56 +01:00
parent e08a18b2d5
commit 60ca3a6190
12 changed files with 417 additions and 181 deletions

44
src/hooks/useCacheSync.ts Normal file
View 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]);
}