"use client";
import { useState } from "react";
import {
Button, CircularProgress, Box, Chip, IconButton, Tooltip, Typography
} from "@mui/material";
import SyncIcon from "@mui/icons-material/Sync";
import RefreshIcon from "@mui/icons-material/Refresh";
import FolderIcon from "@mui/icons-material/Folder";
import InsertDriveFileIcon from "@mui/icons-material/InsertDriveFile";
import DeleteIcon from "@mui/icons-material/Delete";
import { DataGrid, GridColDef } from "@mui/x-data-grid";
import { syncOneDrive } from "./sync-actions";
import { deleteFileAction } from "./actions"; // Un-commented and assumed active
import { useRouter } from "next/navigation";
interface DashboardViewProps {
initialFiles: any[];
user?: {
id?: string;
role?: string;
};
}
export default function DashboardView({ initialFiles, user }: DashboardViewProps) {
const [loading, setLoading] = useState(false);
const [isRefreshing, setIsRefreshing] = useState(false);
const router = useRouter();
const isAdmin = user?.role === "ADMIN";
/**
* Recursive function to build the virtual path for the "Location" column.
* Ensures we see "Projects / Project-1" instead of UUIDs or "ROOT".
*/
const getVirtualPath = (parentId: string | null): string => {
if (!parentId) return "WebCalibre";
const parent = initialFiles.find((f) => f.id === parentId);
if (!parent) return "WebCalibre";
// Recursive lookup for breadcrumbs
const prefix = parent.parentId ? `${getVirtualPath(parent.parentId)} / ` : "";
return `${prefix}${parent.name}`;
};
const handleSync = async () => {
setLoading(true);
try {
await syncOneDrive();
router.refresh();
} catch (error) {
console.error("Sync failed:", error);
alert("Failed to sync OneDrive");
} finally {
setLoading(false);
}
};
const handleRefresh = () => {
setIsRefreshing(true);
router.refresh();
// Visual feedback for the refresh action
setTimeout(() => setIsRefreshing(false), 800);
};
const handleDelete = async (id: string, name: string) => {
if (!confirm(`Are you sure you want to delete "${name}"?`)) return;
try {
await deleteFileAction(id);
router.refresh();
} catch (error: any) {
alert(error.message || "Failed to delete file");
}
};
const columns: GridColDef[] = [
{
field: "name",
headerName: "Name",
flex: 1.5,
minWidth: 250,
renderCell: (params) => (
{params.row.isFolder ? (
// Folder yellow
) : (
)}
{params.value}
)
},
{
field: "parentId",
headerName: "Location (Project)",
flex: 1,
minWidth: 200,
renderCell: (params) => {
const path = getVirtualPath(params.value);
return (
);
}
},
{
field: "type",
headerName: "Type",
width: 100,
valueGetter: (value, row) => {
// Priority: Metadata -> isFolder check -> fallback
const type = row.metadata?.type || (row.isFolder ? "FOLDER" : "FILE");
// Ensure we don't display a UUID here if the sync was messy
return type.length > 10 && row.isFolder ? "FOLDER" : type;
},
renderCell: (params) => (
{params.value}
)
},
{
field: "size",
headerName: "Size",
width: 120,
valueGetter: (value, row) => {
if (row.isFolder) return null;
return value;
},
renderCell: (params) => {
if (params.value === null) return "--";
const mb = (Number(params.value) / 1024 / 1024).toFixed(2);
return `${mb} MB`;
}
},
{
field: "actions",
headerName: "Actions",
width: 100,
sortable: false,
align: 'right',
headerAlign: 'right',
renderCell: (params) => {
const isOwner = params.row.ownerId === user?.id;
if (isAdmin || isOwner) {
return (
{
e.stopPropagation();
handleDelete(params.row.id, params.row.name);
}}
>
);
}
return null;
}
}
];
return (
: }
onClick={handleRefresh}
disabled={isRefreshing}
>
{isRefreshing ? "Refreshing..." : "Refresh List"}
: }
onClick={handleSync}
disabled={loading}
>
{loading ? "Syncing..." : "Sync OneDrive"}
);
}