Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions src/hooks/usePWA.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use client';

import { useState, useEffect, useCallback } from 'react';
import { useState, useEffect, useCallback, useRef } from 'react';

interface BeforeInstallPromptEvent extends Event {
readonly platforms: string[];
Expand All @@ -17,6 +17,7 @@ export const usePWA = () => {
const [updateAvailable, setUpdateAvailable] = useState(false);
const [registration, setRegistration] = useState<ServiceWorkerRegistration | null>(null);
const [isOffline, setIsOffline] = useState(false);
const updateFoundHandlerRef = useRef<(() => void) | null>(null);

useEffect(() => {
// Check if app is already installed
Expand All @@ -41,15 +42,17 @@ export const usePWA = () => {
window.addEventListener('beforeinstallprompt', handleBeforeInstallPrompt);

// Track installation
window.addEventListener('appinstalled', () => {
const handleAppInstalled = () => {
setIsInstalled(true);
setInstallPrompt(null);
});
};
window.addEventListener('appinstalled', handleAppInstalled);

return () => {
window.removeEventListener('online', handleOnline);
window.removeEventListener('offline', handleOffline);
window.removeEventListener('beforeinstallprompt', handleBeforeInstallPrompt);
window.removeEventListener('appinstalled', handleAppInstalled);
};
}, []);

Expand All @@ -59,7 +62,7 @@ export const usePWA = () => {
const reg = await navigator.serviceWorker.register('/serviceWorker.js');
setRegistration(reg);

reg.addEventListener('updatefound', () => {
const handleUpdateFound = () => {
const newWorker = reg.installing;
if (newWorker) {
newWorker.addEventListener('statechange', () => {
Expand All @@ -68,13 +71,23 @@ export const usePWA = () => {
}
});
}
});
};
updateFoundHandlerRef.current = handleUpdateFound;
reg.addEventListener('updatefound', handleUpdateFound);
} catch (error) {
console.error('Service worker registration failed:', error);
}
}
}, []);

useEffect(() => {
return () => {
if (registration && updateFoundHandlerRef.current) {
registration.removeEventListener('updatefound', updateFoundHandlerRef.current);
}
};
}, [registration]);

const installApp = async () => {
if (!installPrompt) return;
await installPrompt.prompt();
Expand Down
8 changes: 7 additions & 1 deletion src/hooks/useSearch.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use client';

import { useState, useCallback, useRef } from 'react';
import { useState, useCallback, useRef, useEffect } from 'react';

export interface SearchResult {
id: string;
Expand Down Expand Up @@ -75,5 +75,11 @@ export function useSearch<T extends SearchResult>(
setError(null);
}, []);

useEffect(() => {
return () => {
if (debounceTimer.current) clearTimeout(debounceTimer.current);
};
}, []);

return { query, updateQuery, results, isLoading, error, hasMore, loadMore, reset };
}
Loading