Open source CapCut alternative.
TypeScript
fix(site): media previews observe late-mounting elements
David Le274af71a0745
diff --git a/site/src/cut/hooks/useInView.ts b/site/src/cut/hooks/useInView.tsindex 874185e0..3ad7ae2c 100644--- a/site/src/cut/hooks/useInView.ts+++ b/site/src/cut/hooks/useInView.ts@@ -1,29 +1,38 @@"use client";-import { useEffect, useRef, useState, type RefObject } from "react";+import { useCallback, useRef, useState } from "react";/*** Whether the element has been scrolled into view at least once. Media* previews mount wherever history renders — chat scrollback, attachment* chips, render history — so their `src` gates on this: a reload fetches only* what's actually on screen instead of every file the project ever made.+ *+ * The ref is a callback ref, so the observer attaches whenever the element+ * actually mounts — including elements that appear only after a later render+ * (a job tile that swaps from its status row to a preview when the render+ * lands). An effect keyed on first render would miss those forever.*/-export function useInView<T extends HTMLElement>(): [RefObject<T | null>, boolean] {- const ref = useRef<T>(null);+export function useInView<T extends HTMLElement>(): [(node: T | null) => void, boolean] {const [seen, setSeen] = useState(false);- useEffect(() => {- if (seen) return;- const el = ref.current;- if (!el) return;- const io = new IntersectionObserver(+ const io = useRef<IntersectionObserver | null>(null);+ const ref = useCallback((node: T | null) => {+ io.current?.disconnect();+ io.current = null;+ if (!node) return;+ const obs = new IntersectionObserver((entries) => {- if (entries.some((e) => e.isIntersecting)) setSeen(true);+ if (entries.some((e) => e.isIntersecting)) {+ obs.disconnect();+ if (io.current === obs) io.current = null;+ setSeen(true);+ }},// Start loading just before the tile scrolls in, so it's ready on arrival.{ rootMargin: "150px" });- io.observe(el);- return () => io.disconnect();- }, [seen]);+ obs.observe(node);+ io.current = obs;+ }, []);return [ref, seen];}