1- import { useEffect , useMemo } from 'react'
1+ import { useMemo , useState } from 'react'
22import { createFileRoute } from '@tanstack/react-router'
33import { FileText , Folder } from 'lucide-react'
4-
5- import { Button } from '@/components/ui/button'
4+ import { createServerFn } from '@tanstack/react-start'
65
76import type { TreeDataItem } from '@/components/ui/tree-view'
87
@@ -14,40 +13,53 @@ import {
1413
1514import { TreeView } from '@/components/ui/tree-view'
1615
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+
1749export const Route = createFileRoute ( '/' ) ( {
1850 component : App ,
1951 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- )
4152 return {
42- addOns : await getAllAddOns ( 'react' , 'file-router' ) ,
53+ addOns : await getAddons ( ) ,
4354 projectPath : process . env . PROJECT_PATH ! ,
44- output,
55+ output : await runCreateApp ( ) ,
4556 }
4657 } ,
4758} )
4859
4960function App ( ) {
5061 const { projectPath, output } = Route . useLoaderData ( )
62+ const [ selectedFile , setSelectedFile ] = useState < string | null > ( null )
5163
5264 const tree = useMemo ( ( ) => {
5365 const treeData : Array < TreeDataItem > = [ ]
@@ -61,43 +73,43 @@ function App() {
6173 currentLevel = existingNode . children || [ ]
6274 } else {
6375 const newNode : TreeDataItem = {
64- id : `${ file } -${ index } ` ,
76+ id : index === parts . length - 1 ? file : `${ file } -${ index } ` ,
6577 name : part ,
6678 children : index < parts . length - 1 ? [ ] : undefined ,
6779 icon :
6880 index < parts . length - 1
6981 ? ( ) => < Folder className = "w-4 h-4 mr-2" />
7082 : ( ) => < 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 ,
7589 }
7690 currentLevel . push ( newNode )
7791 currentLevel = newNode . children !
7892 }
7993 } )
8094 } )
81- console . log ( JSON . stringify ( treeData , null ) )
8295 return treeData
8396 } , [ projectPath , output ] )
8497
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-
9398 return (
94- < div className = "p-5" >
95- < Button onClick = { ( ) => console . log ( 'clicked' ) } > Click me</ Button >
99+ < div className = "p-5 flex flex-row" >
96100 < TreeView
97101 data = { tree }
98102 defaultNodeIcon = { ( ) => < Folder className = "w-4 h-4 mr-2" /> }
99103 defaultLeafIcon = { ( ) => < FileText className = "w-4 h-4 mr-2" /> }
104+ className = "max-w-1/4 w-1/4 pr-2"
100105 />
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 >
101113 </ div >
102114 )
103115}
0 commit comments