diff --git a/packages/twenty-front/src/modules/ai/components/AgentChatProviderContent.tsx b/packages/twenty-front/src/modules/ai/components/AgentChatProviderContent.tsx index 70cedeaab9f..d8471610a38 100644 --- a/packages/twenty-front/src/modules/ai/components/AgentChatProviderContent.tsx +++ b/packages/twenty-front/src/modules/ai/components/AgentChatProviderContent.tsx @@ -1,9 +1,4 @@ -import { AgentChatStreamSubscriptionEffect } from '@/ai/components/AgentChatStreamSubscriptionEffect'; -import { AgentChatMessagesFetchEffect } from '@/ai/components/AgentChatMessagesFetchEffect'; -import { AgentChatSessionStartTimeEffect } from '@/ai/components/AgentChatSessionStartTimeEffect'; - -import { AgentChatStreamingAutoScrollEffect } from '@/ai/components/AgentChatStreamingAutoScrollEffect'; -import { AgentChatStreamingPartsDiffSyncEffect } from '@/ai/components/AgentChatStreamingPartsDiffSyncEffect'; +import { AgentChatRuntimeEffects } from '@/ai/components/AgentChatRuntimeEffects'; import { AgentChatThreadInitializationEffect } from '@/ai/components/AgentChatThreadInitializationEffect'; import { AgentChatComponentInstanceContext } from '@/ai/contexts/AgentChatComponentInstanceContext'; import { Suspense } from 'react'; @@ -19,11 +14,7 @@ export const AgentChatProviderContent = ({ value={{ instanceId: 'agentChatComponentInstance' }} > - - - - - + {children} diff --git a/packages/twenty-front/src/modules/ai/components/AgentChatRuntimeEffects.tsx b/packages/twenty-front/src/modules/ai/components/AgentChatRuntimeEffects.tsx new file mode 100644 index 00000000000..df4e0b0b46a --- /dev/null +++ b/packages/twenty-front/src/modules/ai/components/AgentChatRuntimeEffects.tsx @@ -0,0 +1,48 @@ +import { AgentChatMessagesFetchEffect } from '@/ai/components/AgentChatMessagesFetchEffect'; +import { AgentChatSessionStartTimeEffect } from '@/ai/components/AgentChatSessionStartTimeEffect'; +import { AgentChatStreamSubscriptionEffect } from '@/ai/components/AgentChatStreamSubscriptionEffect'; +import { AgentChatStreamingAutoScrollEffect } from '@/ai/components/AgentChatStreamingAutoScrollEffect'; +import { AgentChatStreamingPartsDiffSyncEffect } from '@/ai/components/AgentChatStreamingPartsDiffSyncEffect'; +import { hasAgentChatBeenOpenedState } from '@/ai/states/hasAgentChatBeenOpenedState'; +import { isSidePanelOpenedState } from '@/side-panel/states/isSidePanelOpenedState'; +import { sidePanelPageState } from '@/side-panel/states/sidePanelPageState'; +import { useAtomState } from '@/ui/utilities/state/jotai/hooks/useAtomState'; +import { useAtomStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomStateValue'; +import { useEffect } from 'react'; +import { SidePanelPages } from 'twenty-shared/types'; + +export const AgentChatRuntimeEffects = () => { + const isSidePanelOpened = useAtomStateValue(isSidePanelOpenedState); + const sidePanelPage = useAtomStateValue(sidePanelPageState); + + const [hasAgentChatBeenOpened, setHasAgentChatBeenOpened] = useAtomState( + hasAgentChatBeenOpenedState, + ); + + const isAgentChatOpen = + isSidePanelOpened && sidePanelPage === SidePanelPages.AskAI; + + useEffect(() => { + if (isAgentChatOpen && !hasAgentChatBeenOpened) { + setHasAgentChatBeenOpened(true); + } + }, [isAgentChatOpen, hasAgentChatBeenOpened, setHasAgentChatBeenOpened]); + + if (!hasAgentChatBeenOpened) { + return null; + } + + return ( + <> + + + + {isAgentChatOpen && ( + <> + + + + )} + + ); +}; diff --git a/packages/twenty-front/src/modules/ai/states/hasAgentChatBeenOpenedState.ts b/packages/twenty-front/src/modules/ai/states/hasAgentChatBeenOpenedState.ts new file mode 100644 index 00000000000..c2b6aa6ee1d --- /dev/null +++ b/packages/twenty-front/src/modules/ai/states/hasAgentChatBeenOpenedState.ts @@ -0,0 +1,6 @@ +import { createAtomState } from '@/ui/utilities/state/jotai/utils/createAtomState'; + +export const hasAgentChatBeenOpenedState = createAtomState({ + key: 'ai/hasAgentChatBeenOpenedState', + defaultValue: false, +});