"use client"; import { useRef, useState } from "react"; import { cn } from "@/lib/utils"; import { FILES_PANEL_INSET } from "@/modules/files/components/file-manager-surface"; import { useGoogleDriveFiles, useGoogleDriveDeleteFile, useGoogleDriveCreateFolder, useGoogleDriveUpload, buildDriveDownloadUrl, } from "@/modules/files/hooks/useGoogleDriveFiles"; import type { DriveFileEntry } from "@/lib/shared/contracts/google-drive"; import { Cloud, Loader2, Trash2, FolderOpen, Download, Upload, Plus, ChevronRight, File, } from "@/components/icons/platform-icons"; type NavEntry = { id: string; name: string }; type Props = { connectionId: string; accountEmail: string; }; function formatSize(bytes: number | null): string { if (bytes == null) return ""; if (bytes < 1024) return `${bytes} B`; if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`; if (bytes < 1024 * 1024 * 1024) return `${(bytes / (1024 * 1024)).toFixed(1)} MB`; return `${(bytes / (1024 * 1024 * 1024)).toFixed(2)} GB`; } function formatDate(iso: string | null): string { if (!iso) return ""; return new Date(iso).toLocaleDateString(undefined, { year: "numeric", month: "short", day: "numeric" }); } function isGoogleNative(mimeType: string): boolean { return mimeType.startsWith("application/vnd.google-apps."); } export function GoogleDrivePanel({ connectionId, accountEmail }: Props) { const [navStack, setNavStack] = useState([{ id: "root", name: "My Drive" }]); const [newFolderName, setNewFolderName] = useState(""); const [showNewFolder, setShowNewFolder] = useState(false); const [deleteConfirm, setDeleteConfirm] = useState(null); const uploadRef = useRef(null); const current = navStack[navStack.length - 1]; const folderId = current.id; const browseQuery = useGoogleDriveFiles(connectionId, folderId); const deleteFile = useGoogleDriveDeleteFile(connectionId, folderId); const createFolder = useGoogleDriveCreateFolder(connectionId, folderId); const uploadFiles = useGoogleDriveUpload(connectionId, folderId); function navigateInto(entry: DriveFileEntry) { setNavStack((prev) => [...prev, { id: entry.id, name: entry.name }]); } function navigateTo(index: number) { setNavStack((prev) => prev.slice(0, index + 1)); } function handleUpload(files: FileList | null) { if (!files || files.length === 0) return; void uploadFiles.mutate({ files: Array.from(files) }); } function handleCreateFolder() { const name = newFolderName.trim(); if (!name) return; createFolder.mutate( { parentId: folderId, name }, { onSuccess: () => { setNewFolderName(""); setShowNewFolder(false); }, }, ); } function handleDelete(entry: DriveFileEntry) { deleteFile.mutate(entry.id, { onSuccess: () => setDeleteConfirm(null) }); } const entries = browseQuery.data?.entries ?? []; return (
{/* Header */}
{/* Breadcrumb */} {/* Toolbar actions */}
handleUpload(e.target.files)} />
{/* New folder input */} {showNewFolder && (
setNewFolderName(e.target.value)} onKeyDown={(e) => { if (e.key === "Enter") handleCreateFolder(); if (e.key === "Escape") { setShowNewFolder(false); setNewFolderName(""); } }} placeholder="Folder name" className="h-7 flex-1 rounded-md border border-glass-border/60 bg-background/40 px-2 text-[12px] text-foreground placeholder:text-muted-foreground/50 focus:outline-none focus:ring-1 focus:ring-primary/40" />
)} {/* Content */}
{browseQuery.isLoading ? (
) : browseQuery.isError ? (

{browseQuery.error instanceof Error ? browseQuery.error.message : "Failed to load"}

) : entries.length === 0 ? (

This folder is empty

) : (
{entries.map((entry) => (
{/* Icon */} {entry.isFolder ? ( ) : ( )} {/* Name + meta */} {/* Actions */}
{!entry.isFolder && !isGoogleNative(entry.mimeType) && ( )} {!entry.isFolder && isGoogleNative(entry.mimeType) && entry.webViewLink && ( )}
))}
)}
{/* Footer */}

{accountEmail}

{/* Delete confirm overlay */} {deleteConfirm && (

Delete “{deleteConfirm.name}”?

This will move the file to Google Drive Trash.

)}
); }