summaryrefslogtreecommitdiff
path: root/frontend/components/drive/DriveDirectoryView.tsx
blob: 20ed9d8a18e4c25c7017b88f33b6a9956f23a731 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { DriveLsEntry } from "@/lib/drive_types"
import { DriveDirectoryClient } from "./DriveDirectoryClient"

interface DriveDirectoryViewProps {
  path: string
  files: DriveLsEntry[]
}

// Generate breadcrumbs from path
function generateBreadcrumbs(currentPath: string) {
  if (currentPath === '/') {
    return [{ name: 'Root', path: '/drive' }]
  }
  
  const parts = currentPath.split('/').filter(Boolean)
  const breadcrumbs = [{ name: 'Root', path: '/drive' }]
  
  let accumulatedPath = ''
  parts.forEach((part, index) => {
    accumulatedPath += '/' + part
    breadcrumbs.push({
      name: decodeURIComponent(part), // Decode URL encoded characters
      path: '/drive' + accumulatedPath
    })
  })
  
  return breadcrumbs
}

// Sort files: directories first, then files, all alphabetically
function sortFiles(files: DriveLsEntry[]): DriveLsEntry[] {
  return [...files].sort((a, b) => {
    // Directories first, then files
    if (a.type === "dir" && b.type === "file") return -1;
    if (a.type === "file" && b.type === "dir") return 1;
    
    // Both same type, sort alphabetically by name (case-insensitive)
    const aName = a.path.split('/').pop() || a.path;
    const bName = b.path.split('/').pop() || b.path;
    return aName.toLowerCase().localeCompare(bName.toLowerCase());
  });
}

export function DriveDirectoryView({ path, files }: DriveDirectoryViewProps) {
  const sortedFiles = sortFiles(files)
  const breadcrumbs = generateBreadcrumbs(path)
  
  return (
    <DriveDirectoryClient 
      path={path} 
      files={sortedFiles} 
      breadcrumbs={breadcrumbs}
    />
  )
}