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

refactor: 사전 식물 이미지 해상도를 조정한다 #387

Merged
merged 14 commits into from
Sep 21, 2023
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
3 changes: 2 additions & 1 deletion .github/workflows/frontend-e2e-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,11 @@ jobs:
if: steps.cache.outputs.cache-hit != 'true'

- name: Run Cypress
uses: cypress-io/github-action@v5
uses: cypress-io/github-action@v6
with:
working-directory: frontend
start: npm run local
wait-on: 'http://localhost:8282'

- name: Send slack notification if test failed
if: ${{ failure() }}
Expand Down
34 changes: 17 additions & 17 deletions frontend/cypress/e2e/auth.cy.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,45 @@
import login from '../utils/login';

describe('비로그인 상태에서는 로그인 페이지로 이동한다.', () => {
// it('리마인더', () => {
// cy.visit('/reminder');
// cy.get('#toast-root').contains('로그인 후 이용 가능').url().should('match', /login/);
// });
it('리마인더', () => {
cy.visit('/reminder');
cy.url().should('match', /login/);
});

// it('내 반려 식물 목록', () => {
// cy.visit('/pet');
// cy.get('#toast-root').contains('로그인 후 이용 가능').url().should('match', /login/);
// });
it('내 반려 식물 목록', () => {
cy.visit('/pet');
cy.url().should('match', /login/);
});

it('반려 식물 상세 정보', () => {
cy.visit('/pet/123');
cy.get('#toast-root').contains('로그인 후 이용 가능').url().should('match', /login/);
cy.url().should('match', /login/);
});

// it('반려 식물 등록: 검색', () => {
// cy.visit('/pet/register');
// cy.get('#toast-root').contains('로그인 후 이용 가능').url().should('match', /login/);
// cy.url().should('match', /login/);
// });

// it('반려 식물 등록: 양식', () => {
// cy.visit('/pet/register/1');
// cy.get('#toast-root').contains('로그인 후 이용 가능').url().should('match', /login/);
// cy.url().should('match', /login/);
// });

// it('마이페이지', () => {
// cy.visit('/myPage');
// cy.get('#toast-root').contains('로그인 후 이용 가능').url().should('match', /login/);
// cy.url().should('match', /login/);
// });

it('반려 식물 정보 수정', () => {
cy.visit('/pet/1/edit');
cy.get('#toast-root').contains('로그인 후 이용 가능').url().should('match', /login/);
cy.url().should('match', /login/);
});

// it('타임라인', () => {
// cy.visit('/pet/1/timeline');
// cy.get('#toast-root').contains('로그인 후 이용 가능').url().should('match', /login/);
// });
it('타임라인', () => {
cy.visit('/pet/1/timeline');
cy.url().should('match', /login/);
});
});

describe('로그인 상태에서는 접근이 가능하다.', () => {
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/@common/Image/Image.style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { keyframes, styled } from 'styled-components';

export interface StyledImageProps {
type: 'circle' | 'square' | 'wide';
size: string;
size: `${number}px`;
}

const wave = (size: string) => keyframes`
Expand Down
19 changes: 16 additions & 3 deletions frontend/src/components/@common/Image/index.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
import { forwardRef } from 'react';
import { forwardRef, useRef } from 'react';
import type { StyledImageProps } from './Image.style';
import { StyledImage } from './Image.style';
import { getResizedImageUrl } from 'utils/image';
import sadpiumiPng from 'assets/sadpiumi-imageFail.png';

type ImageProps = Omit<React.ImgHTMLAttributes<HTMLImageElement>, 'onError'> &
Partial<StyledImageProps>;

const Image = forwardRef<HTMLImageElement, ImageProps>(function Image(props, ref) {
const { type = 'circle', size = '77px', ...imageProps } = props;
const { type = 'circle', size = '77px', src = sadpiumiPng, ...imageProps } = props;

const sizeValue = Number(size.slice(0, -2));
const fallbackImages = useRef<string[]>([
sadpiumiPng,
src,
getResizedImageUrl(src, sizeValue, 'png'),
getResizedImageUrl(src, sizeValue, 'webp'),
]);

const currentImage = fallbackImages.current[fallbackImages.current.length - 1];

const setErrorImage: React.ReactEventHandler<HTMLImageElement> = ({ currentTarget }) => {
currentTarget.src = sadpiumiPng;
fallbackImages.current.pop();
currentTarget.src = fallbackImages.current[fallbackImages.current.length - 1];
};

return (
Expand All @@ -20,6 +32,7 @@ const Image = forwardRef<HTMLImageElement, ImageProps>(function Image(props, ref
size={size}
onError={setErrorImage}
loading="lazy"
src={currentImage}
{...imageProps}
/>
);
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/petPlant/PetPlantCard/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const PetPlantCard = ({
<Wrapper>
<CrownArea>{isBirthday && <SvgIcons icon="crown" size={64} aria-hidden />}</CrownArea>
<ImageArea>
<Image src={imageUrl} type="square" size="100%" alt="반려 식물 이미지" />
<Image src={imageUrl} type="square" size="160px" alt="반려 식물 이미지" />
</ImageArea>
<ContentArea>
<Nickname aria-label="식물 별명">{nickname} </Nickname>
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/search/SearchBox/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ const SearchBox = (props: SearchBoxProps) => {
alt={name}
src={image}
type="circle"
size={`calc(${height} * 2/3)`}
size={`${Math.round(numberHeight * (2 / 3))}px`}
loading="lazy"
/>
<Name>{name}</Name>
Expand Down
31 changes: 31 additions & 0 deletions frontend/src/utils/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,34 @@ export const getImageUrl = (file: File) => {

export const isAllowedImageExtension = (file: File) =>
ALLOWED_IMAGE_EXTENSIONS.includes(file.type.toLowerCase());

const X_SMALL_WIDTH = 64;
const SMALL_WIDTH = 256;

/**
* 피움 서비스의 정적 이미지 파일 네이밍 정책을 따르는
* 알맞은 크기의 사진 url을 반환합니다.
*
* `size`가 64보다 작으면 x-small, 256보다 작으면 small 크기로 재조정된 사진 url을 반환합니다.
*
* @param url img src에 들어갈 수 있는 url. 확장자명으로 끝나야 제대로 작동합니다.
* @param size 사진 픽셀 크기
* @param extension 원하는 확장자
* @returns 새로운 url
*/
export const getResizedImageUrl = (url: string, size: number, extension: 'png' | 'webp') => {
const urlTokens = url.split('.');
const originalExtension = urlTokens.pop();

if (originalExtension === undefined) return '';

if (size < X_SMALL_WIDTH) {
urlTokens.push('x-small', extension);
} else if (size < SMALL_WIDTH) {
urlTokens.push('small', extension);
} else {
urlTokens.push(originalExtension);
}

return urlTokens.join('.');
};