Skip to content

The open alternative to Salesforce, designed for AI.

Open the first inbox chat with a redirect instead of an effect (#27569)

Félix Malfait3cffd19b1cd4
diff --git a/packages/twenty-front/src/modules/ai/components/AiChatInboxSelectionEffect.tsx b/packages/twenty-front/src/modules/ai/components/AiChatInboxSelectionEffect.tsx
index c90a61a9913..31d658085ce 100644
--- a/packages/twenty-front/src/modules/ai/components/AiChatInboxSelectionEffect.tsx
+++ b/packages/twenty-front/src/modules/ai/components/AiChatInboxSelectionEffect.tsx
@@ -8,16 +8,15 @@ import { useNavigateApp } from '~/hooks/useNavigateApp';
type AiChatInboxSelectionEffectProps = {
selectedThreadId: string | undefined;
threads: Pick<AgentChatThreadRecord, 'id'>[];
- // Split view always has a chat beside the list; otherwise the list stands alone
- shouldSelectFirstThread: boolean;
};
// A chat leaves the list when it is done, snoozed or deleted; the one that
-// takes its place is selected, the way a mail inbox moves on
+// takes its place is selected, the way a mail inbox moves on. The list is
+// watched because generic record deletes, other tabs and reloads change it
+// before any chat code hears of it
export const AiChatInboxSelectionEffect = ({
selectedThreadId,
threads,
- shouldSelectFirstThread,
}: AiChatInboxSelectionEffectProps) => {
const navigate = useNavigateApp();
const [lastListedSelection, setLastListedSelection] = useState<{
@@ -26,17 +25,7 @@ export const AiChatInboxSelectionEffect = ({
} | null>(null);
useEffect(() => {
- const selectThread = (threadId: string | null) =>
- // oxlint-disable-next-line twenty/no-navigate-prefer-link
- navigate(AppPath.AiChatInbox, { threadId }, undefined, {
- replace: true,
- });
-
if (!isDefined(selectedThreadId)) {
- if (shouldSelectFirstThread && threads.length > 0) {
- selectThread(threads[0].id);
- }
-
return;
}
@@ -61,14 +50,14 @@ export const AiChatInboxSelectionEffect = ({
const nextThread =
threads[Math.min(lastListedSelection.index, threads.length - 1)];
- selectThread(nextThread?.id ?? null);
- }, [
- lastListedSelection,
- navigate,
- selectedThreadId,
- shouldSelectFirstThread,
- threads,
- ]);
+ // oxlint-disable-next-line twenty/no-navigate-prefer-link
+ navigate(
+ AppPath.AiChatInbox,
+ { threadId: nextThread?.id ?? null },
+ undefined,
+ { replace: true },
+ );
+ }, [lastListedSelection, navigate, selectedThreadId, threads]);
return null;
};
diff --git a/packages/twenty-front/src/modules/ai/components/__tests__/AiChatInboxSelectionEffect.test.tsx b/packages/twenty-front/src/modules/ai/components/__tests__/AiChatInboxSelectionEffect.test.tsx
index c493174c898..2c7fa2fd640 100644
--- a/packages/twenty-front/src/modules/ai/components/__tests__/AiChatInboxSelectionEffect.test.tsx
+++ b/packages/twenty-front/src/modules/ai/components/__tests__/AiChatInboxSelectionEffect.test.tsx
@@ -26,23 +26,10 @@ describe('AiChatInboxSelectionEffect', () => {
navigate.mockClear();
});
- it('selects the first chat when none is selected', () => {
- render(
- <AiChatInboxSelectionEffect
- selectedThreadId={undefined}
- shouldSelectFirstThread
- threads={buildThreads(['thread-1', 'thread-2'])}
- />,
- );
-
- expectSelected('thread-1');
- });
-
it('selects the chat that takes the place of the one leaving the list', () => {
const { rerender } = render(
<AiChatInboxSelectionEffect
selectedThreadId="thread-2"
- shouldSelectFirstThread
threads={buildThreads(['thread-1', 'thread-2', 'thread-3'])}
/>,
);
@@ -52,7 +39,6 @@ describe('AiChatInboxSelectionEffect', () => {
rerender(
<AiChatInboxSelectionEffect
selectedThreadId="thread-2"
- shouldSelectFirstThread
threads={buildThreads(['thread-1', 'thread-3'])}
/>,
);
@@ -64,7 +50,6 @@ describe('AiChatInboxSelectionEffect', () => {
const { rerender } = render(
<AiChatInboxSelectionEffect
selectedThreadId="thread-2"
- shouldSelectFirstThread
threads={buildThreads(['thread-1', 'thread-2'])}
/>,
);
@@ -72,7 +57,6 @@ describe('AiChatInboxSelectionEffect', () => {
rerender(
<AiChatInboxSelectionEffect
selectedThreadId="thread-2"
- shouldSelectFirstThread
threads={buildThreads(['thread-1'])}
/>,
);
@@ -84,17 +68,12 @@ describe('AiChatInboxSelectionEffect', () => {
const { rerender } = render(
<AiChatInboxSelectionEffect
selectedThreadId="thread-1"
- shouldSelectFirstThread
threads={buildThreads(['thread-1'])}
/>,
);
rerender(
- <AiChatInboxSelectionEffect
- selectedThreadId="thread-1"
- shouldSelectFirstThread
- threads={[]}
- />,
+ <AiChatInboxSelectionEffect selectedThreadId="thread-1" threads={[]} />,
);
expectSelected(null);
@@ -104,23 +83,10 @@ describe('AiChatInboxSelectionEffect', () => {
render(
<AiChatInboxSelectionEffect
selectedThreadId="thread-9"
- shouldSelectFirstThread
threads={buildThreads(['thread-1'])}
/>,
);
expect(navigate).not.toHaveBeenCalled();
});
-
- it('leaves the list alone when no chat is beside it', () => {
- render(
- <AiChatInboxSelectionEffect
- selectedThreadId={undefined}
- shouldSelectFirstThread={false}
- threads={buildThreads(['thread-1', 'thread-2'])}
- />,
- );
-
- expect(navigate).not.toHaveBeenCalled();
- });
});
diff --git a/packages/twenty-front/src/pages/ai-chat/AiChatInboxPage.tsx b/packages/twenty-front/src/pages/ai-chat/AiChatInboxPage.tsx
index 3aedf17b8e4..5e8516bb17c 100644
--- a/packages/twenty-front/src/pages/ai-chat/AiChatInboxPage.tsx
+++ b/packages/twenty-front/src/pages/ai-chat/AiChatInboxPage.tsx
@@ -133,8 +133,14 @@ const AiChatInboxPageContent = () => {
const isListShown = isSplitView || !isDefined(selectedThreadId);
const isSelectionPaneShown = isSplitView && isSelectionShown;
const isThreadShown = isDefined(selectedThreadId) && !isSelectionPaneShown;
+ // Split view always has a chat beside the list
+ const firstThreadToOpen =
+ isSplitView && !isDefined(selectedThreadId) ? threads.at(0) : undefined;
const isEmptyPaneShown =
- isSplitView && !isDefined(selectedThreadId) && !isSelectionShown;
+ isSplitView &&
+ !isDefined(selectedThreadId) &&
+ !isSelectionShown &&
+ !isDefined(firstThreadToOpen);
const openThreadIds =
isDefined(selectedThreadId) && !isSelectionShown ? [selectedThreadId] : [];
@@ -145,8 +151,15 @@ const AiChatInboxPageContent = () => {
<AiChatInboxSelectionEffect
selectedThreadId={selectedThreadId}
threads={threads}
- shouldSelectFirstThread={isSplitView}
/>
+ {isDefined(firstThreadToOpen) && (
+ <Navigate
+ to={getAppPath(AppPath.AiChatInbox, {
+ threadId: firstThreadToOpen.id,
+ })}
+ replace
+ />
+ )}
{/* The selection takes the place of the chat on screen, so the command
menu acts on it */}
{isSelectionShown && !isThreadShown && (
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.tsx
index 6a08b379e2a..1bc4dd9816c 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
@@ -224,6 +224,15 @@ describe('AiChatInboxPage', () => {
expect(screen.getByText('Chat page without the inbox')).toBeInTheDocument();
});
+ it('opens the first chat beside the list', () => {
+ renderInbox('/inbox');
+
+ expect(screen.getByText(`Chat page ${firstThread.id}`)).toBeInTheDocument();
+ expect(
+ screen.queryByText('No conversation selected'),
+ ).not.toBeInTheDocument();
+ });
+
it('opens the clicked chat', () => {
renderInbox();