add pagination to history page

This commit is contained in:
vr10t 2023-03-01 18:00:18 +00:00
parent fb8482b007
commit c6e1ab848c

View file

@ -33,6 +33,9 @@ export default {
}, },
data() { data() {
return { return {
currentVideoCount: 0,
videoStep: 100,
videosStore: [],
videos: [], videos: [],
}; };
}, },
@ -42,29 +45,39 @@ export default {
var tx = window.db.transaction("watch_history", "readonly"); var tx = window.db.transaction("watch_history", "readonly");
var store = tx.objectStore("watch_history"); var store = tx.objectStore("watch_history");
const cursorRequest = store.index("watchedAt").openCursor(null, "prev"); const cursorRequest = store.index("watchedAt").openCursor(null, "prev");
cursorRequest.onsuccess = e => { const cursorPromise = new Promise(resolve => {
const cursor = e.target.result; cursorRequest.onsuccess = e => {
if (cursor) { const cursor = e.target.result;
const video = cursor.value; if (cursor) {
this.videos.push({ const video = cursor.value;
url: "/watch?v=" + video.videoId, this.videosStore.push({
title: video.title, url: "/watch?v=" + video.videoId,
uploaderName: video.uploaderName, title: video.title,
uploaderUrl: video.uploaderUrl, uploaderName: video.uploaderName,
duration: video.duration, uploaderUrl: video.uploaderUrl,
thumbnail: video.thumbnail, duration: video.duration,
watchedAt: video.watchedAt, thumbnail: video.thumbnail,
watched: true, watchedAt: video.watchedAt,
currentTime: video.currentTime, watched: true,
}); currentTime: video.currentTime,
if (this.videos.length < 1000) cursor.continue(); });
} if (this.videosStore.length < 1000) cursor.continue();
}; else resolve();
} else resolve();
};
});
await cursorPromise;
} }
})(); })().then(() => {
this.loadMoreVideos();
});
}, },
activated() { activated() {
document.title = "Watch History - Piped"; document.title = "Watch History - Piped";
window.addEventListener("scroll", this.handleScroll);
},
deactivated() {
window.removeEventListener("scroll", this.handleScroll);
}, },
methods: { methods: {
clearHistory() { clearHistory() {
@ -91,6 +104,17 @@ export default {
}; };
this.download(JSON.stringify(json), `piped_history_${dateStr}.json`, "application/json"); this.download(JSON.stringify(json), `piped_history_${dateStr}.json`, "application/json");
}, },
loadMoreVideos() {
console.log("load more videos", this.videosStore.length, this.currentVideoCount);
this.currentVideoCount = Math.min(this.currentVideoCount + this.videoStep, this.videosStore.length);
if (this.videos.length != this.videosStore.length)
this.videos = this.videosStore.slice(0, this.currentVideoCount);
},
handleScroll() {
if (window.innerHeight + window.scrollY >= document.body.offsetHeight - window.innerHeight) {
this.loadMoreVideos();
}
},
}, },
}; };
</script> </script>