Skip to content

Commit f9832c0

Browse files
BrunoQuaresmaclaude
andcommitted
refactor(hooks): remove attemptCount from useWithRetry state and rename retryAt to nextRetryAt
- Remove attemptCount from RetryState interface as it's not needed externally - Rename retryAt to nextRetryAt for better clarity - Simplify all setState calls to only manage isLoading and nextRetryAt - Keep attempt tracking local to executeAttempt function for delay calculation 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 000f0e4 commit f9832c0

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

site/src/hooks/useWithRetry.ts

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@ const MULTIPLIER = 2;
88

99
interface UseWithRetryResult {
1010
call: () => Promise<void>;
11-
retryAt: Date | undefined;
11+
nextRetryAt: Date | undefined;
1212
isLoading: boolean;
1313
}
1414

1515
interface RetryState {
1616
isLoading: boolean;
17-
retryAt: Date | undefined;
18-
attemptCount: number;
17+
nextRetryAt: Date | undefined;
1918
}
2019

2120
/**
@@ -25,8 +24,7 @@ interface RetryState {
2524
export function useWithRetry(fn: () => Promise<void>): UseWithRetryResult {
2625
const [state, setState] = useState<RetryState>({
2726
isLoading: false,
28-
retryAt: undefined,
29-
attemptCount: 0,
27+
nextRetryAt: undefined,
3028
});
3129

3230
const timeoutRef = useRef<number | null>(null);
@@ -42,25 +40,30 @@ export function useWithRetry(fn: () => Promise<void>): UseWithRetryResult {
4240
clearTimeout();
4341

4442
const executeAttempt = async (attempt: number): Promise<void> => {
45-
setState((prev) => ({ ...prev, isLoading: true, attemptCount: attempt }));
43+
setState({
44+
isLoading: true,
45+
nextRetryAt: undefined,
46+
});
4647

4748
try {
4849
await fn();
49-
setState({ isLoading: false, retryAt: undefined, attemptCount: 0 });
50+
setState({ isLoading: false, nextRetryAt: undefined });
5051
} catch (error) {
5152
const delayMs = Math.min(
5253
DELAY_MS * MULTIPLIER ** attempt,
5354
MAX_DELAY_MS,
5455
);
5556

56-
setState((prev) => ({
57-
...prev,
57+
setState({
5858
isLoading: false,
59-
retryAt: new Date(Date.now() + delayMs),
60-
}));
59+
nextRetryAt: new Date(Date.now() + delayMs),
60+
});
6161

6262
timeoutRef.current = window.setTimeout(() => {
63-
setState((prev) => ({ ...prev, retryAt: undefined }));
63+
setState({
64+
isLoading: false,
65+
nextRetryAt: undefined,
66+
});
6467
executeAttempt(attempt + 1);
6568
}, delayMs);
6669
}
@@ -77,7 +80,7 @@ export function useWithRetry(fn: () => Promise<void>): UseWithRetryResult {
7780

7881
return {
7982
call,
80-
retryAt: state.retryAt,
83+
nextRetryAt: state.nextRetryAt,
8184
isLoading: state.isLoading,
8285
};
8386
}

0 commit comments

Comments
 (0)