feat: improve document chunk loading and error handling in doc-content component
- Added state management for pending document requests to enhance user experience during chunk loading. - Implemented error handling for chunk loading failures, providing user feedback through warnings. - Adjusted scroll handling logic to ensure smoother pagination and prevent multiple concurrent requests. This update enhances the reliability of document loading and improves user interaction during scrolling.
This commit is contained in:
@@ -57,6 +57,8 @@ marked.use({
|
||||
const renderer = new marked.Renderer();
|
||||
let page = 1;
|
||||
let loadingChunks = false;
|
||||
let pendingRequestedPage: number | null = null;
|
||||
let pendingChunksBeforeLoad = 0;
|
||||
let doc = null;
|
||||
let down = ref()
|
||||
let mdContentWrap = ref()
|
||||
@@ -142,9 +144,23 @@ onMounted(() => {
|
||||
watch(() => props.details?.id, () => {
|
||||
page = 1;
|
||||
loadingChunks = false;
|
||||
pendingRequestedPage = null;
|
||||
pendingChunksBeforeLoad = 0;
|
||||
});
|
||||
watch(() => props.details?.md?.length, () => {
|
||||
loadingChunks = false;
|
||||
watch(() => props.details?.chunkLoading, (val) => {
|
||||
if (val === false) {
|
||||
if (pendingRequestedPage !== null) {
|
||||
const currentLength = props.details?.md?.length || 0;
|
||||
const hasError = Boolean(props.details?.chunkLoadError);
|
||||
if (hasError && currentLength <= pendingChunksBeforeLoad) {
|
||||
page = Math.max(1, pendingRequestedPage - 1);
|
||||
MessagePlugin.warning(props.details?.chunkLoadError || '分块加载失败,请继续下滑重试');
|
||||
}
|
||||
}
|
||||
pendingRequestedPage = null;
|
||||
pendingChunksBeforeLoad = 0;
|
||||
loadingChunks = false;
|
||||
}
|
||||
});
|
||||
onUnmounted(() => {
|
||||
doc.removeEventListener('scroll', handleDetailsScroll);
|
||||
@@ -566,10 +582,12 @@ const handleDetailsScroll = () => {
|
||||
if (doc && !loadingChunks) {
|
||||
let pageNum = Math.ceil(props.details.total / 25);
|
||||
const { scrollTop, scrollHeight, clientHeight } = doc;
|
||||
if (scrollTop + clientHeight >= scrollHeight) {
|
||||
if (scrollTop + clientHeight >= scrollHeight - 8) {
|
||||
if (props.details.md.length < props.details.total && page + 1 <= pageNum) {
|
||||
page++;
|
||||
loadingChunks = true;
|
||||
pendingRequestedPage = page;
|
||||
pendingChunksBeforeLoad = props.details.md.length;
|
||||
emit("getDoc", page);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,8 +13,8 @@ import { knowledgeStore } from "@/stores/knowledge";
|
||||
import { useUIStore } from "@/stores/ui";
|
||||
import { useRoute } from 'vue-router';
|
||||
|
||||
const usemenuStore = knowledgeStore();
|
||||
export default function (knowledgeBaseId?: string) {
|
||||
const usemenuStore = knowledgeStore();
|
||||
const route = useRoute();
|
||||
const { cardList, total } = storeToRefs(usemenuStore);
|
||||
let moreIndex = ref(-1);
|
||||
@@ -26,7 +26,9 @@ export default function (knowledgeBaseId?: string) {
|
||||
total: 0,
|
||||
type: "",
|
||||
source: "",
|
||||
file_type: ""
|
||||
file_type: "",
|
||||
chunkLoading: false,
|
||||
chunkLoadError: "",
|
||||
});
|
||||
const getKnowled = (
|
||||
query: { page: number; page_size: number; tag_id?: string; keyword?: string; file_type?: string } = { page: 1, page_size: 35 },
|
||||
@@ -147,7 +149,8 @@ export default function (knowledgeBaseId?: string) {
|
||||
id: "",
|
||||
type: "",
|
||||
source: "",
|
||||
file_type: ""
|
||||
file_type: "",
|
||||
chunkLoadError: "",
|
||||
});
|
||||
getKnowledgeDetails(item.id)
|
||||
.then((result: any) => {
|
||||
@@ -168,6 +171,8 @@ export default function (knowledgeBaseId?: string) {
|
||||
};
|
||||
|
||||
const getfDetails = (id: string, page: number) => {
|
||||
details.chunkLoading = true;
|
||||
details.chunkLoadError = "";
|
||||
getKnowledgeDetailsCon(id, page)
|
||||
.then((result: any) => {
|
||||
if (result.success && result.data) {
|
||||
@@ -180,7 +185,17 @@ export default function (knowledgeBaseId?: string) {
|
||||
details.total = totalResult;
|
||||
}
|
||||
})
|
||||
.catch(() => {});
|
||||
.catch((err: any) => {
|
||||
details.chunkLoadError = err?.message || "分块加载失败";
|
||||
console.error("[ChunkLoad] failed", {
|
||||
knowledgeId: id,
|
||||
page,
|
||||
error: err,
|
||||
});
|
||||
})
|
||||
.finally(() => {
|
||||
details.chunkLoading = false;
|
||||
});
|
||||
};
|
||||
return {
|
||||
cardList,
|
||||
|
||||
Reference in New Issue
Block a user