fix: resolve progress update errors and use dynamic page calculation
- reader-navigation.ts: add fallback for mediaItemId from page URL - reader-navigation.ts: use getCurrentPageFromScroll for accurate page tracking - reader-navigation.ts: use pageCalculationResult.totalPages for total - reader-services.ts: validate mediaItemId before API call - reader-services.ts: avoid double body consumption by checking response.ok
This commit is contained in:
@@ -458,12 +458,35 @@ function sendProgressUpdate() {
|
|||||||
const state = getState();
|
const state = getState();
|
||||||
if (!state.currentReader || !state.readerMetadata) return;
|
if (!state.currentReader || !state.readerMetadata) return;
|
||||||
|
|
||||||
|
const mediaItemId =
|
||||||
|
state.readerMetadata.media_item_id ||
|
||||||
|
document.body.dataset.mediaItemId ||
|
||||||
|
window.location.pathname.split("/").pop();
|
||||||
|
|
||||||
|
if (!mediaItemId) {
|
||||||
|
console.warn("No mediaItemId available for progress update");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
let currentPage = 0;
|
let currentPage = 0;
|
||||||
let totalPages = 0;
|
let totalPages = 0;
|
||||||
|
|
||||||
if (state.currentReader.type === "ebook") {
|
if (state.currentReader.type === "ebook") {
|
||||||
|
// Use dynamic page calculation if available
|
||||||
|
if (pageCalculationResult) {
|
||||||
|
const container = document.getElementById("reader-content");
|
||||||
|
const viewportHeight = window.innerHeight;
|
||||||
|
currentPage = getCurrentPageFromScroll(
|
||||||
|
pageCalculationResult,
|
||||||
|
state.currentReader.currentSpineIndex,
|
||||||
|
container?.scrollTop || 0,
|
||||||
|
viewportHeight,
|
||||||
|
);
|
||||||
|
totalPages = pageCalculationResult.totalPages;
|
||||||
|
} else {
|
||||||
currentPage = state.currentReader.currentSpineIndex + 1;
|
currentPage = state.currentReader.currentSpineIndex + 1;
|
||||||
totalPages = state.currentReader.cif.spine.length;
|
totalPages = state.currentReader.cif.spine.length;
|
||||||
|
}
|
||||||
} else if (state.currentReader.type === "pdf") {
|
} else if (state.currentReader.type === "pdf") {
|
||||||
totalPages = state.readerMetadata.total_pages || 0;
|
totalPages = state.readerMetadata.total_pages || 0;
|
||||||
currentPage = state.currentReader.currentPage;
|
currentPage = state.currentReader.currentPage;
|
||||||
|
|||||||
@@ -9,11 +9,19 @@ export async function updateReadingProgress(
|
|||||||
mediaItemId: string,
|
mediaItemId: string,
|
||||||
progress: ReadingProgress,
|
progress: ReadingProgress,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
|
if (!mediaItemId || mediaItemId === "undefined") {
|
||||||
|
console.warn("Skipping progress update - no valid mediaItemId");
|
||||||
|
return;
|
||||||
|
}
|
||||||
const response = await apiPut(
|
const response = await apiPut(
|
||||||
`/media-items/${mediaItemId}/progress`,
|
`/media-items/${mediaItemId}/progress`,
|
||||||
progress,
|
progress,
|
||||||
);
|
);
|
||||||
await response.json();
|
if (!response.ok) {
|
||||||
|
const errorText = await response.text();
|
||||||
|
console.warn("Progress update failed:", response.status, errorText);
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getChapterNavigation(chapters: any[]) {
|
export function getChapterNavigation(chapters: any[]) {
|
||||||
|
|||||||
Reference in New Issue
Block a user