Initial open-source self-hosted Songs2YT edition
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useRef, useState } from "react";
|
||||
|
||||
type Options = {
|
||||
threshold?: number;
|
||||
rootMargin?: string;
|
||||
once?: boolean;
|
||||
};
|
||||
|
||||
export function useInView<T extends HTMLElement = HTMLDivElement>({
|
||||
threshold = 0.15,
|
||||
rootMargin = "0px 0px -40px 0px",
|
||||
once = true,
|
||||
}: Options = {}) {
|
||||
const ref = useRef<T>(null);
|
||||
const [inView, setInView] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
const el = ref.current;
|
||||
if (!el) return;
|
||||
|
||||
const observer = new IntersectionObserver(
|
||||
([entry]) => {
|
||||
if (entry.isIntersecting) {
|
||||
setInView(true);
|
||||
if (once) observer.unobserve(el);
|
||||
} else if (!once) {
|
||||
setInView(false);
|
||||
}
|
||||
},
|
||||
{ threshold, rootMargin },
|
||||
);
|
||||
|
||||
observer.observe(el);
|
||||
return () => observer.disconnect();
|
||||
}, [threshold, rootMargin, once]);
|
||||
|
||||
return { ref, inView };
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useState } from "react";
|
||||
|
||||
export function useMockupProgress(
|
||||
active: boolean,
|
||||
phaseOneMs = 3000,
|
||||
phaseTwoMs = 2000,
|
||||
) {
|
||||
const [phase, setPhase] = useState<0 | 1 | 2>(0);
|
||||
const [processed, setProcessed] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
if (!active) return;
|
||||
|
||||
const startFrame = requestAnimationFrame(() => setPhase(1));
|
||||
const midTimer = setTimeout(() => setPhase(2), phaseOneMs);
|
||||
const doneTimer = setTimeout(() => setProcessed(true), phaseOneMs + phaseTwoMs);
|
||||
|
||||
return () => {
|
||||
cancelAnimationFrame(startFrame);
|
||||
clearTimeout(midTimer);
|
||||
clearTimeout(doneTimer);
|
||||
};
|
||||
}, [active, phaseOneMs, phaseTwoMs]);
|
||||
|
||||
const transitionMs = phase === 1 ? phaseOneMs : phase === 2 ? phaseTwoMs : 0;
|
||||
|
||||
return { phase, processed, transitionMs };
|
||||
}
|
||||
Reference in New Issue
Block a user