agent-ecosystem/src/renderer/hooks/useVisibleFileSection.ts
iliya 0df816bba6 feat: enhance diff view with continuous scroll and lazy loading
- Introduced a continuous scroll mode for the diff view, allowing users to review multiple files in a single scrollable container.
- Added lazy loading functionality to improve performance by loading file content as it approaches the viewport.
- Implemented a new portion collapse feature to allow users to expand unchanged regions incrementally, enhancing context retention during reviews.
- Updated navigation to support smooth scrolling between files and improved keyboard shortcuts for file navigation.
- Enhanced the review toolbar to manage actions across all files, including bulk accept/reject options.
- Added new hooks and components to support the continuous scroll and lazy loading features, ensuring a seamless user experience.
2026-02-25 15:39:14 +02:00

114 lines
3.4 KiB
TypeScript

import { type RefObject, useCallback, useEffect, useRef } from 'react';
interface UseVisibleFileSectionOptions {
onVisibleFileChange: (filePath: string) => void;
scrollContainerRef: RefObject<HTMLElement | null>;
isProgrammaticScroll: RefObject<boolean>;
}
interface UseVisibleFileSectionReturn {
registerFileSectionRef: (filePath: string) => (element: HTMLElement | null) => void;
}
export function useVisibleFileSection(
options: UseVisibleFileSectionOptions
): UseVisibleFileSectionReturn {
const { onVisibleFileChange, scrollContainerRef, isProgrammaticScroll } = options;
const visibleFilePaths = useRef<Set<string>>(new Set());
const elementRefs = useRef<Map<string, HTMLElement>>(new Map());
const observerRef = useRef<IntersectionObserver | null>(null);
const debounceRef = useRef<ReturnType<typeof setTimeout>>();
const updateTopmostVisible = useCallback(() => {
if (isProgrammaticScroll.current) return;
if (visibleFilePaths.current.size === 0) return;
let topmostPath: string | null = null;
let minTop = Infinity;
visibleFilePaths.current.forEach((filePath) => {
const element = elementRefs.current.get(filePath);
if (element) {
const rect = element.getBoundingClientRect();
if (rect.top < minTop) {
minTop = rect.top;
topmostPath = filePath;
}
}
});
if (topmostPath) {
onVisibleFileChange(topmostPath);
}
}, [onVisibleFileChange, isProgrammaticScroll]);
const debouncedUpdate = useCallback(() => {
clearTimeout(debounceRef.current);
debounceRef.current = setTimeout(updateTopmostVisible, 100);
}, [updateTopmostVisible]);
useEffect(() => {
if (!scrollContainerRef.current) return;
observerRef.current = new IntersectionObserver(
(entries) => {
let changed = false;
for (const entry of entries) {
const filePath = entry.target.getAttribute('data-file-path');
if (!filePath) continue;
if (entry.isIntersecting && entry.intersectionRatio >= 0.1) {
if (!visibleFilePaths.current.has(filePath)) {
visibleFilePaths.current.add(filePath);
changed = true;
}
} else {
if (visibleFilePaths.current.has(filePath)) {
visibleFilePaths.current.delete(filePath);
changed = true;
}
}
}
if (changed) {
debouncedUpdate();
}
},
{
root: scrollContainerRef.current,
threshold: 0.1,
rootMargin: '0px',
}
);
return () => {
observerRef.current?.disconnect();
observerRef.current = null;
clearTimeout(debounceRef.current);
};
}, [scrollContainerRef, debouncedUpdate]);
const registerFileSectionRef = useCallback((filePath: string) => {
return (element: HTMLElement | null) => {
const observer = observerRef.current;
if (!observer) return;
const prev = elementRefs.current.get(filePath);
if (prev) {
observer.unobserve(prev);
elementRefs.current.delete(filePath);
visibleFilePaths.current.delete(filePath);
}
if (element) {
element.setAttribute('data-file-path', filePath);
elementRefs.current.set(filePath, element);
observer.observe(element);
}
};
}, []);
return { registerFileSectionRef };
}