Moved later/gitgram-ai/gitgram/ to apps/ritgram/ (fresh history; full history stays in caelum0x/gitgram). AITimeMachine stays separate in later/gitgram-ai per the Rithub master plan. Logs, pid files and env files are ignored; GPL-3.0 mobile/proximity noted in THIRD_PARTY_NOTICES. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
43 lines
1.0 KiB
TypeScript
43 lines
1.0 KiB
TypeScript
import { useState, useCallback } from 'react';
|
|
|
|
interface UseRetryOptions {
|
|
maxRetries?: number;
|
|
retryDelay?: number;
|
|
onRetry?: (attempt: number) => void;
|
|
}
|
|
|
|
export function useRetry<T>(
|
|
fn: () => Promise<T>,
|
|
options: UseRetryOptions = {}
|
|
) {
|
|
const { maxRetries = 3, retryDelay = 1000, onRetry } = options;
|
|
const [attempt, setAttempt] = useState(0);
|
|
const [loading, setLoading] = useState(false);
|
|
|
|
const execute = useCallback(async (): Promise<T> => {
|
|
setLoading(true);
|
|
let lastError: any;
|
|
|
|
for (let i = 0; i <= maxRetries; i++) {
|
|
setAttempt(i);
|
|
try {
|
|
const result = await fn();
|
|
setLoading(false);
|
|
return result;
|
|
} catch (error) {
|
|
lastError = error;
|
|
if (i < maxRetries) {
|
|
onRetry?.(i + 1);
|
|
await new Promise((resolve) => setTimeout(resolve, retryDelay * (i + 1)));
|
|
}
|
|
}
|
|
}
|
|
|
|
setLoading(false);
|
|
throw lastError;
|
|
}, [fn, maxRetries, retryDelay, onRetry]);
|
|
|
|
return { execute, loading, attempt };
|
|
}
|
|
|