"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 (