caelum0x d56639a594
Some checks failed
build-images / web (push) Has been cancelled
vision2026-ci / Python SDK resource tests (push) Has been cancelled
build-images / api (push) Has been cancelled
build-images / worker-gpu (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (comfy-workflow-schema) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (effects-core) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (eventbus-types) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (growth-analytics) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (provider-adapter-kit) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (publishing-connectors) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (render-core) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (shot-template-schema) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (trends-schema) (push) Has been cancelled
vision2026-ci / Desktop engine crates (test + clippy + fmt) (push) Has been cancelled
codeql / Analyze (javascript-typescript) (push) Has been cancelled
codeql / Analyze (python) (push) Has been cancelled
perf / API hot-path micro-smoke (budgeted) (push) Has been cancelled
perf / Desktop engine crates (release build check) (push) Has been cancelled
vision2026-ci / API phase modules (ruff + pytest) (push) Has been cancelled
vision2026-ci / Alembic upgrade + drift check (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (caption-engine) (push) Has been cancelled
Menivor — open source release (MIT), free to use and fork
Paid, hosted product at menivor.com, now retired as a company project.
Fresh squashed history of the cleaned working tree (secret-scanned,
no .env, no customer data, no outreach logs). Built on
MoneyPrinterTurbo (MIT, harry0703) — see NOTICE.
2026-12-04 02:10:26 +03:00

82 lines
2.7 KiB
TypeScript

"use client";
import * as React from "react";
export interface LazyVideoOptions {
/** The muted loop attached on first view. */
readonly src: string;
/** Same-origin copy used when `src` fails to load. */
readonly fallbackSrc?: string;
readonly fallbackPoster?: string;
readonly rootMargin?: string;
readonly threshold?: number;
}
export interface LazyVideo<T extends HTMLVideoElement> {
readonly ref: React.RefObject<T | null>;
/** True while the video is on screen. */
readonly visible: React.RefObject<boolean>;
}
function reducedMotion(): boolean {
return Boolean(window.matchMedia?.("(prefers-reduced-motion: reduce)").matches);
}
/**
* Poster first, source on view: the `<video>` renders with only its poster
* and no `src`, so nothing downloads until it scrolls near the screen. On the
* first intersection the loop is attached; afterwards it plays while visible
* and pauses off screen. Never autoplays under reduced motion (the poster
* stays). If the media domain fails, the same-origin copy takes over.
*/
export function useInViewPlayback<T extends HTMLVideoElement>(options: LazyVideoOptions): LazyVideo<T> {
const ref = React.useRef<T | null>(null);
const visible = React.useRef(false);
const { src, fallbackSrc, fallbackPoster, rootMargin = "160px", threshold = 0.2 } = options;
React.useEffect(() => {
const video = ref.current;
if (!video) return;
const attach = () => {
if (!video.getAttribute("src")) video.setAttribute("src", src);
};
const play = () => {
if (reducedMotion()) return;
video.play()?.catch?.(() => {
// Autoplay can be refused (data saver, low power); the poster stays.
});
};
const onError = () => {
const current = video.getAttribute("src") ?? "";
if (!fallbackSrc || current === fallbackSrc || current !== src) return;
video.setAttribute("src", fallbackSrc);
if (fallbackPoster) video.setAttribute("poster", fallbackPoster);
if (visible.current) play();
};
video.addEventListener("error", onError);
if (typeof IntersectionObserver === "undefined") {
attach();
return () => video.removeEventListener("error", onError);
}
const observer = new IntersectionObserver(
([entry]) => {
visible.current = Boolean(entry?.isIntersecting);
if (visible.current) {
attach();
play();
} else {
video.pause();
}
},
{ rootMargin, threshold },
);
observer.observe(video);
return () => {
observer.disconnect();
video.removeEventListener("error", onError);
};
}, [src, fallbackSrc, fallbackPoster, rootMargin, threshold]);
return { ref, visible };
}