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

[FE] Creator Image Lazy loading 적용 #795

Merged
merged 5 commits into from
Oct 24, 2024
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
11 changes: 9 additions & 2 deletions client/src/hooks/useImageLazyLoading.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,28 @@ import {useEffect, useState} from 'react';
type UseImageLazyLoadingProps<T extends HTMLElement> = {
targetRef: React.RefObject<T>;
src: string;
fallbackSrc?: string;
threshold?: number;
};

type ImgTagSrcType = string | undefined;

const useImageLazyLoading = <T extends HTMLElement>({
targetRef,
src,
fallbackSrc,
threshold = 0.05,
}: UseImageLazyLoadingProps<T>) => {
const [imageSrc, setImageSrc] = useState<string | undefined>(undefined);
const [imageSrc, setImageSrc] = useState<ImgTagSrcType>(undefined);
const [fallbackImageSrc, setFallbackImageSrc] = useState<ImgTagSrcType>(undefined);

useEffect(() => {
if (targetRef && !imageSrc) {
const observer = new IntersectionObserver(
([entry]) => {
if (entry.isIntersecting) {
setImageSrc(src);
setFallbackImageSrc(fallbackSrc);
if (targetRef.current) {
observer.unobserve(targetRef.current);
}
Expand All @@ -39,10 +45,11 @@ const useImageLazyLoading = <T extends HTMLElement>({
}

return;
}, [targetRef, src]);
}, [targetRef, src, fallbackSrc]);

return {
imageSrc,
fallbackImageSrc,
};
};

Expand Down
7 changes: 6 additions & 1 deletion client/src/pages/MainPage/Section/CreatorSection/Avatar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ interface Props {
const Avatar = ({imagePath, name, navigateUrl}: Props) => {
return (
<a href={navigateUrl} target="_blank" css={avatarStyle}>
<Image src={getImageUrl(imagePath, 'webp')} fallbackSrc={getImageUrl(imagePath, 'png')} css={avatarImageStyle} />
<Image
src={getImageUrl(imagePath, 'webp')}
fallbackSrc={getImageUrl(imagePath, 'png')}
loading="lazy"
css={avatarImageStyle}
/>
<Text size="bodyBold" textColor="white" responsive={true}>
{name}
</Text>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,16 @@ import {descriptionSectionStyle, imgStyle} from './DescriptionSection.style';
const DescriptionSection = () => {
const descriptionRef = useRef<HTMLDivElement>(null);

const {imageSrc} = useImageLazyLoading({
const {imageSrc, fallbackImageSrc} = useImageLazyLoading({
targetRef: descriptionRef,
src: `${process.env.IMAGE_URL}/standingDog.webp`,
src: getImageUrl('standingDog', 'webp'),
fallbackSrc: getImageUrl('standingDog', 'png'),
threshold: 0.05,
});

return (
<div css={descriptionSectionStyle} ref={descriptionRef}>
<Image
css={imgStyle}
src={imageSrc!}
alt="행댕이 - 행동대장 마스코트"
fallbackSrc={getImageUrl('standingDog', 'png')}
/>
<Image css={imgStyle} src={imageSrc!} alt="행댕이 - 행동대장 마스코트" fallbackSrc={fallbackImageSrc} />
<img css={imgStyle} />
<Text style={{textAlign: 'center'}} size="subTitle" responsive={true}>{`행동대장들을 위해
행동대장을 준비했어요
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,24 @@ import useImageLazyLoading from '@hooks/useImageLazyLoading';

import {Text} from '@components/Design';

import getImageUrl from '@utils/getImageUrl';

import {articleStyle, imageStyle, sectionStyle, textContainerStyle} from './AutoCalculate.style';

const AutoCalculate = () => {
const sectionRef = useRef<HTMLElement>(null);

const {imageSrc} = useImageLazyLoading({
const {imageSrc, fallbackImageSrc} = useImageLazyLoading({
targetRef: sectionRef,
src: `${process.env.IMAGE_URL}/feature2.webp`,
src: getImageUrl('feature2', 'webp'),
fallbackSrc: getImageUrl('feature2', 'png'),
threshold: 0.05,
});

return (
<section css={sectionStyle} ref={sectionRef}>
<article css={articleStyle}>
<Image src={imageSrc!} fallbackSrc={imageSrc} css={imageStyle} />
<Image src={imageSrc!} fallbackSrc={fallbackImageSrc} css={imageStyle} />
<div css={textContainerStyle}>
<Text size="subTitle" responsive={true}>
계산은 저희가 알아서 해드려요
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ import useImageLazyLoading from '@hooks/useImageLazyLoading';

import {Text} from '@components/Design';

import getImageUrl from '@utils/getImageUrl';

import {articleStyle, imageStyle, sectionStyle, textContainerStyle} from './CheckDeposit.style';

const CheckDeposit = () => {
const sectionRef = useRef<HTMLElement>(null);

const {imageSrc} = useImageLazyLoading({
const {imageSrc, fallbackImageSrc} = useImageLazyLoading({
targetRef: sectionRef,
src: `${process.env.IMAGE_URL}/feature3.webp`,
src: getImageUrl('feature3', 'webp'),
fallbackSrc: getImageUrl('feature3', 'png'),
threshold: 0.05,
});

Expand All @@ -30,7 +33,7 @@ const CheckDeposit = () => {
간편하게 관리할 수 있어요.`}
</Text>
</div>
<Image src={imageSrc!} fallbackSrc={imageSrc} css={imageStyle} />
<Image src={imageSrc!} fallbackSrc={fallbackImageSrc} css={imageStyle} />
</article>
</section>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {css} from '@emotion/react';
import {useRef} from 'react';

import useMainPageYScroll from '@hooks/useMainPageYScroll';

Expand All @@ -11,7 +10,6 @@ import {RecordMemoryWithPhoto} from './RecordMemoryWithPhoto';

const FeatureSection = () => {
const {featureSectionRef} = useMainPageYScroll();
const simpleTransferRef = useRef<HTMLElement>(null);

return (
<div
Expand All @@ -27,8 +25,8 @@ const FeatureSection = () => {
<SimpleShare />
<AutoCalculate />
<CheckDeposit />
<SimpleTransfer targetRef={simpleTransferRef} />
<RecordMemoryWithPhoto targetRef={simpleTransferRef} />
<SimpleTransfer />
<RecordMemoryWithPhoto />
</div>
);
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
import {useRef} from 'react';

import Image from '@components/Design/components/Image/Image';

import useImageLazyLoading from '@hooks/useImageLazyLoading';

import {Text} from '@components/Design';

import getImageUrl from '@utils/getImageUrl';

import {articleStyle, imageStyle, sectionStyle, textContainerStyle} from './RecordMemoryWithPhoto.style';

type RecordMemoryWithPhotoProps = {
targetRef: React.RefObject<HTMLElement>;
};
const RecordMemoryWithPhoto = () => {
const sectionRef = useRef<HTMLElement>(null);

const RecordMemoryWithPhoto = ({targetRef}: RecordMemoryWithPhotoProps) => {
const {imageSrc} = useImageLazyLoading({
targetRef,
src: `${process.env.IMAGE_URL}/feature5.webp`,
const {imageSrc, fallbackImageSrc} = useImageLazyLoading({
targetRef: sectionRef,
src: getImageUrl('feature5', 'webp'),
fallbackSrc: getImageUrl('feature5', 'png'),
threshold: 0.05,
});

return (
<section css={sectionStyle}>
<section css={sectionStyle} ref={sectionRef}>
<article css={articleStyle}>
<div css={textContainerStyle}>
<Text size="subTitle" responsive={true}>
Expand All @@ -30,7 +33,7 @@ const RecordMemoryWithPhoto = ({targetRef}: RecordMemoryWithPhotoProps) => {
정산은 투명하게, 추억은 오래오래 간직할 수 있어요.`}
</Text>
</div>
<Image src={imageSrc!} fallbackSrc={imageSrc} css={imageStyle} />
<Image src={imageSrc!} fallbackSrc={fallbackImageSrc} css={imageStyle} />
</article>
</section>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ import useImageLazyLoading from '@hooks/useImageLazyLoading';

import {Text} from '@components/Design';

import getImageUrl from '@utils/getImageUrl';

import {articleStyle, imageStyle, sectionStyle, textContainerStyle} from './SimpleShare.style';

const SimpleAccount = () => {
const sectionRef = useRef<HTMLElement>(null);

const {imageSrc} = useImageLazyLoading({
const {imageSrc, fallbackImageSrc} = useImageLazyLoading({
targetRef: sectionRef,
src: `${process.env.IMAGE_URL}/feature1.webp`,
src: getImageUrl('feature1', 'webp'),
fallbackSrc: getImageUrl('feature1', 'png'),
threshold: 0.05,
});

Expand All @@ -30,7 +33,7 @@ const SimpleAccount = () => {
복잡한 절차 없이, 빠르게 정산을 마치세요.`}
</Text>
</div>
<Image src={imageSrc!} fallbackSrc={imageSrc} css={imageStyle} />
<Image src={imageSrc!} fallbackSrc={fallbackImageSrc} css={imageStyle} />
</article>
</section>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
import {useRef} from 'react';

import Image from '@components/Design/components/Image/Image';

import useImageLazyLoading from '@hooks/useImageLazyLoading';

import {Text} from '@components/Design';

import getImageUrl from '@utils/getImageUrl';

import {articleStyle, imageStyle, sectionStyle, textContainerStyle} from './SimpleTransfer.style';

type SimpleTransferProps = {
targetRef: React.RefObject<HTMLElement>;
};
const SimpleTransfer = () => {
const simpleTransferRef = useRef<HTMLElement>(null);

const SimpleTransfer = ({targetRef}: SimpleTransferProps) => {
const {imageSrc} = useImageLazyLoading({
targetRef,
src: `${process.env.IMAGE_URL}/feature4.webp`,
const {imageSrc, fallbackImageSrc} = useImageLazyLoading({
targetRef: simpleTransferRef,
src: getImageUrl('feature4', 'webp'),
fallbackSrc: getImageUrl('feature4', 'png'),
threshold: 0.05,
});

return (
<section css={sectionStyle} ref={targetRef}>
<section css={sectionStyle} ref={simpleTransferRef}>
<article css={articleStyle}>
<Image src={imageSrc!} fallbackSrc={imageSrc} css={imageStyle} />
<Image src={imageSrc!} fallbackSrc={fallbackImageSrc} css={imageStyle} />
<div css={textContainerStyle}>
<Text size="subTitle" responsive={true}>
몇 번의 클릭으로 송금 완료!
Expand Down
Loading