diff --git a/packages/app/src/components/chat/ChatWindow.vue b/packages/app/src/components/chat/ChatWindow.vue index 46894831..4276409a 100644 --- a/packages/app/src/components/chat/ChatWindow.vue +++ b/packages/app/src/components/chat/ChatWindow.vue @@ -137,6 +137,27 @@ const codeContext = useCodeContext() const messageListRef = ref(null) const chatSearchRef = ref | null>(null) +// Scroll position memory per conversation +const scrollPositions = new Map() + +function saveScrollPosition() { + const el = messageListRef.value + const id = chatStore.activeConversationId + if (el && id) { + scrollPositions.set(id, el.scrollTop) + } +} + +function restoreScrollPosition(convId: string) { + const saved = scrollPositions.get(convId) + if (saved !== undefined) { + nextTick(() => { + const el = messageListRef.value + if (el) el.scrollTop = saved + }) + } +} + function scrollToMessageIndex(index: number) { virtualizer.value.scrollToIndex(index, { align: 'center' }) } @@ -313,6 +334,15 @@ function scrollToBottom() { }) } +// Save/restore scroll position on conversation switch +watch( + () => chatStore.activeConversationId, + (newId, oldId) => { + if (oldId) saveScrollPosition() + if (newId) restoreScrollPosition(newId) + } +) + watch( () => messages.value.length, () => scrollToBottom()