import { useRef, useState } from 'react'; import { ImagePlus, Loader2, Trash2 } from 'lucide-react'; import { deleteCurriculumReference, uploadCurriculumReference, type CurriculumReference } from '../lib/curriculum'; type Props = { accessToken: string; courseId: string; week: number; cover: CurriculumReference | null; onChange: (cover: CurriculumReference | null) => void; }; export function WeekCoverEditor({ accessToken, courseId, week, cover, onChange }: Props) { const input = useRef(null); const [busy, setBusy] = useState(false); const [error, setError] = useState(''); async function save(file?: File) { if (busy || (!file && !cover)) return; if (file && (!['image/png', 'image/jpeg', 'image/webp'].includes(file.type) || file.size > 8 * 1024 * 1024)) { setError('8MB 이하의 PNG, JPEG, WebP 이미지를 선택해주세요.'); return; } setBusy(true); setError(''); try { const result = file ? await uploadCurriculumReference(accessToken, { courseId, week, file, kind: 'cover-image' }) : await deleteCurriculumReference(accessToken, cover!.id, courseId); onChange(result.weeks.find((item) => item.week === week)?.detail.coverImage ?? null); } catch (reason) { setError(reason instanceof Error ? reason.message : '대표이미지를 저장하지 못했습니다.'); } finally { setBusy(false); } } return (

대표이미지

{cover && }
{ const file = event.target.files?.[0]; event.target.value = ''; if (file) void save(file); }} /> {cover && {`${week}주차} {error &&

{error}

}
); }