1- import { memo , useCallback , useEffect , useState , useRef } from "react" ;
1+ import { memo , useCallback , useMemo , useRef , useState } from "react" ;
22import { useMountEffect } from "../../hooks/useMountEffect" ;
3+ import { useThumbnailLease } from "../../hooks/useThumbnailLease" ;
4+ import { createThumbnailKey , type ThumbnailPriority } from "../lib/thumbnailScheduler" ;
5+ import { TIMELINE_VIEWPORT_BUDGETS } from "../lib/timelineViewportBudgets" ;
6+ import { computeThumbnailStrip , probeImageAspect } from "./thumbnailUtils" ;
37
48interface CompositionThumbnailProps {
59 previewUrl : string ;
@@ -11,6 +15,10 @@ interface CompositionThumbnailProps {
1115 duration ?: number ;
1216 width ?: number ;
1317 height ?: number ;
18+ projectId ?: string ;
19+ sessionEpoch ?: number ;
20+ priority ?: ThumbnailPriority ;
21+ rich ?: boolean ;
1422}
1523
1624const CLIP_HEIGHT = 66 ;
@@ -34,9 +42,8 @@ export function buildCompositionThumbnailUrl({
3442 const thumbnailBase = previewUrl
3543 . replace ( "/preview/comp/" , "/thumbnail/" )
3644 . replace ( / \/ p r e v i e w $ / , "/thumbnail/index.html" ) ;
37- const midTime = seekTime + duration / 2 ;
3845 const thumbnailUrl = new URL ( thumbnailBase , origin ) ;
39- thumbnailUrl . searchParams . set ( "t" , midTime . toFixed ( 2 ) ) ;
46+ thumbnailUrl . searchParams . set ( "t" , ( seekTime + duration / 2 ) . toFixed ( 2 ) ) ;
4047 thumbnailUrl . searchParams . set ( "v" , THUMBNAIL_URL_VERSION ) ;
4148 if ( selector ) {
4249 thumbnailUrl . searchParams . set ( "selector" , selector ) ;
@@ -47,6 +54,29 @@ export function buildCompositionThumbnailUrl({
4754 return thumbnailUrl . toString ( ) ;
4855}
4956
57+ async function loadCompositionImage ( url : string , signal : AbortSignal ) {
58+ const response = await fetch ( url , { signal } ) ;
59+ if ( ! response . ok ) throw new Error ( `Composition thumbnail failed (${ response . status } )` ) ;
60+ const blob = await response . blob ( ) ;
61+ if ( signal . aborted ) throw new DOMException ( "Aborted" , "AbortError" ) ;
62+ const objectUrl = URL . createObjectURL ( blob ) ;
63+ try {
64+ const aspect = await probeImageAspect ( objectUrl , signal ) ;
65+ return {
66+ value : { kind : "image" as const , url : objectUrl , aspect } ,
67+ weight :
68+ TIMELINE_VIEWPORT_BUDGETS . posterMaxPhysicalWidth *
69+ TIMELINE_VIEWPORT_BUDGETS . posterMaxPhysicalHeight *
70+ 4 ,
71+ dispose : ( ) => URL . revokeObjectURL ( objectUrl ) ,
72+ } ;
73+ } catch ( error ) {
74+ URL . revokeObjectURL ( objectUrl ) ;
75+ throw error ;
76+ }
77+ }
78+
79+ /** Server-rendered composition poster, deduplicated and budgeted by project/session. */
5080export const CompositionThumbnail = memo ( function CompositionThumbnail ( {
5181 previewUrl,
5282 label,
@@ -55,31 +85,12 @@ export const CompositionThumbnail = memo(function CompositionThumbnail({
5585 selectorIndex,
5686 seekTime = 2 ,
5787 duration = 5 ,
88+ projectId = previewUrl ,
89+ sessionEpoch = 0 ,
90+ priority = "visible" ,
5891} : CompositionThumbnailProps ) {
5992 const [ containerWidth , setContainerWidth ] = useState ( 0 ) ;
60- const [ loaded , setLoaded ] = useState ( false ) ;
61- const [ aspect , setAspect ] = useState ( 16 / 9 ) ;
62- const roRef = useRef < ResizeObserver | null > ( null ) ;
63-
64- const setContainerRef = useCallback ( ( el : HTMLDivElement | null ) => {
65- roRef . current ?. disconnect ( ) ;
66- if ( ! el ) return ;
67-
68- const measured = el . parentElement ?. clientWidth || el . clientWidth ;
69- // fallow-ignore-next-line code-duplication
70- setContainerWidth ( measured ) ;
71-
72- const target = el . parentElement || el ;
73- roRef . current = new ResizeObserver ( ( [ entry ] ) => {
74- setContainerWidth ( entry . contentRect . width ) ;
75- } ) ;
76- roRef . current . observe ( target ) ;
77- } , [ ] ) ;
78-
79- useMountEffect ( ( ) => ( ) => {
80- roRef . current ?. disconnect ( ) ;
81- } ) ;
82-
93+ const observerRef = useRef < ResizeObserver | null > ( null ) ;
8394 const url = buildCompositionThumbnailUrl ( {
8495 previewUrl,
8596 seekTime,
@@ -88,52 +99,56 @@ export const CompositionThumbnail = memo(function CompositionThumbnail({
8899 selectorIndex,
89100 origin : window . location . origin ,
90101 } ) ;
102+ const request = useMemo (
103+ ( ) => ( {
104+ key : createThumbnailKey ( { kind : "composition" , url } ) ,
105+ projectId,
106+ sessionEpoch,
107+ kind : "composition" as const ,
108+ priority,
109+ rich : true ,
110+ load : ( signal : AbortSignal ) => loadCompositionImage ( url , signal ) ,
111+ } ) ,
112+ [ priority , projectId , sessionEpoch , url ] ,
113+ ) ;
114+ const snapshot = useThumbnailLease ( request ) ;
115+ const value =
116+ snapshot . status === "ready" && snapshot . value . kind === "image" ? snapshot . value : null ;
117+ const { frameW, frameCount } = computeThumbnailStrip (
118+ containerWidth ,
119+ value ?. aspect ?? 16 / 9 ,
120+ CLIP_HEIGHT ,
121+ 48 ,
122+ ) ;
91123
92- // Probe outside React's DOM and explicitly abort on URL changes/unmount. A
93- // hidden React <img> keeps its Fiber reachable from Blink's pending-activity
94- // queue while a request is unresolved.
95- // eslint-disable-next-line no-restricted-syntax
96- useEffect ( ( ) => {
97- let cancelled = false ;
98- setLoaded ( false ) ;
99- const probe = new Image ( ) ;
100- probe . onload = ( ) => {
101- if ( cancelled ) return ;
102- if ( probe . naturalWidth > 0 && probe . naturalHeight > 0 ) {
103- setAspect ( probe . naturalWidth / probe . naturalHeight ) ;
104- }
105- setLoaded ( true ) ;
106- } ;
107- probe . onerror = ( ) => {
108- if ( ! cancelled ) setLoaded ( false ) ;
109- } ;
110- probe . src = url ;
111- return ( ) => {
112- cancelled = true ;
113- probe . onload = null ;
114- probe . onerror = null ;
115- probe . src = "" ;
116- } ;
117- } , [ url ] ) ;
124+ const setContainerRef = useCallback ( ( element : HTMLDivElement | null ) => {
125+ observerRef . current ?. disconnect ( ) ;
126+ if ( ! element ) return ;
127+ const target = element . parentElement ?? element ;
128+ setContainerWidth ( target . clientWidth ) ;
129+ observerRef . current = new ResizeObserver ( ( [ entry ] ) =>
130+ setContainerWidth ( entry . contentRect . width ) ,
131+ ) ;
132+ observerRef . current . observe ( target ) ;
133+ } , [ ] ) ;
118134
119- const frameW = Math . max ( 48 , Math . round ( CLIP_HEIGHT * aspect ) ) ;
120- const frameCount = containerWidth > 0 ? Math . max ( 1 , Math . ceil ( containerWidth / frameW ) ) : 1 ;
135+ useMountEffect ( ( ) => ( ) => observerRef . current ?. disconnect ( ) ) ;
121136
122137 return (
123138 < div ref = { setContainerRef } className = "absolute inset-0 overflow-hidden" >
124- { loaded && (
139+ { value && (
125140 < div
126141 className = "absolute inset-0 flex"
127142 style = { { animation : "hf-thumb-fade 200ms ease-out" , mixBlendMode : "lighten" } }
128143 >
129- { Array . from ( { length : frameCount } ) . map ( ( _ , i ) => (
144+ { Array . from ( { length : frameCount } , ( _ , index ) => (
130145 < div
131- key = { i }
146+ key = { index }
132147 className = "relative h-full flex-shrink-0 overflow-hidden"
133148 style = { { width : frameW } }
134149 >
135150 < img
136- src = { url }
151+ src = { value . url }
137152 alt = ""
138153 draggable = { false }
139154 className = "absolute inset-0 h-full w-full object-cover"
@@ -143,14 +158,16 @@ export const CompositionThumbnail = memo(function CompositionThumbnail({
143158 ) ) }
144159 </ div >
145160 ) }
146-
161+ { snapshot . status === "loading" && (
162+ < div className = "absolute inset-0 animate-pulse bg-white/[0.035]" />
163+ ) }
147164 { label && (
148- < div className = "absolute left-3 top -0 bottom-0 flex items-center" style = { { zIndex : 10 } } >
165+ < div className = "absolute inset-y -0 left-3 z-10 flex items-center" >
149166 < span
150167 className = "block max-w-full truncate text-[10px] font-semibold leading-none"
151168 style = { {
152169 color : labelColor ,
153- textShadow : loaded ? "0 1px 4px rgba(0,0,0,0.9), 0 0 8px rgba(0,0,0,0.6)" : "none" ,
170+ textShadow : value ? "0 1px 4px rgba(0,0,0,0.9), 0 0 8px rgba(0,0,0,0.6)" : "none" ,
154171 } }
155172 >
156173 { label }
0 commit comments