'use client';
//src/app/dashboard/dashboard-view.tsx
import { useState } from "react";
import {
Button,
CircularProgress,
Box,
Chip,
IconButton,
Typography,
Stack,
TextField,
InputAdornment
} from "@mui/material";
import {
DataGrid,
GridColDef,
Toolbar,
QuickFilter,
QuickFilterControl,
QuickFilterClear,
} from "@mui/x-data-grid";
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 EditIcon from "@mui/icons-material/Edit";
import SearchIcon from '@mui/icons-material/Search';
import CancelIcon from '@mui/icons-material/Cancel';
import { syncOneDrive } from "./sync-actions";
import { deleteFileAction } from "./actions";
import { useRouter } from "next/navigation";
function CustomToolbar() {
return (
Library
(
),
endAdornment: state.value ? (
) : null,
},
}}
/>
)}
/>
);
}
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";
const getVirtualPath = (parentId: string | null): string => {
if (!parentId) return "WebCalibre";
const parent = initialFiles.find((f) => f.id === parentId);
if (!parent) return "WebCalibre";
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);
} finally {
setLoading(false);
}
};
const handleRefresh = () => {
setIsRefreshing(true);
router.refresh();
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 ? : }
{params.value}
)
},
{
field: "parentId",
headerName: "Location",
flex: 1,
renderCell: (params) =>
},
{ field: "description", headerName: "Description", flex: 1 },
{
field: "type",
headerName: "Type",
width: 120,
valueGetter: (value, row) => row.metadata?.type || (row.isFolder ? "Folder" : "File"),
renderCell: (params) => (
{params.value}
)
},
{
field: "size",
headerName: "Size",
width: 100,
renderCell: (params) => params.row.isFolder ? "--" : `${(Number(params.value) / 1024 / 1024).toFixed(2)} MB`
},
{
field: "actions",
headerName: "Actions",
width: 120,
align: 'right',
renderCell: (params) => {
const isOwner = params.row.ownerId === user?.id;
if (isAdmin || isOwner) {
return (
router.push(`/update/${params.row.id}`)}
title="Edit Details"
>
handleDelete(params.row.id, params.row.name)}
title="Delete"
>
);
}
return null;
}
},
{
field: "metadata_search",
headerName: "Metadata Search",
width: 0,
valueGetter: (value, row) => {
if (!row.metadata) return "";
return Object.entries(row.metadata)
.filter(([k]) => k !== 'type' && k !== 'mimeType')
.map(([k, v]) => `${k}:${v}`)
.join(" ");
}
}
];
return (
: } onClick={handleRefresh}>
Refresh List
: } onClick={handleSync} disabled={loading}>
Sync OneDrive
);
}