The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
Stay on an inbox chat that leaves the filtered list by coming back to the inbox (#27571)
Félix Malfait0687b80dcb7b
diff --git a/packages/twenty-front/src/modules/ai/components/AiChatInboxSelectionEffect.tsx b/packages/twenty-front/src/modules/ai/components/AiChatInboxSelectionEffect.tsxindex 31d658085ce..611167f8e36 100644--- a/packages/twenty-front/src/modules/ai/components/AiChatInboxSelectionEffect.tsx+++ b/packages/twenty-front/src/modules/ai/components/AiChatInboxSelectionEffect.tsx@@ -2,7 +2,11 @@ import { useEffect, useState } from 'react';import { AppPath } from 'twenty-shared/types';import { isDefined } from 'twenty-shared/utils';+import { agentChatRecentThreadsSelector } from '@/ai/states/selectors/agentChatRecentThreadsSelector';+import { agentChatThreadInboxStatusFamilySelector } from '@/ai/states/selectors/agentChatThreadInboxStatusFamilySelector';import { type AgentChatThreadRecord } from '@/ai/types/AgentChatThreadRecord';+import { useAtomFamilySelectorValue } from '@/ui/utilities/state/jotai/hooks/useAtomFamilySelectorValue';+import { useAtomStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomStateValue';import { useNavigateApp } from '~/hooks/useNavigateApp';type AiChatInboxSelectionEffectProps = {@@ -19,6 +23,16 @@ export const AiChatInboxSelectionEffect = ({threads,}: AiChatInboxSelectionEffectProps) => {const navigate = useNavigateApp();+ const agentChatRecentThreads = useAtomStateValue(+ agentChatRecentThreadsSelector,+ );+ const { scope: selectedThreadScope } = useAtomFamilySelectorValue(+ agentChatThreadInboxStatusFamilySelector,+ selectedThreadId ?? '',+ );+ const isSelectedThreadInInbox =+ selectedThreadScope === 'INBOX' &&+ agentChatRecentThreads.some(({ id }) => id === selectedThreadId);const [lastListedSelection, setLastListedSelection] = useState<{threadId: string;index: number;@@ -42,8 +56,13 @@ export const AiChatInboxSelectionEffect = ({return;}- // A chat opened from a link without being listed stays open- if (lastListedSelection?.threadId !== selectedThreadId) {+ // A chat opened from a link without being listed stays open, and so does+ // one that left the list by coming back to the inbox, as a done or+ // snoozed chat does when the member writes in it+ if (+ lastListedSelection?.threadId !== selectedThreadId ||+ isSelectedThreadInInbox+ ) {return;}@@ -57,7 +76,13 @@ export const AiChatInboxSelectionEffect = ({undefined,{ replace: true },);- }, [lastListedSelection, navigate, selectedThreadId, threads]);+ }, [+ isSelectedThreadInInbox,+ lastListedSelection,+ navigate,+ selectedThreadId,+ threads,+ ]);return null;};diff --git a/packages/twenty-front/src/pages/ai-chat/__tests__/AiChatInboxPage.test.tsx b/packages/twenty-front/src/pages/ai-chat/__tests__/AiChatInboxPage.test.tsxindex 1bc4dd9816c..f7a9f00615a 100644--- a/packages/twenty-front/src/pages/ai-chat/__tests__/AiChatInboxPage.test.tsx+++ b/packages/twenty-front/src/pages/ai-chat/__tests__/AiChatInboxPage.test.tsx@@ -16,9 +16,15 @@ import { contextStoreTargetedRecordsRuleComponentState } from '@/context-store/simport { ContextStoreComponentInstanceContext } from '@/context-store/states/contexts/ContextStoreComponentInstanceContext';import { AI_CHAT_INBOX_INSTANCE_ID } from '@/ai/constants/AiChatInboxInstanceId';import { AI_CHAT_INBOX_LAYOUT } from '@/ai/constants/AiChatInboxLayout';+import { AGENT_CHAT_THREAD_FILTER_STATUS } from '@/ai/constants/AgentChatThreadFilterStatus';+import { useOptimisticallyRestoreOnSend } from '@/ai/hooks/useOptimisticallyRestoreOnSend';+import { agentChatThreadFilterStatusState } from '@/ai/states/agentChatThreadFilterStatusState';+import { agentChatThreadListState } from '@/ai/states/agentChatThreadListState';+import { agentChatThreadParticipantsState } from '@/ai/states/agentChatThreadParticipantsState';import { aiChatInboxLayoutState } from '@/ai/states/aiChatInboxLayoutState';import { type AgentChatThreadRecord } from '@/ai/types/AgentChatThreadRecord';import { type EnrichedObjectMetadataItem } from '@/object-metadata/types/EnrichedObjectMetadataItem';+import { recordStoreFamilyState } from '@/object-record/record-store/states/recordStoreFamilyState';import { isDropdownOpenComponentState } from '@/ui/layout/dropdown/states/isDropdownOpenComponentState';import { useAvailableComponentInstanceIdOrThrow } from '@/ui/utilities/state/component-state/hooks/useAvailableComponentInstanceIdOrThrow';import {@@ -56,10 +62,6 @@ jest.mock('@/object-metadata/states/objectMetadataItemFamilySelector', () => ({},}));-jest.mock('@/ai/hooks/useChatThreads', () => ({- useChatThreads: () => ({ threads: THREADS, loading: false }),-}));-jest.mock('@/ai/components/AiChatThreadList', () => ({AiChatThreadList: ({threads,@@ -122,6 +124,7 @@ jest.mock('~/pages/ai-chat/AiChatPageEffects', () => ({AiChatPageEffects: () => null,}));+// Sending stands in for the composer with what a send applies right awayjest.mock('~/pages/ai-chat/AiChatThreadPageContent', () => ({AiChatThreadPageContent: ({threadId,@@ -131,13 +134,27 @@ jest.mock('~/pages/ai-chat/AiChatThreadPageContent', () => ({threadId: string;headerTitlePrefix?: ReactNode;headerActions?: ReactNode;- }) => (- <div>- {headerTitlePrefix}- Chat page {threadId}- {headerActions}- </div>- ),+ }) => {+ const { applyOptimisticRestore } = useOptimisticallyRestoreOnSend();++ return (+ <div>+ {headerTitlePrefix}+ Chat page {threadId}+ {headerActions}+ <button+ onClick={() =>+ applyOptimisticRestore({+ threadId,+ optimisticUpdatedAt: '2026-09-09T00:00:00.000Z',+ })+ }+ >+ Send+ </button>+ </div>+ );+ },}));let isMobile = false;@@ -211,7 +228,17 @@ const getTargetedThreadIds = (contextStoreInstanceId: string) =>describe('AiChatInboxPage', () => {beforeEach(() => {+ // The list filter is kept in local storage+ localStorage.clear();resetJotaiStore();+ THREADS.forEach((thread) =>+ jotaiStore.set(recordStoreFamilyState.atomFamily(thread.id), thread),+ );+ jotaiStore.set(agentChatThreadListState.atom, {+ threadIds: THREADS.map(({ id }) => id),+ hasNextPage: false,+ endCursor: null,+ });isMobile = false;mockIsAiChatInboxEnabled = true;});@@ -243,6 +270,44 @@ describe('AiChatInboxPage', () => {).toBeInTheDocument();});+ it('stays on a done chat the member writes in, though it leaves the done list', () => {+ jotaiStore.set(+ agentChatThreadFilterStatusState.atom,+ AGENT_CHAT_THREAD_FILTER_STATUS.DONE,+ );+ jotaiStore.set(+ agentChatThreadParticipantsState.atom,+ Object.fromEntries(+ THREADS.map(({ id }) => [+ id,+ {+ id: `participant-${id}`,+ threadId: id,+ lastReadAt: '2026-09-07T00:00:00.000Z',+ archivedAt: '2026-09-08T00:00:00.000Z',+ snoozedUntil: null,+ isSubscribed: true,+ lastMentionedAt: null,+ updatedAt: '2026-09-08T00:00:00.000Z',+ },+ ]),+ ),+ );++ renderInbox(`/inbox/${secondThread.id}`);++ expect(isRowOpen('Second chat')).toBe(true);++ fireEvent.click(screen.getByRole('button', { name: 'Send' }));++ expect(+ screen.queryByRole('button', { name: 'Second chat' }),+ ).not.toBeInTheDocument();+ expect(+ screen.getByText(`Chat page ${secondThread.id}`),+ ).toBeInTheDocument();+ });+it('adds a chat to the one on screen with cmd+click', () => {renderInbox();