"use client" import { useEffect } from "react" import Link from "next/link" import { useRouter } from "next/navigation" import { Building2, ChevronRight } from "lucide-react" import { DriveFolderIcon } from "@/lib/drive/drive-file-icon" import { DRIVE_ICON_BTN, DRIVE_SIDEBAR_CARET_SLOT_CLASS, DRIVE_SIDEBAR_ROW_BODY_CLASS, DRIVE_SIDEBAR_ROW_CLASS, } from "@/lib/drive/drive-chrome-classes" import { mailNavRowClass } from "@/lib/mail-chrome-classes" import { cn } from "@/lib/utils" import { folderPathFromSegments } from "@/lib/drive/drive-url" import { displayFileName } from "@/lib/drive/display-file-name" import { ancestorFolderPaths, driveFolderHref, orgRootKey, selectedFolderPath, } from "@/lib/drive/drive-sidebar-tree" import { useDriveOrgFolders, useDriveOrgList } from "@/lib/api/hooks/use-drive-queries" import type { DriveOrgFolder } from "@/lib/api/types" import { useIsMobile } from "@/hooks/use-mobile" import { useDriveUIStore } from "@/lib/stores/drive-ui-store" const INDENT_PX = 16 function OrgFolderTree({ folder, active, pathSegments, }: { folder: DriveOrgFolder active: boolean pathSegments: string[] }) { const router = useRouter() const isMobile = useIsMobile() const expandedPaths = useDriveUIStore((s) => s.expandedSidebarPaths) const toggleSidebarPath = useDriveUIStore((s) => s.toggleSidebarPath) const ensureSidebarPathsExpanded = useDriveUIStore((s) => s.ensureSidebarPathsExpanded) const rootKey = orgRootKey(folder.id) const isExpanded = expandedPaths.has(rootKey) const currentPath = active ? selectedFolderPath("org", pathSegments) : "" const isRootSelected = active && pathSegments.length === 0 const rootHref = driveFolderHref("org", "/", folder.id) const list = useDriveOrgList(folder.id, "/", 1, isExpanded) const directories = list.data?.files.filter((f) => f.type === "directory") ?? [] useEffect(() => { if (!active) return ensureSidebarPathsExpanded(ancestorFolderPaths(folderPathFromSegments(pathSegments))) ensureSidebarPathsExpanded([rootKey]) }, [active, ensureSidebarPathsExpanded, pathSegments, rootKey]) return (
{ event.preventDefault() router.push(rootHref) if (isMobile) useDriveUIStore.getState().setSidebarCollapsed(true) }} > {folder.mount_point}
{isExpanded ? directories.map((child) => ( )) : null}
) } function OrgFolderNode({ orgFolder, folderPath, depth, active, currentPath, }: { orgFolder: DriveOrgFolder folderPath: string depth: number active: boolean currentPath: string }) { const router = useRouter() const isMobile = useIsMobile() const expandedPaths = useDriveUIStore((s) => s.expandedSidebarPaths) const toggleSidebarPath = useDriveUIStore((s) => s.toggleSidebarPath) const isExpanded = expandedPaths.has(folderPath) const isSelected = active && currentPath === folderPath const href = driveFolderHref("org", folderPath, orgFolder.id) const list = useDriveOrgList(orgFolder.id, folderPath, 1, isExpanded) const directories = list.data?.files.filter((f) => f.type === "directory") ?? [] return (
{ event.preventDefault() router.push(href) if (isMobile) useDriveUIStore.getState().setSidebarCollapsed(true) }} > {displayFileName(folderPath.split("/").pop() ?? folderPath)}
{isExpanded ? directories.map((child) => ( )) : null}
) } export function DriveSidebarOrgFolders({ active, pathSegments, rootId, }: { active: boolean pathSegments: string[] rootId: string | null }) { const orgFolders = useDriveOrgFolders() const folders = orgFolders.data ?? [] const expandedPaths = useDriveUIStore((s) => s.expandedSidebarPaths) const toggleSidebarPath = useDriveUIStore((s) => s.toggleSidebarPath) const sectionKey = "/__org_section__" const isSectionExpanded = expandedPaths.has(sectionKey) if (orgFolders.isLoading) { return (
Dossiers d'organisation…
) } if (folders.length === 0) { return null } return (
{isSectionExpanded ? folders.map((folder) => ( )) : null}
) }