File size: 1,919 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73

'use client';

import * as React from 'react';
import { Suspense } from 'react';
import { FileBrowser } from '@/components/file-browser';
import type { FileSystemNode } from '@/app/api/files/route';
import { FileBrowserSkeleton } from '@/components/file-browser-skeleton';
import { SplashScreen } from '@/components/splash-screen';

async function getFileSystemData(): Promise<FileSystemNode[]> {
  const res = await fetch(`/api/files`, { cache: 'no-store' });
  if (!res.ok) {
    console.error('Failed to fetch file system data', res.status, res.statusText);
    throw new Error('Failed to fetch file system data');
  }
  const data = await res.json();
  return data as FileSystemNode[];
}

function FileBrowserComponent({ onDataLoaded }: { onDataLoaded: () => void }) {
  const [data, setData] = React.useState<FileSystemNode[] | null>(null);

  React.useEffect(() => {
    getFileSystemData().then(fetchedData => {
      setData(fetchedData);
      onDataLoaded();
    });
  }, [onDataLoaded]);

  if (!data) {
    return <FileBrowserSkeleton />;
  }

  const rootFolder = {
      id: 'root',
      type: 'folder' as const,
      name: 'Files',
      path: '/',
      children: data,
  };

  return <FileBrowser initialData={rootFolder} />;
}


export default function Home() {
  const [isAppLoading, setIsAppLoading] = React.useState(true);

  React.useEffect(() => {
    const timer = setTimeout(() => {
       handleDataLoaded();
    }, 2000); 

    return () => clearTimeout(timer);
  }, []);

  const handleDataLoaded = () => {
    setIsAppLoading(false);
  };

  return (
    <main>
      <SplashScreen isVisible={isAppLoading} />
      <div className={`transition-opacity duration-500 ${isAppLoading ? 'opacity-0' : 'opacity-100'}`}>
        <Suspense fallback={<FileBrowserSkeleton />}>
          <FileBrowserComponent onDataLoaded={handleDataLoaded} />
        </Suspense>
      </div>
    </main>
  );
}