Skip to content

Commit 460aa53

Browse files
committed
refactor: make log web sockets logic more clear
1 parent 07ff536 commit 460aa53

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

site/src/modules/resources/AgentLogs/useAgentLogs.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,18 @@ export function useAgentLogs(
3131
const queryOptions = agentLogs(workspaceId, agentId);
3232
const { data: logs, isFetched } = useQuery({ ...queryOptions, enabled });
3333

34+
// Track the ID of the last log received when the initial logs response comes
35+
// back. If the logs are not complete, the ID will mark the start point of the
36+
// Web sockets response so that the remaining logs can be received over time
3437
const lastQueriedLogId = useRef(0);
3538
useEffect(() => {
36-
const lastLog = logs?.at(-1);
37-
const canSetLogId = lastLog !== undefined && lastQueriedLogId.current === 0;
39+
const isAlreadyTracking = lastQueriedLogId.current !== 0;
40+
if (isAlreadyTracking) {
41+
return;
42+
}
3843

39-
if (canSetLogId) {
44+
const lastLog = logs?.at(-1);
45+
if (lastLog !== undefined) {
4046
lastQueriedLogId.current = lastLog.id;
4147
}
4248
}, [logs]);

0 commit comments

Comments
 (0)