Skip to content

Commit

Permalink
fix: thread auto scroll on open/close and open only one thread
Browse files Browse the repository at this point in the history
  • Loading branch information
TopETH committed May 13, 2024
1 parent 7e0852a commit b9b09a4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
6 changes: 4 additions & 2 deletions apps/masterbots.ai/components/shared/thread-accordion.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,10 @@ export function ThreadAccordion({
// Cleanup function to remove the query parameter on unmount
return () => {
const url = new URL(window.location.href)
url.searchParams.delete('threadId')
window.history.pushState({}, '', url.pathname + url.search)
if (url.searchParams.get('threadId') === thread.threadId) {
url.searchParams.delete('threadId')
window.history.pushState({}, '', url.pathname + url.search)
}
}
}, [thread.threadId, pathname])

Expand Down
20 changes: 19 additions & 1 deletion apps/masterbots.ai/components/shared/thread-list-accordion.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import type { Thread } from '@repo/mb-genql'
import { DialogProps } from '@radix-ui/react-dialog'
import { useSetState } from 'react-use'
import { cn } from '@/lib/utils'
import { cn, sleep } from '@/lib/utils'
import {
Accordion,
AccordionContent,
Expand All @@ -13,6 +13,8 @@ import {
import { ThreadAccordion } from './thread-accordion'
import { ThreadHeading } from './thread-heading'
import { createMessagePairs } from '@/lib/threads'
import { useSearchParams } from 'next/navigation'
import { useEffect, useRef } from 'react'

export function ThreadListAccordion({
thread,
Expand All @@ -27,13 +29,29 @@ export function ThreadListAccordion({
firstResponse:
thread.messages.find(m => m.role === 'assistant')?.content || 'not found'
})
const searchParams = useSearchParams();
const threadRef = useRef<HTMLDivElement>(null)
const handleThreadIdChange = async () => {
if (searchParams.get('threadId') === thread.threadId) {
await sleep(300) // animation time
threadRef.current?.scrollIntoView({ behavior: 'smooth', block: 'start' })
} else if (state.isOpen && searchParams.get('threadId')) {
setState({ isOpen: false })
}
}

useEffect(() => {
handleThreadIdChange()
}, [searchParams ])

return (
<Accordion
ref={threadRef}
className="w-full"
onValueChange={v => {
setState({ isOpen: v[0] === 'pair-1' })
}}
value={state.isOpen ? ['pair-1'] : []}
type="multiple"
>
{/* Frist level question and excerpt visible on lists */}
Expand Down

0 comments on commit b9b09a4

Please sign in to comment.