1
- import { useEffect , useMemo } from 'react'
1
+ import { useMemo , useState } from 'react'
2
2
import { createFileRoute } from '@tanstack/react-router'
3
3
import { FileText , Folder } from 'lucide-react'
4
-
5
- import { Button } from '@/components/ui/button'
4
+ import { createServerFn } from '@tanstack/react-start'
6
5
7
6
import type { TreeDataItem } from '@/components/ui/tree-view'
8
7
@@ -14,40 +13,53 @@ import {
14
13
15
14
import { TreeView } from '@/components/ui/tree-view'
16
15
16
+ const getAddons = createServerFn ( {
17
+ method : 'GET' ,
18
+ } ) . handler ( ( ) => {
19
+ return getAllAddOns ( 'react' , 'file-router' )
20
+ } )
21
+
22
+ const runCreateApp = createServerFn ( {
23
+ method : 'POST' ,
24
+ } ) . handler ( async ( ) => {
25
+ const { output, environment } = createMemoryEnvironment ( )
26
+ await createApp (
27
+ {
28
+ addOns : false ,
29
+ framework : 'react' ,
30
+ chosenAddOns : [ ] ,
31
+ git : true ,
32
+ mode : 'code-router' ,
33
+ packageManager : 'npm' ,
34
+ projectName : 'foo' ,
35
+ tailwind : false ,
36
+ toolchain : 'none' ,
37
+ typescript : false ,
38
+ variableValues : { } ,
39
+ } ,
40
+ {
41
+ silent : true ,
42
+ environment,
43
+ cwd : process . env . PROJECT_PATH ,
44
+ } ,
45
+ )
46
+ return output
47
+ } )
48
+
17
49
export const Route = createFileRoute ( '/' ) ( {
18
50
component : App ,
19
51
loader : async ( ) => {
20
- const { output, environment } = createMemoryEnvironment ( )
21
- await createApp (
22
- {
23
- addOns : false ,
24
- framework : 'react' ,
25
- chosenAddOns : [ ] ,
26
- git : true ,
27
- mode : 'code-router' ,
28
- packageManager : 'npm' ,
29
- projectName : 'foo' ,
30
- tailwind : false ,
31
- toolchain : 'none' ,
32
- typescript : false ,
33
- variableValues : { } ,
34
- } ,
35
- {
36
- silent : true ,
37
- environment,
38
- cwd : process . env . PROJECT_PATH ,
39
- } ,
40
- )
41
52
return {
42
- addOns : await getAllAddOns ( 'react' , 'file-router' ) ,
53
+ addOns : await getAddons ( ) ,
43
54
projectPath : process . env . PROJECT_PATH ! ,
44
- output,
55
+ output : await runCreateApp ( ) ,
45
56
}
46
57
} ,
47
58
} )
48
59
49
60
function App ( ) {
50
61
const { projectPath, output } = Route . useLoaderData ( )
62
+ const [ selectedFile , setSelectedFile ] = useState < string | null > ( null )
51
63
52
64
const tree = useMemo ( ( ) => {
53
65
const treeData : Array < TreeDataItem > = [ ]
@@ -61,43 +73,43 @@ function App() {
61
73
currentLevel = existingNode . children || [ ]
62
74
} else {
63
75
const newNode : TreeDataItem = {
64
- id : `${ file } -${ index } ` ,
76
+ id : index === parts . length - 1 ? file : `${ file } -${ index } ` ,
65
77
name : part ,
66
78
children : index < parts . length - 1 ? [ ] : undefined ,
67
79
icon :
68
80
index < parts . length - 1
69
81
? ( ) => < Folder className = "w-4 h-4 mr-2" />
70
82
: ( ) => < FileText className = "w-4 h-4 mr-2" /> ,
71
- onClick : ( ) => {
72
- console . log ( 'clicked' )
73
- console . log ( 'clicked' , newNode )
74
- } ,
83
+ onClick :
84
+ index === parts . length - 1
85
+ ? ( ) => {
86
+ setSelectedFile ( file )
87
+ }
88
+ : undefined ,
75
89
}
76
90
currentLevel . push ( newNode )
77
91
currentLevel = newNode . children !
78
92
}
79
93
} )
80
94
} )
81
- console . log ( JSON . stringify ( treeData , null ) )
82
95
return treeData
83
96
} , [ projectPath , output ] )
84
97
85
- console . log ( 'Hello world' )
86
-
87
- useEffect ( ( ) => {
88
- console . log ( 'output' , output )
89
- console . log ( 'projectPath' , projectPath )
90
- console . log ( 'tree' , tree )
91
- } , [ output , projectPath , tree ] )
92
-
93
98
return (
94
- < div className = "p-5" >
95
- < Button onClick = { ( ) => console . log ( 'clicked' ) } > Click me</ Button >
99
+ < div className = "p-5 flex flex-row" >
96
100
< TreeView
97
101
data = { tree }
98
102
defaultNodeIcon = { ( ) => < Folder className = "w-4 h-4 mr-2" /> }
99
103
defaultLeafIcon = { ( ) => < FileText className = "w-4 h-4 mr-2" /> }
104
+ className = "max-w-1/4 w-1/4 pr-2"
100
105
/>
106
+ < div className = "max-w-3/4 w-3/4 pl-2" >
107
+ < pre >
108
+ { selectedFile
109
+ ? output . files [ selectedFile ] || 'Select a file to view its content'
110
+ : null }
111
+ </ pre >
112
+ </ div >
101
113
</ div >
102
114
)
103
115
}
0 commit comments