Compare commits

..

1 Commits

Author SHA1 Message Date
mustafa-rezae
080a4fee59 Update useInfiniteScroll files 2025-05-19 11:58:03 +03:30
5 changed files with 104 additions and 72 deletions

View File

@ -67,13 +67,7 @@
<script> <script>
import { mapActions, mapState } from "pinia"; import { mapActions, mapState } from "pinia";
import { useResearchStore } from "~/stores/researchStore";
import { useAuthStore } from "@stores/authStore";
import { useCommonStore } from "@stores/commonStore";
import { useEntityStore } from "@entity/entityStore";
import { useSearchStore } from "@search/stores/searchStore";
import { useResearchStore } from "@research/stores/researchStore";
export default { export default {
mounted() { mounted() {
@ -108,18 +102,13 @@ export default {
}, },
methods: { methods: {
...mapActions(useCommonStore, [ ...mapActions("common", ["systemsSetter", "activeSystemSetter"]),
"systemsSetter",
"activeSystemSetter",
"searchActiveTabSetter",
]),
...mapActions("aiTools", [ ...mapActions("aiTools", [
"aiToolsActiveActionSetter", "aiToolsActiveActionSetter",
"aiToolsSchemaSetter", "aiToolsSchemaSetter",
"aiToolsActiveDomainSetter", "aiToolsActiveDomainSetter",
]), ]),
...mapActions(useEntityStore, [ ...mapActions("entity", [
"activeTabSetter", "activeTabSetter",
"entityViewSchemaSetter", "entityViewSchemaSetter",
"activeEntityViewSchemaSetter", "activeEntityViewSchemaSetter",
@ -130,9 +119,10 @@ export default {
"domainActiveSetter", "domainActiveSetter",
"selectionFilterItemsSetter", "selectionFilterItemsSetter",
]), ]),
...mapActions(useSearchStore, [ ...mapActions("search", [
"domainActiveSetter", //3 "domainActiveSetter", //3
"activeTabSetter", //2 "activeTabSetter", //2
"searchActiveTabSetter",
"searchSchemaSetter", "searchSchemaSetter",
"helpSchemaSetter", "helpSchemaSetter",
"helpActiveSchemaSetter", "helpActiveSchemaSetter",
@ -144,7 +134,7 @@ export default {
]), ]),
reloadPage() { reloadPage() {
let appVersion = useStorage("app-version", "0.0.0"); let appVersion = useStorage("app-version", '0.0.0');
appVersion.vlaue = this.envVersion; appVersion.vlaue = this.envVersion;
this.clearStorage(); this.clearStorage();

View File

@ -1,18 +1,58 @@
// composables/useInfiniteScroll.ts // composables/useInfiniteScroll.ts
import { ref, onMounted, onBeforeUnmount } from "vue";
export default function useInfiniteScroll( export default function useInfiniteScroll(
callback: () => void, callback: () => Promise<void>,
elementId: string elementId?: string
) { ) {
const route = useRoute();
const isFetching = ref(false); const isFetching = ref(false);
const infiniteScroll = ref<HTMLElement | null>(null); const infiniteScroll = ref<HTMLElement | Window | null>(null);
const handleScroll = () => { // Throttle function to limit scroll event frequency
if (isFetching.value) return; const throttle = (func: (...args: any[]) => void, limit: number) => {
const scrollPosition = let lastFunc: ReturnType<typeof setTimeout>;
infiniteScroll.value.scrollTop + infiniteScroll.value.clientHeight; let lastRan: number;
const threshold = infiniteScroll.value.scrollHeight - 100; return function (this: any, ...args: any[]) {
const context = this;
if (!lastRan) {
func.apply(context, args);
lastRan = Date.now();
} else {
clearTimeout(lastFunc);
lastFunc = setTimeout(() => {
if (Date.now() - lastRan >= limit) {
func.apply(context, args);
lastRan = Date.now();
}
}, limit - (Date.now() - lastRan));
}
};
};
if (scrollPosition >= threshold) { const checkScrollPosition = () => {
if (isFetching.value || !infiniteScroll.value) return;
let scrollPosition: number;
let threshold: number;
let clientHeight: number;
let scrollHeight: number;
if (infiniteScroll.value === window) {
scrollPosition = window.scrollY || window.pageYOffset;
clientHeight = document.documentElement.clientHeight;
scrollHeight = document.documentElement.scrollHeight;
} else {
const el = infiniteScroll.value as HTMLElement;
scrollPosition = el.scrollTop;
clientHeight = el.clientHeight;
scrollHeight = el.scrollHeight;
}
threshold = scrollHeight - 100;
const currentPosition = scrollPosition + clientHeight;
if (currentPosition >= threshold) {
isFetching.value = true; isFetching.value = true;
callback().finally(() => { callback().finally(() => {
isFetching.value = false; isFetching.value = false;
@ -20,28 +60,57 @@ export default function useInfiniteScroll(
} }
}; };
// Throttled version of scroll handler
const throttledScrollHandler = throttle(checkScrollPosition, 200);
const handleTouchEnd = () => { const handleTouchEnd = () => {
// Add a slight delay to ensure scroll position is updated setTimeout(throttledScrollHandler, 100);
setTimeout(handleScroll, 100);
}; };
onMounted(() => { onMounted(() => {
const targetElement = document.getElementById(elementId); console.info(route.name)
if (route.name == "hadithaSearch" || route.name == "hadithaLibrary") {
const targetElement = elementId
? document.getElementById(elementId)
: window;
if (!targetElement) {
console.warn(
`Element ${elementId || "window"} not found for infinite scroll`
);
return;
}
infiniteScroll.value = targetElement; infiniteScroll.value = targetElement;
if (targetElement) { if (targetElement === window) {
targetElement.addEventListener("scroll", handleScroll); window.addEventListener("scroll", throttledScrollHandler);
window.addEventListener("touchend", handleTouchEnd);
} else {
targetElement.addEventListener("scroll", throttledScrollHandler);
targetElement.addEventListener("touchend", handleTouchEnd); targetElement.addEventListener("touchend", handleTouchEnd);
} }
}
}); });
onBeforeUnmount(() => { onBeforeUnmount(() => {
const targetElement = document.getElementById(elementId); if (route.name == "hadithaSearch" || route.name == "hadithaLibrary") {
infiniteScroll.value = targetElement; if (!infiniteScroll.value) return;
if (targetElement) { if (infiniteScroll.value === window) {
targetElement.removeEventListener("scroll", handleScroll); window.removeEventListener("scroll", throttledScrollHandler);
targetElement.removeEventListener("touchend", handleTouchEnd); window.removeEventListener("touchend", handleTouchEnd);
} else {
(infiniteScroll.value as HTMLElement).removeEventListener(
"scroll",
throttledScrollHandler
);
(infiniteScroll.value as HTMLElement).removeEventListener(
"touchend",
handleTouchEnd
);
}
} }
}); });

View File

@ -131,7 +131,7 @@ export default {
}; };
}, },
computed: { computed: {
...mapState(useCommonStore, ["searchSchemaGetter"]), ...mapState(useSearchStore, ["searchSchemaGetter"]),
}, },
methods: { methods: {
...mapActions(["checkPermissions"]), ...mapActions(["checkPermissions"]),

Binary file not shown.

Before

Width:  |  Height:  |  Size: 73 KiB

View File

@ -20,15 +20,9 @@ import type {
Pagination, Pagination,
} from "~/types/commonTypes"; } from "~/types/commonTypes";
import type { researchTerms, searchActiveTab } from "~/types/researchTypes"; import type { researchTerms } from "~/types/researchTypes";
import type { ActiveEntityViewSchema, ActiveTab } from "~/types/entityType"; import type { ActiveEntityViewSchema, ActiveTab } from "~/types/entityType";
import type { import type { Domain, helpActiveSchema, helpSchema } from "~/types/searchTypes";
Domain,
helpActiveSchema,
helpSchema,
searchSchema,
searchSynonymTitle,
} from "~/types/searchTypes";
export const useCommonStore = defineStore("commonStore", { export const useCommonStore = defineStore("commonStore", {
persist: { persist: {
@ -91,11 +85,6 @@ export const useCommonStore = defineStore("commonStore", {
appVersion: "" as string, appVersion: "" as string,
isShowHilight: true as boolean, isShowHilight: true as boolean,
sidebarMenu: {}, sidebarMenu: {},
// search
searchSynonymTitle: undefined as searchSynonymTitle | undefined,
searchActiveTab: undefined as searchActiveTab | undefined,
searchSchema: undefined as searchSchema | undefined,
}), }),
getters: { getters: {
// admin // admin
@ -106,18 +95,10 @@ export const useCommonStore = defineStore("commonStore", {
return state.helpActiveSchema; return state.helpActiveSchema;
}, },
// from search // from search
searchSynonymTitleGetter(state) {
return state.searchSynonymTitle;
},
searchActiveTabGetter(state) {
return state.searchActiveTab;
},
searchSchemaGetter(state) {
return state.searchSchema;
},
domainActiveGetter(state) { domainActiveGetter(state) {
return state.domainActive; return state.domainActive;
}, },
diffTypeGetter(diffType) { diffTypeGetter(diffType) {
return diffType; return diffType;
}, },
@ -127,6 +108,7 @@ export const useCommonStore = defineStore("commonStore", {
activeEntityViewSchemaGetter(state) { activeEntityViewSchemaGetter(state) {
return state.activeEntityViewSchema; return state.activeEntityViewSchema;
}, },
researchTermsGetter(state) { researchTermsGetter(state) {
return state.researchTerms; return state.researchTerms;
}, },
@ -177,17 +159,6 @@ export const useCommonStore = defineStore("commonStore", {
this.helpActiveSchema = helpActiveSchema; this.helpActiveSchema = helpActiveSchema;
}, },
// from search // from search
searchSynonymTitleSetter(searchSynonymTitle = undefined) {
this.searchSynonymTitle = searchSynonymTitle;
},
searchActiveTabSetter(searchActiveTab: searchActiveTab) {
this.searchActiveTab = searchActiveTab;
},
searchSchemaSetter(searchSchema = undefined) {
this.searchSchema = searchSchema;
},
domainActiveSetter(domain = undefined) { domainActiveSetter(domain = undefined) {
this.domainActive = domain; this.domainActive = domain;
}, },
@ -214,6 +185,7 @@ export const useCommonStore = defineStore("commonStore", {
this.collapsed = isCollapsed; this.collapsed = isCollapsed;
}, },
SET_SECTIONS(jahatSection: JahatSection) { SET_SECTIONS(jahatSection: JahatSection) {
this.jahatSection = jahatSection; this.jahatSection = jahatSection;
}, },
@ -283,6 +255,7 @@ export const useCommonStore = defineStore("commonStore", {
this.$reset(); this.$reset();
}, },
async getState() { async getState() {
const settingsApi = (await import("~/apis/adminApi")).default; const settingsApi = (await import("~/apis/adminApi")).default;
const url = settingsApi.user.get; const url = settingsApi.user.get;