Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 0 additions & 18 deletions frontend/src/components/common/LazyImage/LazyImage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,24 +85,6 @@ describe('LazyImage 컴포넌트', () => {
expect(mockDisconnect).toHaveBeenCalled();
});

it('index prop에 따라 지연 시간이 적용되어야 함', () => {
render(<LazyImage {...defaultProps} index={2} delayMs={100} />);

const [[callback]] = mockIntersectionObserver.mock.calls;
const entry = { isIntersecting: true };
act(() => {
callback([entry], {} as IntersectionObserver);
});

expect(screen.queryByRole('img')).not.toBeInTheDocument();

act(() => {
jest.advanceTimersByTime(200);
});

expect(screen.getByRole('img')).toBeInTheDocument();
});

it('onError prop이 호출되어야 함', async () => {
const onError = jest.fn();
render(<LazyImage {...defaultProps} onError={onError} />);
Expand Down
49 changes: 17 additions & 32 deletions frontend/src/components/common/LazyImage/LazyImage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,34 @@ interface LazyImageProps {
src: string;
alt: string;
onError?: () => void;
index?: number;
delayMs?: number;
}

const LazyImage = ({
src,
alt,
onError,
index = 0,
delayMs = 200,
}: LazyImageProps) => {
const [shouldLoad, setShouldLoad] = useState(false);
const LazyImage = ({ src, alt, onError }: LazyImageProps) => {
const [isVisible, setIsVisible] = useState(false);
const imgRef = useRef<HTMLImageElement | null>(null);

useEffect(() => {
const observer = new IntersectionObserver(
([entry]) => {
if (entry.isIntersecting) {
const delay = index * delayMs;
const timeout = setTimeout(() => {
setShouldLoad(true);
}, delay);
observer.disconnect();

return () => clearTimeout(timeout);
}
},
{ threshold: 0.1 },
);
let timeout: ReturnType<typeof setTimeout>;

const observer = new IntersectionObserver(([entry]) => {
if (entry.isIntersecting) {
const delay = 100;
timeout = setTimeout(() => {
setIsVisible(true);
}, delay);
observer.disconnect();
}
});

if (imgRef.current) {
observer.observe(imgRef.current);
}

return () => observer.disconnect();
}, [index, delayMs]);

useEffect(() => {
if (shouldLoad) {
setIsVisible(true);
}
}, [shouldLoad]);
return () => {
observer.disconnect();
clearTimeout(timeout);
};
}, []);

return isVisible ? (
<img ref={imgRef} src={src} alt={alt} onError={onError} />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,6 @@ const PhotoList = ({ feeds: photos, sectionRefs }: PhotoListProps) => {
<LazyImage
src={url}
alt={`활동 사진 ${index + 1}`}
index={index}
delayMs={200}
onError={() => handleImageError(index)}
/>
) : (
Expand Down