Skip to content

Commit 207a93b

Browse files
tanchekweisunner
authored andcommitted
Add scroll helper, auto scroll for thread
1 parent 77c9f46 commit 207a93b

File tree

4 files changed

+27
-25
lines changed

4 files changed

+27
-25
lines changed

src/App.vue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ import Chats from "@/store/chats";
164164
import { initializeQueues, startQueuesProcessing } from "@/store/queue";
165165
import { useObservable } from "@vueuse/rxjs";
166166
import { liveQuery } from "dexie";
167+
import { onScroll } from "./helpers/scroll-helper";
167168
168169
// Components
169170
import ChatDrawer from "@/components/ChatDrawer/ChatDrawer.vue";
@@ -269,6 +270,8 @@ onMounted(() => {
269270
270271
initializeQueues(store);
271272
startQueuesProcessing();
273+
274+
window.addEventListener("scroll", onScroll);
272275
});
273276
274277
watch(

src/components/Messages/ChatMessages.vue

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@
2929
<script setup>
3030
import Messages from "@/store/messages";
3131
import { liveQuery } from "dexie";
32-
import { computed, nextTick, onMounted, onUnmounted, ref, watch } from "vue";
32+
import { computed, nextTick, onMounted, ref, watch } from "vue";
3333
import { useStore } from "vuex";
3434
import ChatPrompt from "./ChatPrompt.vue";
3535
import ChatResponse from "./ChatResponse.vue";
36+
import { autoScrollToBottom, scrollToBottom } from "@/helpers/scroll-helper";
3637
3738
const store = useStore();
3839
@@ -46,7 +47,6 @@ const props = defineProps({
4647
},
4748
});
4849
49-
const autoScroll = ref(true);
5050
const loading = ref(false);
5151
const gridTemplateColumns = computed(() => `repeat(${props.columns}, 1fr)`);
5252
const currentChatMessages = ref([]);
@@ -86,19 +86,6 @@ let createChatMessageLiveQuery = (index) => {
8686
});
8787
};
8888
89-
const scrollToBottom = ({ immediately = false }) => {
90-
window.scrollTo({
91-
top: document.documentElement.scrollHeight,
92-
behavior: immediately ? "instant" : "smooth",
93-
});
94-
};
95-
96-
const autoScrollToBottom = () => {
97-
if (autoScroll.value) {
98-
scrollToBottom({ immediately: true });
99-
}
100-
};
101-
10289
const currentChatIndex = computed(() => store.state.currentChatIndex);
10390
let currentMessageSub;
10491
let scrollToBottomFirst;
@@ -125,20 +112,10 @@ watch(
125112
{ immediate: true },
126113
);
127114
128-
const onScroll = () => {
129-
const scrollPosition = window.pageYOffset + window.innerHeight;
130-
autoScroll.value =
131-
scrollPosition >= document.documentElement.scrollHeight - 40;
132-
};
133-
134115
onMounted(async () => {
135116
await Messages.table
136117
.filter((message) => message.done !== true)
137118
.modify({ done: true });
138-
window.addEventListener("scroll", onScroll);
139-
});
140-
onUnmounted(() => {
141-
window.removeEventListener("scroll", onScroll);
142119
});
143120
</script>
144121

src/components/Messages/ChatThread.vue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import ChatResponse from "@/components/Messages/ChatResponse.vue";
2323
import Threads from "@/store/threads";
2424
import { useObservable } from "@vueuse/rxjs";
2525
import { liveQuery } from "dexie";
26+
import { nextTick } from "vue";
27+
import { autoScrollToBottom } from "@/helpers/scroll-helper";
2628
2729
const props = defineProps({
2830
chat: {
@@ -73,6 +75,7 @@ const currentChatMessages = useObservable(
7375
}
7476
7577
currentChatMessages.value = groupedMessage;
78+
nextTick(() => autoScrollToBottom());
7679
console.log("groupedMessage threads: ", groupedMessage);
7780
return groupedMessage;
7881
}),

src/helpers/scroll-helper.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export const scrollToBottom = ({ immediately = false }) => {
2+
window.scrollTo({
3+
top: document.documentElement.scrollHeight,
4+
behavior: immediately ? "instant" : "smooth",
5+
});
6+
};
7+
8+
export const autoScrollToBottom = () => {
9+
if (autoScroll) {
10+
scrollToBottom({ immediately: true });
11+
}
12+
};
13+
14+
export const onScroll = () => {
15+
const scrollPosition = window.pageYOffset + window.innerHeight;
16+
autoScroll = scrollPosition >= document.documentElement.scrollHeight - 40;
17+
};
18+
19+
export let autoScroll = false;

0 commit comments

Comments
 (0)