Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Add] feed image lazy loading #32 #252

Merged
merged 1 commit into from
Dec 23, 2019
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
61 changes: 44 additions & 17 deletions client/src/composition/Feed/ImageContainer.tsx
Original file line number Diff line number Diff line change
@@ -1,34 +1,61 @@
import React from 'react';
import React, { useState } from 'react';
import { Image } from 'react-components.d';
import { Carousel } from 'react-responsive-carousel';
import 'style/carousel.css';
import useIntersect from 'hooks/useIntersectObserver';
import { useCallback } from 'react';
import styled from 'styled-components';

const FaceImageContainer = styled.div`
width: 480px;
height: 500px;
background: ${props => props.theme.colors.borderColor};
border-radius: 2px;
`;
interface IProps {
images: (Image | null)[];
width: string;
height: string;
}

const EMPTY_URL = 'Empty';
function ImageContainer({ images, width, height }: IProps) {
const [imageUrl, setImageUrl] = useState<string[]>([]); //fake image
const setImages = useCallback(() => {
const imagelist: string[] = images.map(image => {
return image && image.url ? image.url : EMPTY_URL;
});
setImageUrl(imagelist);
}, [images]);

const [, setRef] = useIntersect(setImages, () => {}, { threshold: 0.1 });
if (!images) return <></>;

return (
<>
<Carousel
autoPlay
dynamicHeight={true}
showThumbs={false}
showStatus={false}>
{images.map((image, idx) =>
image && image.url ? (
<div key={idx + image.url}>
<img style={{ width, height }} src={image.url} alt={image.url} />
</div>
) : (
<></>
)
)}
</Carousel>
{imageUrl && imageUrl.length === 0 ? (
<FaceImageContainer ref={setRef as any}></FaceImageContainer>
) : (
<Carousel
autoPlay
dynamicHeight={true}
showThumbs={false}
showStatus={false}>
{images.map((image, idx) =>
image && image.url ? (
<div key={idx + image.url}>
<img
style={{ width, height }}
src={imageUrl[idx]}
data-src={image.url}
alt={image.url}
/>
</div>
) : (
<></>
)
)}
</Carousel>
)}
</>
);
}
Expand Down
8 changes: 4 additions & 4 deletions client/src/hooks/useIntersectObserver.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ const baseOption = {
const useIntersect = (
interSectAction: any,
outerSectAction: any,
option: any
option: any = baseOption
) => {
const [ref, setRef] = useState(null);

const checkIntersect = useCallback(
(entry: IntersectionObserverEntry[]) => {
(entry: IntersectionObserverEntry[], observer) => {
if (entry[0].isIntersecting) {
interSectAction();
interSectAction(entry[0], observer);
} else {
outerSectAction();
}
Expand All @@ -29,7 +29,7 @@ const useIntersect = (
let observer: any;

if (ref) {
observer = new IntersectionObserver(checkIntersect, baseOption);
observer = new IntersectionObserver(checkIntersect, option);
observer.observe(ref);
}
return () => observer && observer.disconnect();
Expand Down