dify/web/app/components/plugins/marketplace/intersection-line/hooks.ts

31 lines
1.1 KiB
TypeScript
Raw Normal View History

2024-10-12 12:46:29 +08:00
import { useEffect } from 'react'
2024-10-12 18:02:24 +08:00
import { usePluginPageContext } from '@/app/components/plugins/plugin-page/context'
import { useMarketplaceContext } from '@/app/components/plugins/marketplace/context'
2024-10-12 12:46:29 +08:00
export const useScrollIntersection = (
anchorRef: React.RefObject<HTMLDivElement>,
) => {
2024-10-12 18:02:24 +08:00
const containerRef = usePluginPageContext(v => v.containerRef)
const intersected = useMarketplaceContext(v => v.intersected)
const setIntersected = useMarketplaceContext(v => v.setIntersected)
2024-10-12 12:46:29 +08:00
useEffect(() => {
let observer: IntersectionObserver | undefined
2024-10-12 16:34:02 +08:00
if (containerRef?.current && anchorRef.current) {
2024-10-12 12:46:29 +08:00
observer = new IntersectionObserver((entries) => {
2024-10-12 16:34:02 +08:00
const isIntersecting = entries[0].isIntersecting
2024-10-12 18:02:24 +08:00
if (isIntersecting && !intersected)
setIntersected(true)
if (!isIntersecting && intersected)
setIntersected(false)
2024-10-12 12:46:29 +08:00
}, {
root: containerRef.current,
})
observer.observe(anchorRef.current)
}
return () => observer?.disconnect()
2024-10-12 18:02:24 +08:00
}, [containerRef, anchorRef, intersected, setIntersected])
2024-10-12 12:46:29 +08:00
}