fix: refetch messages and git status when switching between tasks (#110)
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { useEffect, useCallback } from 'react';
|
||||
import { useEffect, useRef } from 'react';
|
||||
import { useAppStore } from '@/components/state-provider';
|
||||
import { getWebSocketClient } from '@/lib/ws/connection';
|
||||
import type { GitSnapshot } from '@/lib/state/slices/session-runtime/types';
|
||||
@@ -14,70 +14,80 @@ export function useSessionGitStatus(sessionId: string | null) {
|
||||
sessionId ? state.gitStatus.bySessionId[sessionId] : undefined
|
||||
);
|
||||
const setGitStatus = useAppStore((state) => state.setGitStatus);
|
||||
|
||||
// Fetch latest git snapshot to populate initial status
|
||||
const fetchLatestSnapshot = useCallback(async () => {
|
||||
if (!sessionId) return;
|
||||
|
||||
const client = getWebSocketClient();
|
||||
if (!client) return;
|
||||
|
||||
try {
|
||||
const response = await client.request<{ snapshots?: GitSnapshot[] }>(
|
||||
'session.git.snapshots',
|
||||
{
|
||||
session_id: sessionId,
|
||||
limit: 1, // Only fetch the latest snapshot
|
||||
}
|
||||
);
|
||||
|
||||
if (response?.snapshots && response.snapshots.length > 0) {
|
||||
const latest = response.snapshots[0];
|
||||
|
||||
// Extract file paths by status from the files object
|
||||
const modified: string[] = [];
|
||||
const added: string[] = [];
|
||||
const deleted: string[] = [];
|
||||
const untracked: string[] = [];
|
||||
const renamed: string[] = [];
|
||||
|
||||
if (latest.files) {
|
||||
Object.entries(latest.files).forEach(([path, fileInfo]) => {
|
||||
const status = fileInfo.status?.toLowerCase();
|
||||
if (status === 'modified') modified.push(path);
|
||||
else if (status === 'added') added.push(path);
|
||||
else if (status === 'deleted') deleted.push(path);
|
||||
else if (status === 'untracked') untracked.push(path);
|
||||
else if (status === 'renamed') renamed.push(path);
|
||||
});
|
||||
}
|
||||
|
||||
// Populate git status from latest snapshot
|
||||
setGitStatus(sessionId, {
|
||||
branch: latest.branch,
|
||||
remote_branch: latest.remote_branch,
|
||||
modified,
|
||||
added,
|
||||
deleted,
|
||||
untracked,
|
||||
renamed,
|
||||
ahead: latest.ahead,
|
||||
behind: latest.behind,
|
||||
files: latest.files,
|
||||
timestamp: latest.created_at,
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Failed to fetch latest git snapshot:', error);
|
||||
}
|
||||
}, [sessionId, setGitStatus]);
|
||||
const prevSessionIdRef = useRef<string | null>(null);
|
||||
|
||||
// Fetch initial status on mount if not already loaded
|
||||
// Also refetch when switching to a different session
|
||||
useEffect(() => {
|
||||
if (sessionId && !gitStatus) {
|
||||
fetchLatestSnapshot();
|
||||
if (!sessionId) return;
|
||||
|
||||
// Detect session change to force refetch
|
||||
const sessionChanged = prevSessionIdRef.current !== null &&
|
||||
prevSessionIdRef.current !== sessionId;
|
||||
prevSessionIdRef.current = sessionId;
|
||||
|
||||
// Skip fetch if status exists and session hasn't changed
|
||||
if (gitStatus && !sessionChanged) {
|
||||
return;
|
||||
}
|
||||
}, [sessionId, gitStatus, fetchLatestSnapshot]);
|
||||
|
||||
// Fetch latest git snapshot
|
||||
const fetchSnapshot = async () => {
|
||||
const client = getWebSocketClient();
|
||||
if (!client) return;
|
||||
|
||||
try {
|
||||
const response = await client.request<{ snapshots?: GitSnapshot[] }>(
|
||||
'session.git.snapshots',
|
||||
{
|
||||
session_id: sessionId,
|
||||
limit: 1, // Only fetch the latest snapshot
|
||||
}
|
||||
);
|
||||
|
||||
if (response?.snapshots && response.snapshots.length > 0) {
|
||||
const latest = response.snapshots[0];
|
||||
|
||||
// Extract file paths by status from the files object
|
||||
const modified: string[] = [];
|
||||
const added: string[] = [];
|
||||
const deleted: string[] = [];
|
||||
const untracked: string[] = [];
|
||||
const renamed: string[] = [];
|
||||
|
||||
if (latest.files) {
|
||||
Object.entries(latest.files).forEach(([path, fileInfo]) => {
|
||||
const status = fileInfo.status?.toLowerCase();
|
||||
if (status === 'modified') modified.push(path);
|
||||
else if (status === 'added') added.push(path);
|
||||
else if (status === 'deleted') deleted.push(path);
|
||||
else if (status === 'untracked') untracked.push(path);
|
||||
else if (status === 'renamed') renamed.push(path);
|
||||
});
|
||||
}
|
||||
|
||||
// Populate git status from latest snapshot
|
||||
setGitStatus(sessionId, {
|
||||
branch: latest.branch,
|
||||
remote_branch: latest.remote_branch,
|
||||
modified,
|
||||
added,
|
||||
deleted,
|
||||
untracked,
|
||||
renamed,
|
||||
ahead: latest.ahead,
|
||||
behind: latest.behind,
|
||||
files: latest.files,
|
||||
timestamp: latest.created_at,
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Failed to fetch latest git snapshot:', error);
|
||||
}
|
||||
};
|
||||
|
||||
fetchSnapshot();
|
||||
}, [sessionId, gitStatus, setGitStatus]);
|
||||
|
||||
// Subscribe to session updates to receive git status via WebSocket
|
||||
useEffect(() => {
|
||||
|
||||
@@ -34,6 +34,7 @@ export function useSessionMessages(
|
||||
const initialFetchStartRef = useRef<number | null>(null);
|
||||
const lastFetchedSessionIdRef = useRef<string | null>(null);
|
||||
const lastFetchStateKeyRef = useRef<string | null>(null);
|
||||
const prevSessionIdRef = useRef<string | null>(null);
|
||||
const hasAgentMessage = messages.some((message: Message) => message.author_type === 'agent');
|
||||
|
||||
useEffect(() => {
|
||||
@@ -64,8 +65,21 @@ export function useSessionMessages(
|
||||
return;
|
||||
}
|
||||
|
||||
// Detect session change to force refetch
|
||||
const sessionChanged = prevSessionIdRef.current !== null &&
|
||||
prevSessionIdRef.current !== taskSessionId;
|
||||
|
||||
// Update previous session ref
|
||||
prevSessionIdRef.current = taskSessionId;
|
||||
|
||||
// If session changed, reset fetch guard to force refetch
|
||||
if (sessionChanged) {
|
||||
lastFetchedSessionIdRef.current = null;
|
||||
}
|
||||
|
||||
// Check if messages are already loaded (from SSR or previous fetch)
|
||||
if (messages.length > 0) {
|
||||
// BUT: if session just changed, we want to refetch anyway
|
||||
if (messages.length > 0 && !sessionChanged) {
|
||||
lastFetchedSessionIdRef.current = taskSessionId;
|
||||
setIsWaitingForInitialMessages(false);
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user