'use client'; // src/app/update/[id]/update-view.tsx import { useState } from "react"; import { Box, Button, Typography, Paper, Stack, TextField, MenuItem, IconButton, Grid, Divider, Checkbox, CircularProgress, Chip, Tooltip } from "@mui/material"; import SaveIcon from "@mui/icons-material/Save"; import ArrowBackIcon from '@mui/icons-material/ArrowBack'; import AssignmentIcon from '@mui/icons-material/Assignment'; import AddCircleOutlineIcon from '@mui/icons-material/AddCircleOutline'; import DeleteOutlineIcon from '@mui/icons-material/DeleteOutline'; import AutoFixHighIcon from '@mui/icons-material/AutoFixHigh'; import MapIcon from '@mui/icons-material/Map'; import { useRouter } from "next/navigation"; import { updateFileAction } from "./_actions"; import { getMetadataPreviewAction } from "@/app/dashboard/actions"; interface MetadataPair { key: string; value: string; selected: boolean; // Checkbox state isPending?: boolean; // Visual highlight for auto-extracted fields } export default function UpdateView({ fileNode, folders: availablefolders // Renaming 'folders' to 'availablefolders' }: { fileNode: any; folders: any[]; }) { const router = useRouter(); const [loading, setLoading] = useState(false); const [isExtracting, setIsExtracting] = useState(false); // 1. Initialize Basic Info const [name, setName] = useState(fileNode.name); const [description, setDescription] = useState(fileNode.description || ""); const [parentId, setParentId] = useState(fileNode.parentId || ""); // 2. Initialize Metadata from DB (all checked by default) const initialMetadata: MetadataPair[] = Object.entries(fileNode.metadata || {}) .filter(([key]) => !['type', 'mimeType'].includes(key)) .map(([key, value]) => ({ key, value: String(value), selected: true, isPending: false })); const [customMetadata, setCustomMetadata] = useState(initialMetadata); // --- MAGIC FILL LOGIC --- const handleMagicEnhance = async () => { setIsExtracting(true); try { const result = await getMetadataPreviewAction(fileNode.id); if (result.success) { // Convert extracted JSON into pending rows const extractedRows: MetadataPair[] = Object.entries(result.data ?? {}) .filter(([key]) => !['type', 'mimeType'].includes(key)) .map(([key, value]) => ({ key, value: String(value), selected: true, // Default to checked as requested isPending: true })); // Merge logic: Add only if the key doesn't already exist in our list setCustomMetadata(prev => { const existingKeys = new Set(prev.map(r => r.key)); const filteredNew = extractedRows.filter(r => !existingKeys.has(r.key)); return [...prev, ...filteredNew]; }); } } catch (err) { alert("Failed to extract metadata. Ensure service is configured correctly."); } finally { setIsExtracting(false); } }; const handleUpdate = async () => { setLoading(true); const formData = new FormData(); formData.append("id", fileNode.id); formData.append("name", name); formData.append("description", description); formData.append("parentId", parentId); // Convert array back to object, ONLY including selected/checked rows const metadataObj = customMetadata.reduce((acc, curr) => { if (curr.selected && curr.key.trim()) { acc[curr.key.trim()] = curr.value; } return acc; }, {} as Record); formData.append("customMetadata", JSON.stringify(metadataObj)); const res = await updateFileAction(formData); if (res.success) { router.push("/dashboard"); router.refresh(); } else { alert("Update failed"); setLoading(false); } }; return ( Edit File Details {/* --- MAGIC FILL BUTTON --- */} Enrich Metadata Extract tags like GPS, Author, and Dimensions from the original file. setName(e.target.value)} slotProps={{ inputLabel: { shrink: true } }} /> setParentId(e.target.value)} slotProps={{ select: { displayEmpty: true }, inputLabel: { shrink: true } }} > -- Root -- {availablefolders?.map((f: any) => ( {f.name} ))} Metadata Attributes {customMetadata.map((row, index) => ( { const updated = [...customMetadata]; updated[index].selected = e.target.checked; setCustomMetadata(updated); }} /> { const updated = [...customMetadata]; updated[index].key = e.target.value; setCustomMetadata(updated); }} /> { const updated = [...customMetadata]; updated[index].value = e.target.value; setCustomMetadata(updated); }} /> setCustomMetadata(customMetadata.filter((_, i) => i !== index))}> {/* --- GOOGLE MAPS SHORTCUT --- */} {row.key.toLowerCase().includes('latitude') && row.value && ( )} ))} setDescription(e.target.value)} slotProps={{ inputLabel: { shrink: true } }} /> ); }