refactor: extract useSync hook from page.tsx

This commit is contained in:
2026-04-19 12:24:25 +03:00
parent f5441a7ca7
commit 6dfc76ad92
2 changed files with 46 additions and 18 deletions

View File

@@ -6,6 +6,7 @@ import { inventoryApi } from '@/lib/api';
import { fetchAndCacheItems, syncOfflineOperations } from '@/lib/sync';
import { useScanner } from '@/hooks/useScanner';
import { useStockAdjustment } from '@/hooks/useStockAdjustment';
import { useSync } from '@/hooks/useSync';
import Scanner from '@/components/Scanner';
import AIOnboarding from '@/components/AIOnboarding';
import PageShell from '@/components/PageShell';
@@ -55,7 +56,6 @@ export default function Home() {
const [isEditing, setIsEditing] = useState(false);
const [editedItem, setEditedItem] = useState<Partial<Item>>({});
const [trashReason, setTrashReason] = useState('Damaged');
const [syncing, setSyncing] = useState(false);
const [currentUser, setCurrentUser] = useState<any | null>(null);
const [categories, setCategories] = useState<any[]>([]);
const [comparisonModal, setComparisonModal] = useState<{ show: boolean, newItem: any, existingItem: any, existingId: number | null }>({ show: false, newItem: null, existingItem: null, existingId: null });
@@ -106,6 +106,12 @@ export default function Home() {
onInventoryUpdate: setInventory
});
const { syncing, handleSync } = useSync({
isOnline,
currentUser,
onInventoryUpdate: setInventory
});
useEffect(() => {
if (!localStorage.getItem('inventory_token')) {
window.location.href = '/login';
@@ -248,23 +254,6 @@ export default function Home() {
loadInventory();
};
const handleSync = useCallback(async () => {
if (!isOnline || !currentUser) return;
setSyncing(true);
try {
const result = await syncOfflineOperations(currentUser.id);
if (result.success > 0) {
toast.success(`Synced ${result.success} operations!`);
}
await loadInventory();
} catch (error) {
console.error("Sync failed", error);
} finally {
setSyncing(false);
}
}, [isOnline, currentUser]);
const handleUpdateItem = async () => {
if (!selectedItem) return;
try {

39
frontend/hooks/useSync.ts Normal file
View File

@@ -0,0 +1,39 @@
import { useCallback, useState } from 'react';
import { toast } from 'react-hot-toast';
import { syncOfflineOperations, fetchAndCacheItems } from '@/lib/sync';
interface UseSyncOptions {
isOnline: boolean;
currentUser: any;
onInventoryUpdate?: (items: any[]) => void;
}
export function useSync(options: UseSyncOptions) {
const { isOnline, currentUser, onInventoryUpdate } = options;
const [syncing, setSyncing] = useState(false);
const handleSync = useCallback(async () => {
if (!isOnline || !currentUser) return;
setSyncing(true);
try {
const result = await syncOfflineOperations(currentUser.id);
if (result.success > 0) {
toast.success(`Synced ${result.success} operations!`);
}
const fresh = await fetchAndCacheItems();
if (onInventoryUpdate) {
onInventoryUpdate(fresh);
}
} catch (error) {
console.error("Sync failed", error);
toast.error("Sync failed");
} finally {
setSyncing(false);
}
}, [isOnline, currentUser, onInventoryUpdate]);
return {
syncing,
handleSync
};
}