fix: prevent infinite loop in git-diff screen

Added hasRequestedRef to track if a git diff request was already made
for the current agentId. This prevents duplicate requests when the
requestGitDiff function reference changes due to store updates.

The issue was that requestGitDiff in the useEffect dependency array
caused an infinite loop: request → store update → new function ref →
effect re-runs → request again.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Mohamed Boudra
2025-12-21 17:08:05 +00:00
co-authored by Claude Opus 4.5
parent 6bae6f3a89
commit 5d5a196525
2 changed files with 10 additions and 2 deletions
+8 -1
View File
@@ -1,4 +1,4 @@
import { useEffect, useState } from "react";
import { useEffect, useRef, useState } from "react";
import { View, Text, ScrollView, ActivityIndicator } from "react-native";
import { useLocalSearchParams } from "expo-router";
import { StyleSheet } from "react-native-unistyles";
@@ -97,6 +97,7 @@ function GitDiffContent({
serverLabel: string;
}) {
const [isLoading, setIsLoading] = useState(true);
const hasRequestedRef = useRef<string | null>(null);
const agent = useSessionStore((state) =>
agentId && serverId ? state.sessions[serverId]?.agents?.get(agentId) : undefined
@@ -116,6 +117,12 @@ function GitDiffContent({
return;
}
// Prevent duplicate requests for the same agentId
if (hasRequestedRef.current === agentId) {
return;
}
hasRequestedRef.current = agentId;
setIsLoading(true);
requestGitDiff(agentId);
+2 -1
View File
@@ -222,13 +222,14 @@ Remove `requestGitDiff` from useEffect dependencies in `git-diff.tsx`. Use a ref
---
- [ ] **Fix**: Remove requestGitDiff from useEffect dependencies in git-diff.tsx.
- [x] **Fix**: Remove requestGitDiff from useEffect dependencies in git-diff.tsx.
- The `requestGitDiff` function reference changes on every store update
- Remove it from the dependency array to prevent infinite loop
- Use a ref to track if we've already requested the diff
- Keep `agentId` as the only dependency (or use empty deps with agentId check)
- Run typecheck after fix.
- **Done (2025-12-21 18:10)**: Added `hasRequestedRef` to track if a request was already made for the current agentId. The ref prevents duplicate requests when `requestGitDiff` reference changes due to store updates. Typecheck passes.
- [ ] **Test**: Verify git diff loads correctly after fix.