"use client" import { AlertTriangle, CheckCircle, FileX, Search, Trash2 } from "lucide-react" import { useEffect, useState } from "react" import { Button } from "@/components/ui/button" import { Separator } from "@/components/ui/separator" import { promptUserToFindFile } from "@/features/media/services/media-restoration-service" import { SavedMediaFile } from "@/features/media/types/saved-media" import { useModal } from "@/features/modals/services" interface FileResolution { file: SavedMediaFile action: "pending" | "found" | "remove" | "skip" newPath?: string isProcessing?: boolean } export function MissingFilesModal() { const { modalData, closeModal } = useModal() const missingFiles = (modalData?.missingFiles as SavedMediaFile[]) || [] const onResolve = modalData?.onResolve as | (( resolved: Array<{ file: SavedMediaFile newPath?: string action: "found" | "remove" }>, ) => void) | undefined const [resolutions, setResolutions] = useState([]) // Инициализация состояния при изменении missingFiles useEffect(() => { setResolutions(missingFiles.map((file) => ({ file, action: "pending" }))) }, [missingFiles]) const handleFindFile = async (index: number) => { const resolution = resolutions[index] // Обновляем состояние - показываем, что файл обрабатывается setResolutions((prev) => prev.map((r, i) => (i === index ? { ...r, isProcessing: true } : r))) try { const newPath = await promptUserToFindFile(resolution.file) setResolutions((prev) => prev.map((r, i) => i === index ? { ...r, action: newPath ? "found" : "skip", newPath: newPath || undefined, isProcessing: false, } : r, ), ) } catch (error) { console.error("Ошибка при поиске файла:", error) setResolutions((prev) => prev.map((r, i) => (i === index ? { ...r, isProcessing: false } : r))) } } const handleRemoveFile = (index: number) => { setResolutions((prev) => prev.map((r, i) => (i === index ? { ...r, action: "remove" } : r))) } const handleSkipFile = (index: number) => { setResolutions((prev) => prev.map((r, i) => (i === index ? { ...r, action: "skip" } : r))) } const handleResolveAll = () => { const resolved = resolutions .filter((r) => r.action === "found" || r.action === "remove") .map((r) => ({ file: r.file, newPath: r.newPath, action: r.action as "found" | "remove", })) onResolve?.(resolved) closeModal() } const handleSkipAll = () => { onResolve?.([]) closeModal() } const getActionIcon = (action: FileResolution["action"]) => { switch (action) { case "found": return case "remove": return case "skip": return default: return } } const getActionText = (action: FileResolution["action"]) => { switch (action) { case "found": return "Найден" case "remove": return "Удалить" case "skip": return "Пропущен" default: return "Ожидает" } } const resolvedCount = resolutions.filter((r) => r.action === "found" || r.action === "remove").length const canProceed = resolvedCount > 0 return (

При открытии проекта обнаружены отсутствующие файлы. Выберите действие для каждого файла: найти новое расположение или удалить из проекта.

Файлов: {missingFiles.length} Обработано: {resolvedCount}/{missingFiles.length}
{resolutions.map((resolution, index) => (
{getActionIcon(resolution.action)} {resolution.file.name} {getActionText(resolution.action)}

{resolution.newPath || resolution.file.originalPath}

{resolution.file.size && (

Размер: {(resolution.file.size / 1024 / 1024).toFixed(1)} МБ

)}
{resolution.action === "pending" && ( <> )} {resolution.action !== "pending" && ( )}
{index < resolutions.length - 1 && }
))}
{canProceed &&

Будет обработано {resolvedCount} файлов

}
) }