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
TypeScript ai-agentai-agent-toolsai-agentsai-agents-frameworkmcpmcp-servermcp-toolsmcpsn8n-alternativeno-code-automationworkflowworkflow-automationworkflows
fix(chat): a finished chat from another tab shows as unread (#17172)
Hazem Adelf32d99afdf12
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.tsxindex ac83a227f18..2b0e440f486 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@@ -50,15 +50,18 @@ export function ConversationList({const [settingsOpen, setSettingsOpen] = useState(false);const listRef = useRef<HTMLDivElement>(null);- const { data: conversationsPage, isLoading: isLoadingConversations } =- useQuery({- queryKey: ['chat-conversations', agentId ?? 'chat'],- queryFn: () =>- chatApi.listConversations({- limit: 100,- ...(agentId === undefined ? {} : { agentId }),- }),- });+ const {+ data: conversationsPage,+ isLoading: isLoadingConversations,+ isSuccess: conversationsLoaded,+ } = useQuery({+ queryKey: ['chat-conversations', agentId ?? 'chat'],+ queryFn: () =>+ chatApi.listConversations({+ limit: 100,+ ...(agentId === undefined ? {} : { agentId }),+ }),+ });const selectedIdRef = useRef(selectedId);selectedIdRef.current = selectedId;@@ -80,6 +83,8 @@ export function ConversationList({const { getIndicator, markRead } = useConversationIndicators({conversations: allConversations,activeId: selectedId ?? null,+ scope: agentId ?? 'chat',+ loaded: conversationsLoaded,});const conversations = useMemo(() => {diff --git a/packages/web/src/features/chat/lib/use-conversation-indicators.ts b/packages/web/src/features/chat/lib/use-conversation-indicators.tsindex ea014520612..1d2a2633048 100644--- a/packages/web/src/features/chat/lib/use-conversation-indicators.ts+++ b/packages/web/src/features/chat/lib/use-conversation-indicators.ts@@ -14,9 +14,13 @@ import { chatApi } from './chat-api';export function useConversationIndicators({conversations,activeId,+ scope,+ loaded,}: {conversations: AgentConversation[];activeId: string | null;+ scope: string;+ loaded: boolean;}) {const socket = useSocket();const queryClient = useQueryClient();@@ -26,23 +30,19 @@ export function useConversationIndicators({const conversationsRef = useRef(conversations);conversationsRef.current = conversations;+ const seededScopesRef = useRef(new Set<string>());useEffect(() => {- if (conversations.length === 0) return;+ if (!loaded) return;+ const initialLoad = !seededScopesRef.current.has(scope);+ seededScopesRef.current.add(scope);setLastSeen((prev) => {- let changed = false;- const next = { ...prev };- for (const conv of conversations) {- if (next[conv.id] === undefined) {- next[conv.id] = conv.updated;- changed = true;- }- }- if (!changed) return prev;+ const next = seedLastSeen({ prev, conversations, initialLoad });+ if (next === prev) return prev;writeLastSeen(next);return next;});- }, [conversations]);+ }, [conversations, loaded, scope]);const markRead = useCallback((id: string) => {const conv = conversationsRef.current.find((c) => c.id === id);@@ -151,6 +151,24 @@ export function useConversationIndicators({return { getIndicator, markRead };}+function seedLastSeen({+ prev,+ conversations,+ initialLoad,+}: {+ prev: Record<string, string>;+ conversations: Pick<AgentConversation, 'id' | 'updated'>[];+ initialLoad: boolean;+}): 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,+ ]);+ return { ...prev, ...Object.fromEntries(seeded) };+}+function readLastSeen(): Record<string, string> {try {const raw = localStorage.getItem(LAST_SEEN_STORAGE_KEY);@@ -176,6 +194,7 @@ function writeLastSeen(value: Record<string, string>): void {}const LAST_SEEN_STORAGE_KEY = 'chat-conversation-last-seen';+const NEVER_SEEN = '';const GATE_REFETCH_INTERVAL_MS = 3000;type SocketEvent = {@@ -186,3 +205,5 @@ type SocketEvent = {};export type ConversationIndicatorState = 'working' | 'waiting' | 'unread';++export const conversationIndicatorUtils = { seedLastSeen };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.tsnew file mode 100644index 00000000000..ad54ff6a7ee--- /dev/null+++ b/packages/web/test/features/chat/lib/use-conversation-indicators.test.ts@@ -0,0 +1,106 @@+// @vitest-environment jsdom+import { AgentConversation, AgentRunSource } from '@activepieces/shared';+import { QueryClient, QueryClientProvider } from '@tanstack/react-query';+import { renderHook } from '@testing-library/react';+import { createElement, ReactNode } from 'react';+import { afterEach, describe, expect, it } from 'vitest';++import {+ conversationIndicatorUtils,+ useConversationIndicators,+} 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', () => {+ const seeded = conversationIndicatorUtils.seedLastSeen({+ prev: {},+ conversations: [{ id: 'a', updated: T1 }],+ initialLoad: true,+ });+ expect(T1 > seeded.a).toBe(false);+ });++ it('shows a conversation that appears later, already finished, as unread', () => {+ const seeded = conversationIndicatorUtils.seedLastSeen({+ prev: { a: T1 },+ conversations: [+ { id: 'a', updated: T1 },+ { id: 'b', updated: T1 },+ ],+ initialLoad: false,+ });+ expect(T1 > seeded.b).toBe(true);+ expect(seeded.a).toBe(T1);+ });+});++describe('unread dots across loads', () => {+ afterEach(() => localStorage.clear());++ it('shows a finished chat as unread when it arrives after an empty first load', () => {+ const { result, rerender } = renderIndicators({+ conversations: [],+ scope: 'chat',+ });+ const arrived = conversation({ id: 'b' });+ rerender({ conversations: [arrived], scope: 'chat' });++ expect(result.current.getIndicator(arrived)).toBe('unread');+ });++ it('treats chats from the first successful load as seen after a failed load', () => {+ const { result, rerender } = renderIndicators({+ conversations: [],+ scope: 'chat',+ loaded: false,+ });+ const existing = conversation({ id: 'a' });+ rerender({ conversations: [existing], scope: 'chat', loaded: true });++ expect(result.current.getIndicator(existing)).toBeNull();+ });++ it('does not mark existing chats unread when switching to another agent', () => {+ const { result, rerender } = renderIndicators({+ conversations: [conversation({ id: 'a' })],+ scope: 'agent-1',+ });+ const otherAgentChat = conversation({ id: 'b' });+ rerender({ conversations: [otherAgentChat], scope: 'agent-2' });++ expect(result.current.getIndicator(otherAgentChat)).toBeNull();+ });+});++function renderIndicators(initialProps: {+ conversations: AgentConversation[];+ scope: string;+ loaded?: boolean;+}) {+ const queryClient = new QueryClient();+ const wrapper = ({ children }: { children: ReactNode }) =>+ createElement(QueryClientProvider, { client: queryClient }, children);+ return renderHook(+ ({ conversations, scope, loaded = true }) =>+ useConversationIndicators({+ conversations,+ activeId: null,+ scope,+ loaded,+ }),+ { initialProps, wrapper },+ );+}++function conversation({ id }: { id: string }): AgentConversation {+ return AgentConversation.parse({+ id,+ created: T1,+ updated: T1,+ platformId: 'platform-1',+ userId: 'user-1',+ source: AgentRunSource.CHAT,+ });+}++const T1 = '2026-11-03T10:00:00.000Z';