File size: 704 Bytes
052672d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
'use client';

import * as React from 'react';
import { useInView } from 'react-intersection-observer';

import { useAtBottom } from '@/lib/hooks/useAtBottom';

interface ChatScrollAnchorProps {
	trackVisibility?: boolean;
}

export function ChatScrollAnchor({ trackVisibility }: ChatScrollAnchorProps) {
	const isAtBottom = useAtBottom();
	const { ref, entry, inView } = useInView({
		trackVisibility,
		delay: 100,
		rootMargin: '0px 0px -150px 0px',
	});

	React.useEffect(() => {
		if (isAtBottom && trackVisibility && !inView) {
			entry?.target.scrollIntoView({
				block: 'start',
			});
		}
	}, [inView, entry, isAtBottom, trackVisibility]);

	return <div ref={ref} className="h-px w-full" />;
}