'use client'; import { useState } from "react"; import { Box, Button, Typography, Paper, Stack, TextField, MenuItem, IconButton, Grid, Divider } 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 FolderIcon from "@mui/icons-material/Folder"; import { useRouter } from "next/navigation"; import { updateFileAction } from "./_actions"; interface MetadataPair { key: string; value: string; } export default function UpdateView({ fileNode, folders }: any) { const router = useRouter(); const [loading, setLoading] = 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. Parse existing JSON metadata into Key/Value array for the UI // We filter out 'type' and 'mimeType' as they are system-managed const initialMetadata = Object.entries(fileNode.metadata || {}) .filter(([key]) => !['type', 'mimeType'].includes(key)) .map(([key, value]) => ({ key, value: String(value) })); const [customMetadata, setCustomMetadata] = useState(initialMetadata); 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 for storage const metadataObj = customMetadata.reduce((acc, curr) => { if (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 {/* Name Field */} setName(e.target.value)} slotProps={{ inputLabel: { shrink: true } }} /> {/* Folder Select */} setParentId(e.target.value)} slotProps={{ select: { displayEmpty: true }, inputLabel: { shrink: true } }} > -- Root -- {folders.map((f: any) => ( {f.name} ))} {/* Custom Metadata Section */} Custom Attributes {customMetadata.map((row, index) => ( { 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))}> ))} setDescription(e.target.value)} slotProps={{ inputLabel: { shrink: true } }} /> ); }