Load watch progress in one request on dashboard and player
Deploy Vault CMS / deploy (push) Failing after 5s
Deploy Vault CMS / deploy (push) Failing after 5s
Add GET /api/progress/all, which returns the signed-in user's progress and merged watch segments for every video (or one playlist) using two queries. - Dashboard: replaces two sequential requests per video with a single request on load - Player sidebar: replaces one request per playlist video with a single request per playlist Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
+19
-17
@@ -303,32 +303,34 @@ React.useEffect(() => {
|
||||
};
|
||||
}, [videoId]);
|
||||
|
||||
// Fetch segments for all playlist videos
|
||||
// Fetch watched segments for every video in the playlist in one request
|
||||
const loadedPlaylistId: string | undefined = playlist?.id;
|
||||
React.useEffect(() => {
|
||||
if (!playlist?.videos || playlist.videos.length === 0) return;
|
||||
if (!loadedPlaylistId) return;
|
||||
let cancelled = false;
|
||||
|
||||
const fetchPlaylistSegments = async () => {
|
||||
const segments: Record<string, WatchSegment[]> = {};
|
||||
|
||||
for (const video of playlist.videos) {
|
||||
try {
|
||||
const res = await fetch(`/api/progress/segments?videoId=${video.id}`);
|
||||
if (res.ok) {
|
||||
const data = await res.json();
|
||||
segments[video.id] = data.segments ?? [];
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(`Failed to fetch segments for video ${video.id}:`, err);
|
||||
try {
|
||||
const res = await fetch(`/api/progress/all?playlistId=${encodeURIComponent(loadedPlaylistId)}`);
|
||||
if (!res.ok || cancelled) return;
|
||||
const data = await res.json();
|
||||
const segments: Record<string, WatchSegment[]> = {};
|
||||
for (const [id, entry] of Object.entries<any>(data.progress ?? {})) {
|
||||
segments[id] = entry.segments ?? [];
|
||||
}
|
||||
// Keep anything newer that arrived from a progress save in the meantime.
|
||||
if (!cancelled) setPlaylistSegments((prev) => ({ ...segments, ...prev }));
|
||||
} catch (err) {
|
||||
console.error('Failed to fetch playlist progress:', err);
|
||||
}
|
||||
|
||||
// Keep anything newer that arrived from a progress save in the meantime.
|
||||
setPlaylistSegments((prev) => ({ ...segments, ...prev }));
|
||||
};
|
||||
|
||||
setPlaylistSegments({});
|
||||
fetchPlaylistSegments();
|
||||
}, [playlist?.videos]);
|
||||
return () => {
|
||||
cancelled = true;
|
||||
};
|
||||
}, [loadedPlaylistId]);
|
||||
|
||||
// Fetch user's VideoUnlock records
|
||||
React.useEffect(() => {
|
||||
|
||||
Reference in New Issue
Block a user