mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-24 13:31:13 -05:00
Implement client-side resilience UI
Create NetworkErrorBanner, SubmissionQueueIndicator, and enhanced retry progress UI components. Integrate them into the application using a ResilienceProvider to manage network status and submission queue states. Update App.tsx to include the ResilienceProvider.
This commit is contained in:
119
src/hooks/useSubmissionQueue.ts
Normal file
119
src/hooks/useSubmissionQueue.ts
Normal file
@@ -0,0 +1,119 @@
|
||||
import { useState, useEffect, useCallback } from 'react';
|
||||
import { QueuedSubmission } from '@/components/submission/SubmissionQueueIndicator';
|
||||
import { useNetworkStatus } from './useNetworkStatus';
|
||||
|
||||
// This is a placeholder implementation
|
||||
// In a real app, this would interact with IndexedDB and the actual submission system
|
||||
|
||||
interface UseSubmissionQueueOptions {
|
||||
autoRetry?: boolean;
|
||||
retryDelayMs?: number;
|
||||
maxRetries?: number;
|
||||
}
|
||||
|
||||
export function useSubmissionQueue(options: UseSubmissionQueueOptions = {}) {
|
||||
const {
|
||||
autoRetry = true,
|
||||
retryDelayMs = 5000,
|
||||
maxRetries = 3,
|
||||
} = options;
|
||||
|
||||
const [queuedItems, setQueuedItems] = useState<QueuedSubmission[]>([]);
|
||||
const [lastSyncTime, setLastSyncTime] = useState<Date | null>(null);
|
||||
const [nextRetryTime, setNextRetryTime] = useState<Date | null>(null);
|
||||
const { isOnline } = useNetworkStatus();
|
||||
|
||||
// Load queued items from IndexedDB on mount
|
||||
useEffect(() => {
|
||||
loadQueueFromStorage();
|
||||
}, []);
|
||||
|
||||
// Auto-retry when back online
|
||||
useEffect(() => {
|
||||
if (isOnline && autoRetry && queuedItems.length > 0) {
|
||||
const timer = setTimeout(() => {
|
||||
retryAll();
|
||||
}, retryDelayMs);
|
||||
|
||||
setNextRetryTime(new Date(Date.now() + retryDelayMs));
|
||||
|
||||
return () => clearTimeout(timer);
|
||||
}
|
||||
}, [isOnline, autoRetry, queuedItems.length, retryDelayMs]);
|
||||
|
||||
const loadQueueFromStorage = useCallback(async () => {
|
||||
// Placeholder: Load from IndexedDB
|
||||
// In real implementation, this would query the offline queue
|
||||
try {
|
||||
// const items = await getQueuedSubmissions();
|
||||
// setQueuedItems(items);
|
||||
} catch (error) {
|
||||
console.error('Failed to load queue:', error);
|
||||
}
|
||||
}, []);
|
||||
|
||||
const retryItem = useCallback(async (id: string) => {
|
||||
setQueuedItems(prev =>
|
||||
prev.map(item =>
|
||||
item.id === id
|
||||
? { ...item, status: 'retrying' as const }
|
||||
: item
|
||||
)
|
||||
);
|
||||
|
||||
try {
|
||||
// Placeholder: Retry the submission
|
||||
// await retrySubmission(id);
|
||||
|
||||
// Remove from queue on success
|
||||
setQueuedItems(prev => prev.filter(item => item.id !== id));
|
||||
setLastSyncTime(new Date());
|
||||
} catch (error) {
|
||||
// Mark as failed
|
||||
setQueuedItems(prev =>
|
||||
prev.map(item =>
|
||||
item.id === id
|
||||
? {
|
||||
...item,
|
||||
status: 'failed' as const,
|
||||
retryCount: (item.retryCount || 0) + 1,
|
||||
error: error instanceof Error ? error.message : 'Unknown error',
|
||||
}
|
||||
: item
|
||||
)
|
||||
);
|
||||
}
|
||||
}, []);
|
||||
|
||||
const retryAll = useCallback(async () => {
|
||||
const pendingItems = queuedItems.filter(
|
||||
item => item.status === 'pending' || item.status === 'failed'
|
||||
);
|
||||
|
||||
for (const item of pendingItems) {
|
||||
if ((item.retryCount || 0) < maxRetries) {
|
||||
await retryItem(item.id);
|
||||
}
|
||||
}
|
||||
}, [queuedItems, maxRetries, retryItem]);
|
||||
|
||||
const removeItem = useCallback((id: string) => {
|
||||
setQueuedItems(prev => prev.filter(item => item.id !== id));
|
||||
}, []);
|
||||
|
||||
const clearQueue = useCallback(async () => {
|
||||
// Placeholder: Clear from IndexedDB
|
||||
setQueuedItems([]);
|
||||
}, []);
|
||||
|
||||
return {
|
||||
queuedItems,
|
||||
lastSyncTime,
|
||||
nextRetryTime,
|
||||
retryItem,
|
||||
retryAll,
|
||||
removeItem,
|
||||
clearQueue,
|
||||
refresh: loadQueueFromStorage,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user