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
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export const ImageLoadingBar = () => {
return (
<div className='absolute'>
<svg
aria-hidden='true'
className='text-neutral-tertiary fill-brand h-8 w-8 animate-spin'
fill='none'
viewBox='0 0 100 101'
xmlns='http://www.w3.org/2000/svg'
>
<path
d='M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z'
fill='#e8ecef'
/>
<path
d='M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z'
fill='#00c9ec'
/>
</svg>
</div>
);
};
60 changes: 33 additions & 27 deletions src/components/pages/create-group/fields/images-field/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,48 @@

import Image from 'next/image';

import { useRef } from 'react';
import { useRef, useState } from 'react';

import { AnyFieldApi } from '@tanstack/react-form';

import { Icon } from '@/components/icon';
import { ImageLoadingBar } from '@/components/pages/create-group/fields/images-field/image-loading-bar';
import { Hint } from '@/components/ui';
import { useUploadGroupImages } from '@/hooks/use-group/use-group-upload-images';
import { cn } from '@/lib/utils';
import { ALLOWED_IMAGE_TYPES } from '@/types/service/common';
import { validateImage } from '@/lib/validateImage';
import { PreUploadGroupImageResponse } from '@/types/service/group';

interface Props {
field: AnyFieldApi;
}

export const GroupImagesField = ({ field }: Props) => {
const { mutateAsync } = useUploadGroupImages();

const onUploadImage = async (e: React.ChangeEvent<HTMLInputElement>) => {
const maxAllowed = 3 - field.state.value.length;
const files = e.target.files;
const [preUploadError, setPreUploadError] = useState('');
const inputRef = useRef<HTMLInputElement | null>(null);

if (!files || files.length === 0) return;
if (files.length > maxAllowed) return;
const { mutateAsync, isPending } = useUploadGroupImages();

const fileArray = Array.from(files);
const onUploadImage = async (e: React.ChangeEvent<HTMLInputElement>) => {
if (!e.target.files || !e.target.files.length) return;

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const invalidFile = fileArray.find((file) => !ALLOWED_IMAGE_TYPES.includes(file.type as any));
const fileArray = Array.from(e.target.files);

if (invalidFile) {
alert('jpg, png, webp 파일만 업로드 가능합니다.');
e.target.value = '';
return;
for (const file of fileArray) {
const { valid, error } = await validateImage(file);
if (!valid && error) {
setPreUploadError(error);
e.target.value = '';
return;
}
}

const response = await mutateAsync({
images: fileArray,
});

field.handleChange([...field.state.value, ...response.images]);
setPreUploadError('');
};

const onUploadImageButtonClick = () => {
Expand All @@ -54,29 +56,32 @@ export const GroupImagesField = ({ field }: Props) => {
field.handleChange(removedArray);
};

const inputRef = useRef<HTMLInputElement | null>(null);

return (
<div className='space-y-1'>
<div className='mt-6 flex flex-row gap-2'>
<button
className={cn(
'flex-center bg-mono-white group aspect-square w-full max-w-20 cursor-pointer rounded-2xl border-1 border-gray-300', // 기본 스타일
'flex-center bg-mono-white group relative aspect-square w-full max-w-20 cursor-pointer rounded-2xl border-1 border-gray-300', // 기본 스타일
'hover:bg-gray-50', // hover 스타일
'transition-all duration-300', // animation 스타일
)}
aria-label='이미지 선택 버튼'
disabled={isPending}
type='button'
onClick={onUploadImageButtonClick}
>
<Icon
id='plus'
className={cn(
'size-6 text-gray-600', // 기본 스타일
'group-hover:scale-120', // hover 스타일
'transition-all duration-300', // animation 스타일
)}
/>
{isPending ? (
<ImageLoadingBar />
) : (
<Icon
id='plus'
className={cn(
'size-6 text-gray-600', // 기본 스타일
'group-hover:scale-120', // hover 스타일
'transition-all duration-300', // animation 스타일
)}
/>
)}
<input
ref={inputRef}
className='hidden'
Expand Down Expand Up @@ -113,6 +118,7 @@ export const GroupImagesField = ({ field }: Props) => {
),
)}
</div>
{preUploadError && <Hint message={preUploadError} />}
<p className='text-text-sm-medium px-2 text-gray-500 select-none'>
최대 3개까지 업로드할 수 있어요.
</p>
Expand Down
5 changes: 4 additions & 1 deletion src/components/ui/hint/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ export const Hint = ({ className, message, ...props }: HintProps) => {
return (
<p
{...props}
className={cn('text-error-500 text-text-sm-medium w-full px-2 select-none', className)}
className={cn(
'text-error-500 text-text-sm-medium w-full px-2 whitespace-pre-wrap select-none',
className,
)}
aria-live='polite'
>
{message}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/constants/image.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export const IMAGE_CONFIG = {
maxSizeBytes: 20971520, // 20MB
maxSizeBytes: 10485760, // 10MB
maxWidth: 2000,
maxHeight: 2000,
allowedTypes: ['image/jpeg', 'image/png', 'image/webp'],
Expand Down
9 changes: 6 additions & 3 deletions src/lib/validateImage.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { IMAGE_CONFIG } from './constants/image';

export const validateImage = async (file: File): Promise<{ valid: boolean; error?: string }> => {
// 1. 확장자 검증
// 1. 파일 확장자 & 파일 타입 검증
const fileName = file.name.toLowerCase();
const hasValidExtension = IMAGE_CONFIG.allowedExtensions.some((ext) => fileName.endsWith(ext));
const hasValidType = IMAGE_CONFIG.allowedTypes.some((type) => file.type === type);

if (!hasValidExtension) {
const isValidImage = hasValidExtension && hasValidType;

if (!isValidImage) {
return {
valid: false,
error: `파일 확장자가 올바르지 않습니다. \n(${IMAGE_CONFIG.allowedExtensions.join(', ')}만 가능)`,
Expand All @@ -17,7 +20,7 @@ export const validateImage = async (file: File): Promise<{ valid: boolean; error
const currentSizeMB = (file.size / (1024 * 1024)).toFixed(0);
return {
valid: false,
error: `이미지 크기가 너무 큽니다. 최대 20MB까지 가능합니다. \n현재: ${currentSizeMB}MB`,
error: `이미지 크기가 너무 큽니다. 최대 10MB까지 가능합니다. \n현재: ${currentSizeMB}MB`,
};
}

Expand Down
2 changes: 0 additions & 2 deletions src/types/service/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,3 @@ export class CommonSuccessResponse<T> {
public data: T,
) {}
}

export const ALLOWED_IMAGE_TYPES = ['image/jpeg', 'image/png', 'image/webp'] as const;