@@ -6,51 +6,51 @@ import { ChatList } from '@/components/routes/chat/chat-list'
66import { Button } from '@/components/ui/button'
77import { IconClose } from '@/components/ui/icons'
88import { Skeleton } from '@/components/ui/skeleton'
9- import { useAtBottom } from '@/lib/hooks/use-at-bottom'
109import { useMBChat } from '@/lib/hooks/use-mb-chat'
1110import { useSidebar } from '@/lib/hooks/use-sidebar'
1211import { useThread } from '@/lib/hooks/use-thread'
13- import { cn , getRouteType , scrollToBottomOfElement } from '@/lib/utils'
12+ import { cn , getRouteType } from '@/lib/utils'
1413import { getMessages } from '@/services/hasura'
1514import type { Message as AiMessage } from 'ai'
16- import { useScroll } from 'framer-motion'
1715import type { Chatbot , Message } from 'mb-genql'
1816import { usePathname } from 'next/navigation'
1917import { useEffect , useRef , useState } from 'react'
18+ import { useScroll } from '@/lib/hooks/use-scroll'
2019
2120export function ThreadPopup ( { className } : { className ?: string } ) {
2221 const { activeChatbot } = useSidebar ( )
2322 const { isOpenPopup, activeThread } = useThread ( )
2423 const [ { allMessages, isLoading } , { sendMessageFromResponse } ] = useMBChat ( )
2524 const [ browseMessages , setBrowseMessages ] = useState < Message [ ] > ( [ ] )
26- const popupContentRef = useRef < HTMLDivElement > ( )
25+ const popupContentRef = useRef < HTMLDivElement > ( null )
26+ const threadRef = useRef < HTMLDivElement > ( null )
2727 const pathname = usePathname ( )
2828
29- const { scrollY } = useScroll ( {
30- container : popupContentRef as React . RefObject < HTMLElement > ,
31- } )
32-
33- const { isAtBottom } = useAtBottom ( {
34- ref : popupContentRef ,
35- scrollY,
29+ const { isNearBottom, smoothScrollToBottom } = useScroll ( {
30+ containerRef : popupContentRef ,
31+ threadRef,
32+ isNewContent : isLoading ,
33+ hasMore : false ,
34+ isLast : true ,
35+ loading : isLoading ,
36+ loadMore : ( ) => { } ,
3637 } )
3738
3839 const scrollToBottom = ( ) => {
3940 if ( popupContentRef . current ) {
40- const element = popupContentRef . current
41- scrollToBottomOfElement ( element )
41+ smoothScrollToBottom ( )
4242 }
4343 }
4444
45- // biome-ignore lint/correctness/useExhaustiveDependencies: <explanation>
45+ // Update effect to use smoothScrollToBottom from custom hook
4646 useEffect ( ( ) => {
4747 if ( isLoading && isOpenPopup ) {
4848 const timeout = setTimeout ( ( ) => {
49- scrollToBottom ( )
49+ smoothScrollToBottom ( )
5050 clearTimeout ( timeout )
5151 } , 150 )
5252 }
53- } , [ isLoading , isOpenPopup ] )
53+ } , [ isLoading , isOpenPopup , smoothScrollToBottom ] )
5454
5555 // Fetch browse messages when activeThread changes
5656 useEffect ( ( ) => {
@@ -95,46 +95,48 @@ export function ThreadPopup({ className }: { className?: string }) {
9595 />
9696
9797 < div
98+ ref = { popupContentRef }
9899 className = { cn (
99100 'flex flex-col dark:bg-[#18181b] bg-white grow rounded-b-[8px] scrollbar h-full' ,
100101 isBrowseView ? 'pb-2 md:pb-4' : 'pb-[120px] md:pb-[180px]' ,
101- isBrowseView ? '' :'max-h-[calc(100%-240px)] md:max-h-[calc(100%-220px)]' ,
102+ isBrowseView ? '' : 'max-h-[calc(100%-240px)] md:max-h-[calc(100%-220px)]' ,
102103 className ,
103104 ) }
104- ref = { popupContentRef as React . Ref < HTMLDivElement > }
105105 >
106- { isBrowseView ? (
107- // Browse view
108- < div className = "px-8 py-4" >
109- < BrowseChatMessageList
110- chatbot = { activeThread ?. chatbot }
111- user = { activeThread ?. user || undefined }
112- messages = { browseMessages }
113- threadId = { activeThread ?. threadId }
114- />
115- </ div >
116- ) : (
117- // Chat view
118- < >
119- < ChatList
120- isThread = { false }
121- messages = { allMessages }
122- sendMessageFn = { sendMessageFromResponse }
123- chatbot = { activeThread ?. chatbot || ( activeChatbot as Chatbot ) }
124- chatContentClass = "!border-x-gray-300 !px-[16px] !mx-0 max-h-[none] dark:!border-x-mirage"
125- className = "max-w-full !px-[32px] !mx-0"
126- chatArrowClass = "!right-0 !mr-0"
127- chatTitleClass = "!px-[11px]"
128- />
129-
130- < Chat
131- isPopup
132- chatPanelClassName = "!pl-0 rounded-b-[8px] overflow-hidden !absolute"
133- scrollToBottom = { scrollToBottom }
134- isAtBottom = { isAtBottom }
135- />
136- </ >
137- ) }
106+ < div ref = { threadRef } >
107+ { isBrowseView ? (
108+ // Browse view
109+ < div className = "px-8 py-4" >
110+ < BrowseChatMessageList
111+ chatbot = { activeThread ?. chatbot }
112+ user = { activeThread ?. user || undefined }
113+ messages = { browseMessages }
114+ threadId = { activeThread ?. threadId }
115+ />
116+ </ div >
117+ ) : (
118+ // Chat view
119+ < >
120+ < ChatList
121+ isThread = { false }
122+ messages = { allMessages }
123+ sendMessageFn = { sendMessageFromResponse }
124+ chatbot = { activeThread ?. chatbot || ( activeChatbot as Chatbot ) }
125+ chatContentClass = "!border-x-gray-300 !px-[16px] !mx-0 max-h-[none] dark:!border-x-mirage"
126+ className = "max-w-full !px-[32px] !mx-0"
127+ chatArrowClass = "!right-0 !mr-0"
128+ chatTitleClass = "!px-[11px]"
129+ />
130+
131+ < Chat
132+ isPopup
133+ chatPanelClassName = "!pl-0 rounded-b-[8px] overflow-hidden !absolute"
134+ scrollToBottom = { scrollToBottom }
135+ isAtBottom = { isNearBottom }
136+ />
137+ </ >
138+ ) }
139+ </ div >
138140 </ div >
139141 </ div >
140142 </ div >
0 commit comments