Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

fix(chat): older conversations load as you scroll the chat sidebar (#17178)

Hazem Adel9989e50c8a8c
diff --git a/packages/web/src/app/routes/chat-with-ai/conversation-list.tsx b/packages/web/src/app/routes/chat-with-ai/conversation-list.tsx
index 2b0e440f486..a148972293e 100644
--- a/packages/web/src/app/routes/chat-with-ai/conversation-list.tsx
+++ b/packages/web/src/app/routes/chat-with-ai/conversation-list.tsx
@@ -1,5 +1,11 @@
-import { AgentConversation } from '@activepieces/shared';
-import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
+import { AgentConversation, SeekPage } from '@activepieces/shared';
+import {
+ InfiniteData,
+ QueryKey,
+ useInfiniteQuery,
+ useMutation,
+ useQueryClient,
+} from '@tanstack/react-query';
import { t } from 'i18next';
import {
ArrowUpRight,
@@ -51,14 +57,27 @@ export function ConversationList({
const listRef = useRef<HTMLDivElement>(null);
const {
- data: conversationsPage,
+ data: conversationPages,
isLoading: isLoadingConversations,
isSuccess: conversationsLoaded,
- } = useQuery({
+ hasNextPage,
+ isFetching,
+ isFetchNextPageError,
+ fetchNextPage,
+ } = useInfiniteQuery<
+ SeekPage<AgentConversation>,
+ Error,
+ InfiniteData<SeekPage<AgentConversation>>,
+ QueryKey,
+ string | undefined
+ >({
queryKey: ['chat-conversations', agentId ?? 'chat'],
- queryFn: () =>
+ initialPageParam: undefined,
+ getNextPageParam: (lastPage) => lastPage.next ?? undefined,
+ queryFn: ({ pageParam }) =>
chatApi.listConversations({
limit: 100,
+ cursor: pageParam,
...(agentId === undefined ? {} : { agentId }),
}),
});
@@ -78,7 +97,10 @@ export function ConversationList({
},
});
- const allConversations = conversationsPage?.data ?? [];
+ const allConversations = useMemo(
+ () => conversationPages?.pages.flatMap((page) => page.data) ?? [],
+ [conversationPages],
+ );
const { getIndicator, markRead } = useConversationIndicators({
conversations: allConversations,
@@ -102,9 +124,43 @@ export function ConversationList({
setShowBottomFade(el.scrollTop + el.clientHeight < el.scrollHeight - 5);
}, []);
+ const skippedLoadMoreRef = useRef(false);
+
+ const loadMoreIfNearEnd = useCallback(() => {
+ const el = listRef.current;
+ const nearListEnd =
+ !!el &&
+ el.scrollTop + el.clientHeight >= el.scrollHeight - LOAD_MORE_OFFSET_PX;
+ const wantsMore = nearListEnd && hasNextPage;
+ skippedLoadMoreRef.current = wantsMore && isFetching;
+ if (wantsMore && !isFetching) {
+ fetchNextPage().catch(() => undefined);
+ }
+ }, [hasNextPage, isFetching, fetchNextPage]);
+
+ const fillsShortSearchResults =
+ searchQuery.trim().length > 0 && !isFetchNextPageError;
+
useEffect(() => {
checkFades();
- }, [collapsed, conversations, checkFades]);
+ const retriesSkippedScroll =
+ skippedLoadMoreRef.current && !isFetchNextPageError;
+ if (fillsShortSearchResults || retriesSkippedScroll) {
+ loadMoreIfNearEnd();
+ }
+ }, [
+ collapsed,
+ conversations,
+ checkFades,
+ loadMoreIfNearEnd,
+ fillsShortSearchResults,
+ isFetchNextPageError,
+ ]);
+
+ const handleListScroll = useCallback(() => {
+ checkFades();
+ loadMoreIfNearEnd();
+ }, [checkFades, loadMoreIfNearEnd]);
const { today, yesterday, older } = useMemo(() => {
const todayStr = new Date().toDateString();
@@ -265,7 +321,7 @@ export function ConversationList({
)}
<div
ref={listRef}
- onScroll={checkFades}
+ onScroll={handleListScroll}
className="h-full overflow-y-auto px-2 pb-3"
>
{isLoadingConversations ? (
@@ -322,3 +378,5 @@ export function ConversationList({
</div>
);
}
+
+const LOAD_MORE_OFFSET_PX = 200;
diff --git a/packages/web/src/features/chat/lib/use-conversation-indicators.ts b/packages/web/src/features/chat/lib/use-conversation-indicators.ts
index 1d2a2633048..40d719fc8c9 100644
--- a/packages/web/src/features/chat/lib/use-conversation-indicators.ts
+++ b/packages/web/src/features/chat/lib/use-conversation-indicators.ts
@@ -30,14 +30,16 @@ export function useConversationIndicators({
const conversationsRef = useRef(conversations);
conversationsRef.current = conversations;
- const seededScopesRef = useRef(new Set<string>());
+ const firstLoadBaselineRef = useRef(new Map<string, string>());
useEffect(() => {
if (!loaded) return;
- const initialLoad = !seededScopesRef.current.has(scope);
- seededScopesRef.current.add(scope);
+ const baseline =
+ firstLoadBaselineRef.current.get(scope) ??
+ newestUpdated({ conversations });
+ firstLoadBaselineRef.current.set(scope, baseline);
setLastSeen((prev) => {
- const next = seedLastSeen({ prev, conversations, initialLoad });
+ const next = seedLastSeen({ prev, conversations, baseline });
if (next === prev) return prev;
writeLastSeen(next);
return next;
@@ -154,21 +156,32 @@ export function useConversationIndicators({
function seedLastSeen({
prev,
conversations,
- initialLoad,
+ baseline,
}: {
prev: Record<string, string>;
conversations: Pick<AgentConversation, 'id' | 'updated'>[];
- initialLoad: boolean;
+ baseline: string;
}): Record<string, string> {
const unseen = conversations.filter((conv) => prev[conv.id] === undefined);
if (unseen.length === 0) return prev;
const seeded = unseen.map((conv) => [
conv.id,
- initialLoad ? conv.updated : NEVER_SEEN,
+ conv.updated > baseline ? NEVER_SEEN : conv.updated,
]);
return { ...prev, ...Object.fromEntries(seeded) };
}
+function newestUpdated({
+ conversations,
+}: {
+ conversations: Pick<AgentConversation, 'updated'>[];
+}): string {
+ return conversations.reduce(
+ (newest, conv) => (conv.updated > newest ? conv.updated : newest),
+ NEVER_SEEN,
+ );
+}
+
function readLastSeen(): Record<string, string> {
try {
const raw = localStorage.getItem(LAST_SEEN_STORAGE_KEY);
diff --git a/packages/web/test/features/chat/lib/use-conversation-indicators.test.ts b/packages/web/test/features/chat/lib/use-conversation-indicators.test.ts
index ad54ff6a7ee..3fa1caf140f 100644
--- a/packages/web/test/features/chat/lib/use-conversation-indicators.test.ts
+++ b/packages/web/test/features/chat/lib/use-conversation-indicators.test.ts
@@ -11,27 +11,39 @@ import {
} from '@/features/chat/lib/use-conversation-indicators';
describe('seeding when each conversation was last seen', () => {
- it('treats conversations already there on the first load as seen', () => {
+ it('treats conversations up to the first load as seen', () => {
const seeded = conversationIndicatorUtils.seedLastSeen({
prev: {},
conversations: [{ id: 'a', updated: T1 }],
- initialLoad: true,
+ baseline: T1,
});
expect(T1 > seeded.a).toBe(false);
});
- it('shows a conversation that appears later, already finished, as unread', () => {
+ it('shows a conversation updated after the first load as unread', () => {
const seeded = conversationIndicatorUtils.seedLastSeen({
prev: { a: T1 },
conversations: [
{ id: 'a', updated: T1 },
- { id: 'b', updated: T1 },
+ { id: 'b', updated: T2 },
],
- initialLoad: false,
+ baseline: T1,
});
- expect(T1 > seeded.b).toBe(true);
+ expect(T2 > seeded.b).toBe(true);
expect(seeded.a).toBe(T1);
});
+
+ it('treats an older page loaded later as already seen', () => {
+ const seeded = conversationIndicatorUtils.seedLastSeen({
+ prev: { b: T2 },
+ conversations: [
+ { id: 'b', updated: T2 },
+ { id: 'a', updated: T1 },
+ ],
+ baseline: T2,
+ });
+ expect(T1 > seeded.a).toBe(false);
+ });
});
describe('unread dots across loads', () => {
@@ -60,6 +72,20 @@ describe('unread dots across loads', () => {
expect(result.current.getIndicator(existing)).toBeNull();
});
+ it('keeps chats from an older page read when it is appended', () => {
+ const { result, rerender } = renderIndicators({
+ conversations: [conversation({ id: 'b', updated: T2 })],
+ scope: 'chat',
+ });
+ const olderChat = conversation({ id: 'a', updated: T1 });
+ rerender({
+ conversations: [conversation({ id: 'b', updated: T2 }), olderChat],
+ scope: 'chat',
+ });
+
+ expect(result.current.getIndicator(olderChat)).toBeNull();
+ });
+
it('does not mark existing chats unread when switching to another agent', () => {
const { result, rerender } = renderIndicators({
conversations: [conversation({ id: 'a' })],
@@ -92,11 +118,17 @@ function renderIndicators(initialProps: {
);
}
-function conversation({ id }: { id: string }): AgentConversation {
+function conversation({
+ id,
+ updated = T1,
+}: {
+ id: string;
+ updated?: string;
+}): AgentConversation {
return AgentConversation.parse({
id,
created: T1,
- updated: T1,
+ updated,
platformId: 'platform-1',
userId: 'user-1',
source: AgentRunSource.CHAT,
@@ -104,3 +136,4 @@ function conversation({ id }: { id: string }): AgentConversation {
}
const T1 = '2026-11-03T10:00:00.000Z';
+const T2 = '2026-11-03T11:00:00.000Z';