File size: 1,308 Bytes
a5871f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { clsx, type ClassValue } from "clsx"
import { twMerge } from "tailwind-merge"
import type { Folder, File, FileSystemNode } from '@/app/api/files/route';

export function cn(...inputs: ClassValue[]) {
  return twMerge(clsx(inputs))
}

export function findNodeByPath(node: Folder, path: string): Folder | null {
  if (node.path === path) {
    return node;
  }
  for (const child of node.children) {
    if (child.type === 'folder') {
      const found = findNodeByPath(child, path);
      if (found) {
        return found;
      }
    }
  }
  return null;
}

export function getAllFiles(node: Folder): File[] {
  let files: File[] = [];
  for (const child of node.children) {
    if (child.type === 'file') {
      files.push(child);
    } else if (child.type === 'folder') {
      files = files.concat(getAllFiles(child));
    }
  }
  return files;
}

export function getNodesByIds(node: Folder, ids: string[]): FileSystemNode[] {
    let results: FileSystemNode[] = [];
    if (ids.includes(node.id)) {
        results.push(node);
    }
    for (const child of node.children) {
        if (child.type === 'folder') {
            results = results.concat(getNodesByIds(child, ids));
        } else if (ids.includes(child.id)) {
            results.push(child);
        }
    }
    return results;
}