Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix DB race condition. Refresh at then end of a completed stream. #404

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions components/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,22 @@ export function Chat({ id, className, session, missingKeys }: ChatProps) {
const [messages] = useUIState()
const [aiState] = useAIState()

const [_, setNewChatId] = useLocalStorage('newChatId', id)
const [_, setNewChatAnimationId] = useLocalStorage('newChatAnimationId', id)

useEffect(() => {
if (session?.user) {
if (!path.includes('chat') && messages.length === 1) {
window.history.replaceState({}, '', `/chat/${id}`)
setNewChatAnimationId(id)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The sidebar item should only animate the very first time it is created.

}
}
}, [id, path, session?.user, messages])
}, [id, path, session?.user, messages, setNewChatAnimationId])

useEffect(() => {
const messagesLength = aiState.messages?.length
if (messagesLength === 2) {
if (aiState.refreshKey) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There will only be a refresh key at the end of a aiState.done(). No extra refreshes on page loads, but also will have refreshes at the end of each message, not just the first, fixing existing navigation issues like #364

router.refresh()
}
}, [aiState.messages, router])

useEffect(() => {
setNewChatId(id)
})
}, [aiState.refreshKey, router])

useEffect(() => {
missingKeys.map(key => {
Expand Down
6 changes: 3 additions & 3 deletions components/sidebar-item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ export function SidebarItem({ index, chat, children }: SidebarItemProps) {
const pathname = usePathname()

const isActive = pathname === chat.path
const [newChatId, setNewChatId] = useLocalStorage('newChatId', null)
const shouldAnimate = index === 0 && isActive && newChatId
const [animationKey, setNewChatAnimationId] = useLocalStorage('newChatAnimationId', null)
const shouldAnimate = index === 0 && isActive && animationKey

if (!chat?.id) return null

Expand Down Expand Up @@ -105,7 +105,7 @@ export function SidebarItem({ index, chat, children }: SidebarItemProps) {
}}
onAnimationComplete={() => {
if (index === chat.title.length - 1) {
setNewChatId(null)
setNewChatAnimationId(null)
}
}}
>
Expand Down
Loading