'use client'; import React, { useState, useEffect } from 'react'; import { Modal } from '@/components/ui'; import { apiClient } from '@/lib/api'; import { toast } from 'sonner'; import { Folder, Tag as TagIcon, Loader2 } from 'lucide-react'; import type { Document } from '@/types/api'; interface DocumentMetadataEditorProps { isOpen: boolean; onClose: () => void; document: Document; onUpdate?: () => void; } export function DocumentMetadataEditor({ isOpen, onClose, document, onUpdate, }: DocumentMetadataEditorProps) { // Form state const [selectedTags, setSelectedTags] = useState([]); const [selectedFolder, setSelectedFolder] = useState(document.folder?.id || null); // Data lists const [documentTags, setDocumentTags] = useState([]); const [folders, setFolders] = useState([]); // Loading states const [loadingTags, setLoadingTags] = useState(false); const [loadingFolders, setLoadingFolders] = useState(false); const [isSaving, setIsSaving] = useState(false); const [error, setError] = useState(null); // Load current document tags useEffect(() => { if (isOpen && document) { // Load current tags if (document.tags && Array.isArray(document.tags)) { setSelectedTags(document.tags.map((t: any) => t.id)); } } }, [isOpen, document]); // Load reference data useEffect(() => { if (isOpen) { loadAllData(); } }, [isOpen]); const loadAllData = async () => { // Load tags try { setLoadingTags(true); const tagsResponse = await apiClient.getDocumentTags(); setDocumentTags(tagsResponse.results || tagsResponse); } catch (err) { console.error('Error loading tags:', err); } finally { setLoadingTags(false); } // Load folders try { setLoadingFolders(true); const foldersResponse = await apiClient.getFolders({}); setFolders(foldersResponse.results || foldersResponse); } catch (err) { console.error('Error loading folders:', err); } finally { setLoadingFolders(false); } }; const handleSave = async () => { setIsSaving(true); setError(null); try { // Prepare update data const updateData: any = {}; // Tags const currentTagIds = (document.tags || []).map((t: any) => t.id).sort(); const newTagIds = [...selectedTags].sort(); if (JSON.stringify(currentTagIds) !== JSON.stringify(newTagIds)) { updateData.tag_ids = selectedTags; } // Folder if (selectedFolder !== (document.folder?.id || null)) { updateData.folder = selectedFolder; } // If nothing changed if (Object.keys(updateData).length === 0) { toast.info('Нет изменений для сохранения'); onClose(); return; } // Send update request await apiClient.updateDocument(document.id, updateData); toast.success('Метаданные документа обновлены'); onUpdate?.(); onClose(); } catch (err: any) { console.error('Error updating document metadata:', err); const errorMsg = err.response?.data?.detail || 'Не удалось обновить метаданные'; setError(errorMsg); toast.error(errorMsg); } finally { setIsSaving(false); } }; const toggleTag = (tagId: number) => { setSelectedTags((prev) => prev.includes(tagId) ? prev.filter((id) => id !== tagId) : [...prev, tagId] ); }; return (
{error && (

{error}

)} {/* Document info */}

{document.title}

ID: {document.id}

{/* Tags */}
{loadingTags ? (
) : documentTags.length === 0 ? (
Нет доступных тегов
) : (
{documentTags.map((tag) => ( ))}
)}

Выбрано: {selectedTags.length} {selectedTags.length === 1 ? 'тег' : 'тегов'}

{/* Folder */}
{/* Actions */}
); }