"use client" import { useRouter } from "next/navigation" import { FileThumbnail } from "@/components/drive/file-thumbnail" import { DriveGridView } from "@/components/drive/drive-grid-view" import { DriveListModified } from "@/components/drive/drive-list-modified" import { DriveFileContextMenu } from "@/components/drive/drive-file-context-menu" import type { DriveFileInfo } from "@/lib/api/types" import { useDriveSettingsStore } from "@/lib/stores/drive-settings-store" import { openDriveItem } from "@/lib/drive/drive-open-item" import { useDriveMutations } from "@/lib/api/hooks/use-drive-queries" import { pathRefFromRoute, type DrivePathRef } from "@/lib/api/drive-roots" import type { DriveView } from "@/lib/drive/drive-url" import type { PublicShareThumbContext } from "@/lib/api/hooks/use-public-share-preview-thumb" import { useDriveUIStore } from "@/lib/stores/drive-ui-store" import { displayFileBaseName, displayFileFormatLabel, } from "@/lib/drive/display-file-name" import { useDriveGridSelection } from "@/lib/hooks/use-drive-grid-selection" import { DRIVE_CARD_PAD_X } from "@/lib/drive/drive-chrome-classes" import { useDriveRouteRoot } from "@/lib/drive/drive-route-context" import { cn } from "@/lib/utils" function formatSize(n: number) { if (n < 1024) return `${n} o` if (n < 1024 ** 2) return `${(n / 1024).toFixed(1)} Ko` return `${(n / 1024 ** 2).toFixed(1)} Mo` } export function FileBrowser({ items, view = "files", rootId, pathRef: pathRefProp, isTrash, onOpenItem, mutations: mutationsProp, allowShare: allowShareProp, writable = true, hideFavorite = false, disableDnd = false, onDownloadItem, gridClassName, publicShare, }: { items: DriveFileInfo[] view?: DriveView rootId?: string | null pathRef?: DrivePathRef isTrash?: boolean onOpenItem?: (file: DriveFileInfo) => void mutations?: ReturnType allowShare?: boolean writable?: boolean hideFavorite?: boolean disableDnd?: boolean onDownloadItem?: (file: DriveFileInfo) => void gridClassName?: string publicShare?: PublicShareThumbContext }) { const router = useRouter() const routeRoot = useDriveRouteRoot() const viewMode = useDriveSettingsStore((s) => s.viewMode) const openPreview = useDriveUIStore((s) => s.openPreview) const pathRef = pathRefProp ?? pathRefFromRoute(view, rootId ?? null, "/") const mutationsDefault = useDriveMutations(pathRef) const mutations = mutationsProp ?? mutationsDefault const openItem = (file: DriveFileInfo) => { if (onOpenItem) { onOpenItem(file) return } openDriveItem(file, { router, openPreview, view, contextItems: items, isTrash: Boolean(isTrash), routeRoot, }) } const allowShare = allowShareProp ?? view !== "shared" const { handleItemClick } = useDriveGridSelection(items, true) if (viewMode === "grid") { return ( ) } return (
{items.map((file) => ( onDownloadItem(file) : undefined } onOpen={() => openItem(file)} onItemClick={handleItemClick} >
{displayFileBaseName(file.name, file.type === "directory")} {displayFileFormatLabel(file.name, file.type === "directory")} {file.type === "directory" ? "—" : formatSize(file.size)}
))}
) }