Files
Vault/hooks/useVideo.ts
T
twotalesanimation 81ad7e4ea9 Initial commit
2026-06-11 10:46:09 +02:00

48 lines
1.1 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) throw new Error(`HTTP ${res.status}`);
const json = await res.json();
if (!cancelled) {
setVideo(json.video ?? null);
setNext(json.next ?? null);
}
})
.catch((err) => {
if (!cancelled) setError(err);
})
.finally(() => {
if (!cancelled) setIsLoading(false);
});
return () => {
cancelled = true;
};
}, [id]);
return { video, next, isLoading, error };
}