1- import  React  from  'react' 
1+ import  React ,   {   useCallback   }  from  'react' 
22import  {  useRootContext  }  from  '../context/RootContext' 
33import  useAdapterSelector  from  '../hooks/useAdapterSelector' 
44import  {  cn  }  from  '../lib/tailwind' 
@@ -15,15 +15,65 @@ export default function AdapterSelector() {
1515            maxFileSize, 
1616            dark, 
1717            classNames, 
18-             allowFolderUpload , 
18+             showSelectFolderButton , 
1919        } , 
2020        isAddingMore, 
2121        setIsAddingMore, 
2222        inputRef, 
23+         setFiles, 
2324    }  =  useRootContext ( ) 
2425    const  {  chosenAdapters,  handleAdapterClick,  handleInputFileChange }  = 
2526        useAdapterSelector ( ) 
2627
28+     const  handleBrowseFilesClick  =  useCallback ( ( )  =>  { 
29+         if  ( inputRef . current )  { 
30+             inputRef . current . removeAttribute ( 'webkitdirectory' ) 
31+             inputRef . current . removeAttribute ( 'directory' ) 
32+             inputRef . current . click ( ) 
33+         } 
34+     } ,  [ inputRef ] ) 
35+ 
36+     const  handleSelectFolderClick  =  useCallback ( async  ( )  =>  { 
37+         const  anyWindow  =  window  as  any 
38+         if  ( anyWindow . showDirectoryPicker )  { 
39+             try  { 
40+                 const  directoryHandle  =  await  anyWindow . showDirectoryPicker ( ) 
41+                 const  files : File [ ]  =  [ ] 
42+ 
43+                 async  function  getFiles ( dirHandle : any ,  path  =  '' )  { 
44+                     for  await  ( const  entry  of  dirHandle . values ( ) )  { 
45+                         const  newPath  =  path 
46+                             ? `${ path } ${ entry . name }  
47+                             : entry . name 
48+                         if  ( entry . kind  ===  'file' )  { 
49+                             const  file  =  await  entry . getFile ( ) 
50+                             ; ( file  as  any ) . webkitRelativePath  =  newPath 
51+                             files . push ( file ) 
52+                         }  else  if  ( entry . kind  ===  'directory' )  { 
53+                             await  getFiles ( entry ,  newPath ) 
54+                         } 
55+                     } 
56+                 } 
57+                 await  getFiles ( directoryHandle ) 
58+                 if  ( files . length  >  0 )  { 
59+                     setFiles ( files ) 
60+                     if  ( inputRef . current )  { 
61+                         inputRef . current . value  =  '' 
62+                     } 
63+                 } 
64+             }  catch  { 
65+                 // User cancelled, do nothing. 
66+             } 
67+         }  else  { 
68+             // Fallback to existing behavior 
69+             if  ( inputRef . current )  { 
70+                 inputRef . current . setAttribute ( 'webkitdirectory' ,  'true' ) 
71+                 inputRef . current . setAttribute ( 'directory' ,  'true' ) 
72+                 inputRef . current . click ( ) 
73+             } 
74+         } 
75+     } ,  [ inputRef ,  setFiles ] ) 
76+ 
2777    return  ( 
2878        < div 
2979            className = { cn ( 
@@ -84,10 +134,6 @@ export default function AdapterSelector() {
84134                data-testid = "upup-file-input" 
85135                ref = { inputRef } 
86136                multiple = { multiple } 
87-                 // Allow selecting folders when enabled (webkitdirectory works in Chromium-based browsers) 
88-                 { ...( ( allowFolderUpload 
89-                     ? {  webkitdirectory : true ,  directory : true  } 
90-                     : { } )  as  any ) } 
91137                onChange = { handleInputFileChange } 
92138            /> 
93139            < div  className = "upup-flex upup-flex-col upup-items-center upup-gap-1 upup-text-center md:upup-gap-2 md:upup-px-[30px]" > 
@@ -112,23 +158,38 @@ export default function AdapterSelector() {
112158                                    dark , 
113159                            } , 
114160                        ) } 
115-                         onClick = { ( )   =>   inputRef . current ?. click ( ) } 
161+                         onClick = { handleBrowseFilesClick } 
116162                    > 
117-                         browse
163+                         browse files 
118164                    </ button > 
119-                     { allowFolderUpload  &&  ( 
120-                         < span 
121-                             className = { cn ( 
122-                                 'upup-text-xs upup-text-[#0B0B0B] md:upup-text-sm' , 
123-                                 { 
124-                                     'upup-text-white dark:upup-text-white' :
125-                                         dark , 
126-                                 } , 
127-                             ) } 
128-                         > 
129-                             { ' ' } 
130-                             or folder
131-                         </ span > 
165+                     { showSelectFolderButton  &&  ( 
166+                         < > 
167+                             < span 
168+                                 className = { cn ( 
169+                                     'upup-text-xs upup-text-[#0B0B0B] md:upup-text-sm' , 
170+                                     { 
171+                                         'upup-text-white dark:upup-text-white' :
172+                                             dark , 
173+                                     } , 
174+                                 ) } 
175+                             > 
176+                                 { ' ' } 
177+                                 or
178+                             </ span > 
179+                             < button 
180+                                 type = "button" 
181+                                 className = { cn ( 
182+                                     'upup-cursor-pointer upup-text-xs upup-font-semibold upup-text-[#0E2ADD] md:upup-text-sm' , 
183+                                     { 
184+                                         'upup-text-[#59D1F9] dark:upup-text-[#59D1F9]' :
185+                                             dark , 
186+                                     } , 
187+                                 ) } 
188+                                 onClick = { handleSelectFolderClick } 
189+                             > 
190+                                 select a folder
191+                             </ button > 
192+                         </ > 
132193                    ) } 
133194                </ div > 
134195                < p 
0 commit comments