Whoops, condition wrong way around

This commit is contained in:
mrjvs 2023-10-21 05:11:11 +02:00
parent 32f031ab23
commit 76073043bf
4 changed files with 37 additions and 11 deletions

View File

@ -19,7 +19,7 @@ function ThumbnailDisplay(props: { at: number }) {
}, [thumbnailImages, props.at]); }, [thumbnailImages, props.at]);
if (!currentThumbnail) return null; if (!currentThumbnail) return null;
return <img src={currentThumbnail.data} className="h-12" />; return <img src={currentThumbnail.data} className="h-12 -translate-x-1/2" />;
} }
function useMouseHoverPosition(barRef: RefObject<HTMLDivElement>) { function useMouseHoverPosition(barRef: RefObject<HTMLDivElement>) {
@ -72,8 +72,6 @@ export function ProgressBar() {
setDraggingTime((dragPercentage / 100) * duration); setDraggingTime((dragPercentage / 100) * duration);
}, [setDraggingTime, duration, dragPercentage]); }, [setDraggingTime, duration, dragPercentage]);
const mousePosition = Math.floor(dragPercentage * duration);
return ( return (
<div className="w-full relative"> <div className="w-full relative">
<div className="top-0 absolute inset-x-0"> <div className="top-0 absolute inset-x-0">
@ -84,7 +82,7 @@ export function ProgressBar() {
left: `${mousePos}%`, left: `${mousePos}%`,
}} }}
> >
<ThumbnailDisplay at={mousePosition} /> <ThumbnailDisplay at={Math.floor((mousePos / 100) * duration)} />
</div> </div>
) : null} ) : null}
</div> </div>

View File

@ -74,6 +74,7 @@ class ThumnbnailWorker {
this.canvasEl.height this.canvasEl.height
); );
const imgUrl = this.canvasEl.toDataURL(); const imgUrl = this.canvasEl.toDataURL();
if (this.interrupted) return;
this.cb({ this.cb({
at, at,
data: imgUrl, data: imgUrl,
@ -84,13 +85,20 @@ class ThumnbnailWorker {
const vid = this.videoEl; const vid = this.videoEl;
if (!vid) return; if (!vid) return;
await this.initVideo(); await this.initVideo();
if (this.interrupted) return;
await this.takeSnapshot(vid.duration / 2); // TODO make a queue based on refinement algorithm
const queue = [0.5, 0.25, 0.75];
for (let i = 0; i < queue.length; i += 1) {
if (this.interrupted) return;
await this.takeSnapshot(vid.duration * queue[i]);
}
} }
} }
export function ThumbnailScraper() { export function ThumbnailScraper() {
const addImage = usePlayerStore((s) => s.thumbnails.addImage); const addImage = usePlayerStore((s) => s.thumbnails.addImage);
const meta = usePlayerStore((s) => s.meta);
const source = usePlayerStore((s) => s.source); const source = usePlayerStore((s) => s.source);
const workerRef = useRef<ThumnbnailWorker | null>(null); const workerRef = useRef<ThumnbnailWorker | null>(null);
@ -102,8 +110,6 @@ export function ThumbnailScraper() {
}); });
}, [source]); }, [source]);
// TODO stop worker on meta change
// start worker with the stream // start worker with the stream
useEffect(() => { useEffect(() => {
// dont interrupt existing working // dont interrupt existing working
@ -119,9 +125,25 @@ export function ThumbnailScraper() {
// destroy worker on unmount // destroy worker on unmount
useEffect(() => { useEffect(() => {
return () => { return () => {
if (workerRef.current) workerRef.current.destroy(); if (workerRef.current) {
workerRef.current.destroy();
workerRef.current = null;
}
}; };
}, []); }, []);
// if targeted meta changes, abort the scraper
const serializedMeta = JSON.stringify({
id: meta?.tmdbId,
ep: meta?.episode?.tmdbId,
se: meta?.season?.tmdbId,
});
useEffect(() => {
if (workerRef.current) {
workerRef.current.destroy();
workerRef.current = null;
}
}, [serializedMeta]);
return null; return null;
} }

View File

@ -105,6 +105,9 @@ export const createDisplaySlice: MakeSlice<DisplaySlice> = (set, get) => ({
set((s) => { set((s) => {
s.status = playerStatus.IDLE; s.status = playerStatus.IDLE;
s.meta = null; s.meta = null;
s.thumbnails.images = [];
s.progress.time = 0;
s.progress.duration = 0;
}); });
}, },
}); });

View File

@ -28,7 +28,7 @@ export function nearestImageAt(
// no images, early return // no images, early return
if (images.length === 0) return null; if (images.length === 0) return null;
const indexPastTimestamp = images.findIndex((v) => v.at < at); const indexPastTimestamp = images.findIndex((v) => v.at > at);
// no image found past timestamp, so last image must be closest // no image found past timestamp, so last image must be closest
if (indexPastTimestamp === -1) if (indexPastTimestamp === -1)
@ -76,13 +76,14 @@ export const createThumbnailSlice: MakeSlice<ThumbnailSlice> = (set, get) => ({
addImage(img) { addImage(img) {
const store = get(); const store = get();
const exactOrPastImageIndex = store.thumbnails.images.findIndex( const exactOrPastImageIndex = store.thumbnails.images.findIndex(
(v) => v.at <= img.at (v) => v.at >= img.at
); );
// not found past or exact, so just append to the end // not found past or exact, so just append to the end
if (exactOrPastImageIndex === -1) { if (exactOrPastImageIndex === -1) {
set((s) => { set((s) => {
s.thumbnails.images.push(img); s.thumbnails.images.push(img);
s.thumbnails.images = [...s.thumbnails.images];
}); });
return; return;
} }
@ -93,6 +94,7 @@ export const createThumbnailSlice: MakeSlice<ThumbnailSlice> = (set, get) => ({
if (exactOrPastImage.at === img.at) { if (exactOrPastImage.at === img.at) {
set((s) => { set((s) => {
s.thumbnails.images[exactOrPastImageIndex] = img; s.thumbnails.images[exactOrPastImageIndex] = img;
s.thumbnails.images = [...s.thumbnails.images];
}); });
return; return;
} }
@ -100,6 +102,7 @@ export const createThumbnailSlice: MakeSlice<ThumbnailSlice> = (set, get) => ({
// found one past, insert right before it // found one past, insert right before it
set((s) => { set((s) => {
s.thumbnails.images.splice(exactOrPastImageIndex, 0, img); s.thumbnails.images.splice(exactOrPastImageIndex, 0, img);
s.thumbnails.images = [...s.thumbnails.images];
}); });
}, },
}, },