"use client"; import React, { useState } from 'react'; import { Badge } from '@/components/ui/badge'; import { ScrollArea } from '@/components/ui/scroll-area'; import { Button } from '@/components/ui/button'; import { Accordion, AccordionContent, AccordionItem, AccordionTrigger } from '@/components/ui/accordion'; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger } from '@/components/ui/dialog'; import { Info } from 'lucide-react'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; import Image from 'next/image'; import { Document, Folder } from '@/components/types'; interface DocumentDetailProps { selectedDocument: Document | null; handleDeleteDocument: (documentId: string) => Promise; folders: Folder[]; apiBaseUrl: string; authToken: string | null; refreshDocuments: () => void; refreshFolders: () => void; loading: boolean; onClose: () => void; } const DocumentDetail: React.FC = ({ selectedDocument, handleDeleteDocument, folders, apiBaseUrl, authToken, refreshDocuments, refreshFolders, loading, onClose }) => { const [isMovingToFolder, setIsMovingToFolder] = useState(false); if (!selectedDocument) { return (

Select a document to view details

); } const currentFolder = selectedDocument.system_metadata?.folder_name as string | undefined; const handleMoveToFolder = async (folderName: string | null) => { if (isMovingToFolder || !selectedDocument) return; const documentId = selectedDocument.external_id; setIsMovingToFolder(true); try { // First, get the folder ID from the name if a name is provided if (folderName) { // Find the target folder by name const targetFolder = folders.find(folder => folder.name === folderName); if (targetFolder && targetFolder.id) { console.log(`Found folder with ID: ${targetFolder.id} for name: ${folderName}`); // Add to folder using folder ID await fetch(`${apiBaseUrl}/folders/${targetFolder.id}/documents/${documentId}`, { method: 'POST', headers: { 'Content-Type': 'application/json', ...(authToken ? { 'Authorization': `Bearer ${authToken}` } : {}) } }); } else { console.error(`Could not find folder with name: ${folderName}`); } } // If there's a current folder and we're either moving to a new folder or removing from folder if (currentFolder) { // Find the current folder ID const currentFolderObj = folders.find(folder => folder.name === currentFolder); if (currentFolderObj && currentFolderObj.id) { // Remove from current folder using folder ID await fetch(`${apiBaseUrl}/folders/${currentFolderObj.id}/documents/${documentId}`, { method: 'DELETE', headers: { 'Content-Type': 'application/json', ...(authToken ? { 'Authorization': `Bearer ${authToken}` } : {}) } }); } } // Refresh folders first to get updated document_ids await refreshFolders(); // Then refresh documents with the updated folder information await refreshDocuments(); } catch (error) { console.error('Error updating folder:', error); } finally { setIsMovingToFolder(false); } }; return (

Document Details

Filename

{selectedDocument.filename || 'N/A'}

Content Type

{selectedDocument.content_type}

Folder

Folder

Document ID

{selectedDocument.external_id}

Metadata
                  {JSON.stringify(selectedDocument.metadata, null, 2)}
                
System Metadata
                  {JSON.stringify(selectedDocument.system_metadata, null, 2)}
                
Additional Metadata
                  {JSON.stringify(selectedDocument.additional_metadata, null, 2)}
                
Delete Document Are you sure you want to delete this document? This action cannot be undone.

Document: {selectedDocument.filename || selectedDocument.external_id}

ID: {selectedDocument.external_id}

); }; export default DocumentDetail;