Files
Vault/hooks/useVideo.ts
T
twotalesanimation a834caa511
Deploy Vault CMS / deploy (push) Failing after 38s
Fix watch tracking, video unlocking and access-control gaps
Watch progress and unlocking:
- Player samples the playhead and reports the ranges actually played,
  so skipped sections earn no credit and early plays are not lost
- Server merges ranges into compact segment rows; progress never regresses
- Single 80% unlock rule enforced server-side (lib/watch-progress.ts);
  the Unlock button and automatic unlock now agree
- Lock state and progress bar update live from each progress save
- Playlist is derived from the video, so unlocks work for shared
  playlists and for links from watch history and liked videos
- First/previous video decided by playlist position, not index numbers

Access control:
- Video API and HLS/MP4 routes require a signed-in viewer who is
  enrolled and has the video unlocked; thumbnails require sign-in
- Enrolment sync reads course codes from the allow-list, not the request
- /api/admin/meta is admin-only
- Account status, role and levels are re-checked from the database
  during a session, so deactivated students lose access within a minute

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-11 16:58:07 +02:00

57 lines
1.4 KiB
TypeScript

'use client';
import { useEffect, useState } from 'react';
type Video = any;
export function useVideo(id?: string) {
const [video, setVideo] = useState<Video | null>(null);
const [next, setNext] = useState<Video | null>(null);
const [isLoading, setIsLoading] = useState<boolean>(!!id);
const [error, setError] = useState<any>(null);
useEffect(() => {
if (!id) {
setVideo(null);
setNext(null);
setIsLoading(false);
setError(null);
return;
}
let cancelled = false;
setIsLoading(true);
setError(null);
fetch(`/api/videos/${id}`)
.then(async (res) => {
if (!res.ok) {
// The server explains refusals (e.g. which video to finish first).
const body = await res.json().catch(() => null);
throw new Error(body?.error ?? `HTTP ${res.status}`);
}
const json = await res.json();
if (!cancelled) {
setVideo(json.video ?? null);
setNext(json.next ?? null);
}
})
.catch((err) => {
if (!cancelled) {
// Never leave the previous video on screen under a new address.
setVideo(null);
setNext(null);
setError(err);
}
})
.finally(() => {
if (!cancelled) setIsLoading(false);
});
return () => {
cancelled = true;
};
}, [id]);
return { video, next, isLoading, error };
}