@@ -365,6 +365,31 @@ export const useSpaceSummary = (space: Room): {
365365 return { loading, rooms, hierarchy, loadMore } ;
366366} ;
367367
368+ const useIntersectionObserver = ( callback : ( ) => void ) => {
369+ const handleObserver = ( entries : IntersectionObserverEntry [ ] ) => {
370+ const target = entries [ 0 ] ;
371+ if ( target . isIntersecting ) {
372+ callback ( ) ;
373+ }
374+ } ;
375+
376+ const observerRef = useRef < IntersectionObserver > ( ) ;
377+ return ( element : HTMLDivElement ) => {
378+ if ( observerRef . current ) {
379+ observerRef . current . disconnect ( ) ;
380+ } else if ( element ) {
381+ observerRef . current = new IntersectionObserver ( handleObserver , {
382+ root : element . parentElement ,
383+ rootMargin : "0px 0px 600px 0px" ,
384+ } ) ;
385+ }
386+
387+ if ( observerRef . current && element ) {
388+ observerRef . current . observe ( element ) ;
389+ }
390+ } ;
391+ } ;
392+
368393const SpaceHierarchy = ( {
369394 space,
370395 initialText = "" ,
@@ -408,28 +433,7 @@ const SpaceHierarchy = ({
408433 const [ removing , setRemoving ] = useState ( false ) ;
409434 const [ saving , setSaving ] = useState ( false ) ;
410435
411- const handleObserver = ( entries : IntersectionObserverEntry [ ] ) => {
412- const target = entries [ 0 ] ;
413- if ( target . isIntersecting ) {
414- loadMore ( ) ;
415- }
416- } ;
417-
418- const observerRef = useRef < IntersectionObserver > ( ) ;
419- const loaderRef = ( element : HTMLDivElement ) => {
420- if ( observerRef . current ) {
421- observerRef . current . disconnect ( ) ;
422- } else if ( element ) {
423- observerRef . current = new IntersectionObserver ( handleObserver , {
424- root : element . parentElement ,
425- rootMargin : "0px 0px 600px 0px" ,
426- } ) ;
427- }
428-
429- if ( observerRef . current && element ) {
430- observerRef . current . observe ( element ) ;
431- }
432- } ;
436+ const loaderRef = useIntersectionObserver ( loadMore ) ;
433437
434438 if ( ! loading && hierarchy . noSupport ) {
435439 return < p > { _t ( "Your server does not support showing space hierarchies." ) } </ p > ;
0 commit comments