'use client'; import React, { useState, useEffect, useMemo } from 'react'; import { submitHomework, validateHomeworkFiles, getHomeworkById, type Homework } from '@/api/homework'; import { getBackendOrigin } from '@/lib/api-client'; const MAX_FILES = 10; const MAX_FILE_SIZE_MB = 50; type FileKind = 'image' | 'video' | 'audio' | 'pdf' | 'other'; function getFileKind(file: File): FileKind { const t = file.type.toLowerCase(); const name = file.name.toLowerCase(); if (t.startsWith('image/') || /\.(jpe?g|png|gif|webp|bmp|svg|ico)$/i.test(name)) return 'image'; if (t.startsWith('video/') || /\.(mp4|webm|ogg|mov|avi|mkv)$/i.test(name)) return 'video'; if (t.startsWith('audio/') || /\.(mp3|wav|ogg|m4a|flac)$/i.test(name)) return 'audio'; if (t === 'application/pdf' || name.endsWith('.pdf')) return 'pdf'; return 'other'; } function getFileIcon(kind: FileKind): string { switch (kind) { case 'image': return 'image'; case 'video': return 'videocam'; case 'audio': return 'audiotrack'; case 'pdf': return 'picture_as_pdf'; default: return 'description'; } } function formatSize(bytes: number): string { if (bytes >= 1024 * 1024) return `${(bytes / 1024 / 1024).toFixed(2)} МБ`; if (bytes >= 1024) return `${(bytes / 1024).toFixed(1)} КБ`; return `${bytes} Б`; } function FilePreview({ file, onRemove, disabled }: { file: File; onRemove: () => void; disabled?: boolean }) { const kind = getFileKind(file); const [objectUrl, setObjectUrl] = useState(null); const icon = getFileIcon(kind); useEffect(() => { if (kind === 'image' || kind === 'video' || kind === 'pdf') { const url = URL.createObjectURL(file); setObjectUrl(url); return () => URL.revokeObjectURL(url); } }, [file, kind]); const preview = useMemo(() => { if (kind === 'image' && objectUrl) { return ( ); } if (kind === 'video' && objectUrl) { return (