diff --git a/src/layouts/explorer/explorer.tsx b/src/layouts/explorer/explorer.tsx index cf63eb12..5e1941cb 100644 --- a/src/layouts/explorer/explorer.tsx +++ b/src/layouts/explorer/explorer.tsx @@ -2,7 +2,6 @@ import { useGetContents } from '@/services/hooks/content/get-content.hook' import { useRef, useState, useEffect } from 'react' import Analytics from '@/analytics' import { getFaviconFromUrl } from '@/common/utils/icon' -import { HiOutlineLightBulb } from 'react-icons/hi2' interface LinkItem { name: string @@ -27,10 +26,12 @@ export function ExplorerContent() { const scrollContainerRef = useRef(null) useEffect(() => { + if (!catalogData?.contents || !scrollContainerRef.current) return + const observerOptions = { root: scrollContainerRef.current, - rootMargin: '-10% 0px -80% 0px', - threshold: 0, + rootMargin: '0px 0px -40% 0px', + threshold: 0.1, } const observerCallback = (entries: IntersectionObserverEntry[]) => { @@ -42,43 +43,48 @@ export function ExplorerContent() { } const observer = new IntersectionObserver(observerCallback, observerOptions) - Object.values(categoryRefs.current).forEach((div) => { + + const currentRefs = categoryRefs.current + Object.values(currentRefs).forEach((div) => { if (div) observer.observe(div) }) + return () => observer.disconnect() - }, [catalogData]) + }, [catalogData?.contents]) const scrollToCategory = (id: string) => { setActiveCategory(id) - categoryRefs.current[id]?.scrollIntoView({ - behavior: 'smooth', - block: 'start', - }) + const element = categoryRefs.current[id] + if (element) { + element.scrollIntoView({ + behavior: 'smooth', + block: 'start', + }) + } Analytics.event('explorer_click_category') } return (
-