Fix reading session timeline long sessions bug, tooltip perf, and ISO week mismatch (#2848)

This commit is contained in:
ACX
2026-02-22 12:46:33 -07:00
committed by GitHub
parent 4d0f529b76
commit d2e6722f98
4 changed files with 34 additions and 8 deletions
@@ -126,7 +126,7 @@ public class ReadingSessionService {
Long userId = authenticatedUser.getId(); Long userId = authenticatedUser.getId();
LocalDate date = LocalDate.of(year, 1, 1) LocalDate date = LocalDate.of(year, 1, 1)
.with(WeekFields.of(DayOfWeek.MONDAY, 1).weekOfYear(), week); .with(WeekFields.ISO.weekOfYear(), week);
LocalDateTime startOfWeek = date.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)).atStartOfDay(); LocalDateTime startOfWeek = date.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)).atStartOfDay();
LocalDateTime endOfWeek = date.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY)).plusDays(1).atStartOfDay(); LocalDateTime endOfWeek = date.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY)).plusDays(1).atStartOfDay();
@@ -84,7 +84,7 @@
[style.width.%]="session.width" [style.width.%]="session.width"
[style.top]="session.totalLevels > 1 ? 'calc(' + session.level + ' / ' + session.totalLevels + ' * 100% + ' + session.level * 2 + 'px)' : '0'" [style.top]="session.totalLevels > 1 ? 'calc(' + session.level + ' / ' + session.totalLevels + ' * 100% + ' + session.level * 2 + 'px)' : '0'"
[style.height]="session.totalLevels > 1 ? 'calc((1 / ' + session.totalLevels + ' * 100%) - ' + (session.totalLevels - 1) * 2 / session.totalLevels + 'px)' : '100%'" [style.height]="session.totalLevels > 1 ? 'calc((1 / ' + session.totalLevels + ' * 100%) - ' + (session.totalLevels - 1) * 2 / session.totalLevels + 'px)' : '100%'"
[pTooltip]="getTooltipContent(session)" [pTooltip]="session.tooltipContent"
[escape]="false" [escape]="false"
tooltipPosition="top" tooltipPosition="top"
tooltipStyleClass="session-timeline-tooltip" tooltipStyleClass="session-timeline-tooltip"
@@ -42,6 +42,7 @@ interface TimelineSession {
bookType: BookType; bookType: BookType;
level: number; level: number;
totalLevels: number; totalLevels: number;
tooltipContent: string;
} }
interface DayTimeline { interface DayTimeline {
@@ -319,7 +320,7 @@ export class ReadingSessionTimelineComponent implements OnInit {
width = 0.5; width = 0.5;
} }
return { const timelineSession: TimelineSession = {
startHour, startHour,
startMinute, startMinute,
endHour, endHour,
@@ -331,8 +332,11 @@ export class ReadingSessionTimelineComponent implements OnInit {
bookId: session.bookId, bookId: session.bookId,
bookType: session.bookType, bookType: session.bookType,
level, level,
totalLevels totalLevels,
tooltipContent: ''
}; };
timelineSession.tooltipContent = this.buildTooltipContent(timelineSession);
return timelineSession;
} }
public formatTime(hour: number, minute: number): string { public formatTime(hour: number, minute: number): string {
@@ -375,7 +379,7 @@ export class ReadingSessionTimelineComponent implements OnInit {
return this.urlHelperService.getDirectThumbnailUrl(bookId); return this.urlHelperService.getDirectThumbnailUrl(bookId);
} }
public getTooltipContent(session: TimelineSession): string { private buildTooltipContent(session: TimelineSession): string {
return ` return `
<div class="session-tooltip-content"> <div class="session-tooltip-content">
<div class="session-tooltip-cover"> <div class="session-tooltip-cover">
@@ -30,6 +30,7 @@ export class ReadingSessionService {
private currentSession: ReadingSession | null = null; private currentSession: ReadingSession | null = null;
private idleTimer: ReturnType<typeof setTimeout> | null = null; private idleTimer: ReturnType<typeof setTimeout> | null = null;
private activitySubscription: Subscription | null = null; private activitySubscription: Subscription | null = null;
private hiddenAt: number | null = null;
private readonly IDLE_TIMEOUT_MS = 5 * 60 * 1000; private readonly IDLE_TIMEOUT_MS = 5 * 60 * 1000;
private readonly MIN_SESSION_DURATION_SECONDS = 30; private readonly MIN_SESSION_DURATION_SECONDS = 30;
@@ -49,10 +50,28 @@ export class ReadingSessionService {
document.addEventListener('visibilitychange', () => { document.addEventListener('visibilitychange', () => {
if (document.hidden && this.currentSession) { if (document.hidden && this.currentSession) {
this.log('Tab hidden, pausing session'); this.log('Tab hidden, pausing session');
this.hiddenAt = Date.now();
this.pauseIdleDetection(); this.pauseIdleDetection();
} else if (!document.hidden && this.currentSession) { } else if (!document.hidden && this.currentSession) {
this.log('Tab visible, resuming session'); if (this.hiddenAt && Date.now() - this.hiddenAt > this.IDLE_TIMEOUT_MS) {
this.resumeIdleDetection(); this.log('Tab was hidden too long, ending session');
this.stopIdleDetection();
const cappedEndTime = new Date(this.hiddenAt + this.IDLE_TIMEOUT_MS);
this.currentSession.endTime = cappedEndTime;
const durationMs = cappedEndTime.getTime() - this.currentSession.startTime.getTime();
this.currentSession.durationSeconds = Math.floor(durationMs / 1000);
if (this.currentSession.startProgress != null && this.currentSession.endProgress != null) {
this.currentSession.progressDelta = this.currentSession.endProgress - this.currentSession.startProgress;
}
if (this.currentSession.durationSeconds >= this.MIN_SESSION_DURATION_SECONDS) {
this.sendSessionToBackend(this.currentSession);
}
this.currentSession = null;
} else {
this.log('Tab visible, resuming session');
this.resumeIdleDetection();
}
this.hiddenAt = null;
} }
}); });
} }
@@ -124,7 +143,10 @@ export class ReadingSessionService {
return; return;
} }
const endTime = new Date(); let endTime = new Date();
if (this.hiddenAt && endTime.getTime() - this.hiddenAt > this.IDLE_TIMEOUT_MS) {
endTime = new Date(this.hiddenAt + this.IDLE_TIMEOUT_MS);
}
const durationMs = endTime.getTime() - this.currentSession.startTime.getTime(); const durationMs = endTime.getTime() - this.currentSession.startTime.getTime();
const durationSeconds = Math.floor(durationMs / 1000); const durationSeconds = Math.floor(durationMs / 1000);