Skip to content

Commit

Permalink
Add scroll helper, auto scroll for thread
Browse files Browse the repository at this point in the history
  • Loading branch information
tanchekwei authored and sunner committed Feb 9, 2024
1 parent 77c9f46 commit 207a93b
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 25 deletions.
3 changes: 3 additions & 0 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ import Chats from "@/store/chats";
import { initializeQueues, startQueuesProcessing } from "@/store/queue";
import { useObservable } from "@vueuse/rxjs";
import { liveQuery } from "dexie";
import { onScroll } from "./helpers/scroll-helper";
// Components
import ChatDrawer from "@/components/ChatDrawer/ChatDrawer.vue";
Expand Down Expand Up @@ -269,6 +270,8 @@ onMounted(() => {
initializeQueues(store);
startQueuesProcessing();
window.addEventListener("scroll", onScroll);
});
watch(
Expand Down
27 changes: 2 additions & 25 deletions src/components/Messages/ChatMessages.vue
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@
<script setup>
import Messages from "@/store/messages";
import { liveQuery } from "dexie";
import { computed, nextTick, onMounted, onUnmounted, ref, watch } from "vue";
import { computed, nextTick, onMounted, ref, watch } from "vue";
import { useStore } from "vuex";
import ChatPrompt from "./ChatPrompt.vue";
import ChatResponse from "./ChatResponse.vue";
import { autoScrollToBottom, scrollToBottom } from "@/helpers/scroll-helper";
const store = useStore();
Expand All @@ -46,7 +47,6 @@ const props = defineProps({
},
});
const autoScroll = ref(true);
const loading = ref(false);
const gridTemplateColumns = computed(() => `repeat(${props.columns}, 1fr)`);
const currentChatMessages = ref([]);
Expand Down Expand Up @@ -86,19 +86,6 @@ let createChatMessageLiveQuery = (index) => {
});
};
const scrollToBottom = ({ immediately = false }) => {
window.scrollTo({
top: document.documentElement.scrollHeight,
behavior: immediately ? "instant" : "smooth",
});
};
const autoScrollToBottom = () => {
if (autoScroll.value) {
scrollToBottom({ immediately: true });
}
};
const currentChatIndex = computed(() => store.state.currentChatIndex);
let currentMessageSub;
let scrollToBottomFirst;
Expand All @@ -125,20 +112,10 @@ watch(
{ immediate: true },
);
const onScroll = () => {
const scrollPosition = window.pageYOffset + window.innerHeight;
autoScroll.value =
scrollPosition >= document.documentElement.scrollHeight - 40;
};
onMounted(async () => {
await Messages.table
.filter((message) => message.done !== true)
.modify({ done: true });
window.addEventListener("scroll", onScroll);
});
onUnmounted(() => {
window.removeEventListener("scroll", onScroll);
});
</script>

Expand Down
3 changes: 3 additions & 0 deletions src/components/Messages/ChatThread.vue
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import ChatResponse from "@/components/Messages/ChatResponse.vue";
import Threads from "@/store/threads";
import { useObservable } from "@vueuse/rxjs";
import { liveQuery } from "dexie";
import { nextTick } from "vue";
import { autoScrollToBottom } from "@/helpers/scroll-helper";
const props = defineProps({
chat: {
Expand Down Expand Up @@ -73,6 +75,7 @@ const currentChatMessages = useObservable(
}
currentChatMessages.value = groupedMessage;
nextTick(() => autoScrollToBottom());
console.log("groupedMessage threads: ", groupedMessage);
return groupedMessage;
}),
Expand Down
19 changes: 19 additions & 0 deletions src/helpers/scroll-helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export const scrollToBottom = ({ immediately = false }) => {
window.scrollTo({
top: document.documentElement.scrollHeight,
behavior: immediately ? "instant" : "smooth",
});
};

export const autoScrollToBottom = () => {
if (autoScroll) {
scrollToBottom({ immediately: true });
}
};

export const onScroll = () => {
const scrollPosition = window.pageYOffset + window.innerHeight;
autoScroll = scrollPosition >= document.documentElement.scrollHeight - 40;
};

export let autoScroll = false;

0 comments on commit 207a93b

Please sign in to comment.