@@ -23,43 +23,84 @@ public FolderOpener(IFileSystemAccessService fileSystem, SolutionsState state, I
2323
2424 public async Task Consume ( PromptOpenFolder message )
2525 {
26- FileSystemFileHandle [ ] ? fileHandles = null ;
26+ FileSystemDirectoryHandle ? directoryHandle = null ;
2727 try
2828 {
29- OpenFilePickerOptionsStartInWellKnownDirectory options = new ( )
29+ DirectoryPickerOptionsStartInWellKnownDirectory options = new ( )
3030 {
31- Multiple = true ,
32- StartIn = WellKnownDirectory . Downloads
31+ StartIn = WellKnownDirectory . Documents
3332 } ;
34- fileHandles = await _fileSystem . ShowOpenFilePickerAsync ( options ) ;
33+ directoryHandle = await _fileSystem . ShowDirectoryPickerAsync ( options ) ;
3534 }
36- catch ( JSException ex )
35+ catch ( JSException )
3736 {
3837 _snackbar . AddApolloNotification ( "File system api not supported by current browser" , Severity . Error ) ;
38+ return ;
3939 }
40- finally
40+
41+ if ( directoryHandle is null )
4142 {
42- if ( fileHandles != null && fileHandles ? . Length > 0 )
43- {
43+ return ;
44+ }
4445
45- var solution = new SolutionModel ( ) ;
46- foreach ( var fh in fileHandles )
47- {
48- if ( fh is not null )
49- {
50- var file = await fh . GetFileAsync ( ) ;
51- var text = await file . TextAsync ( ) ;
52- var name = await file . GetNameAsync ( ) ;
46+ try
47+ {
48+ var solutionName = await directoryHandle . GetNameAsync ( ) ;
49+ var solution = new SolutionModel ( solutionName ) ;
5350
54- if ( string . IsNullOrWhiteSpace ( solution . Name ) )
55- {
56- solution . Name = name ;
57- }
51+ await ProcessDirectoryAsync ( directoryHandle , solution , string . Empty ) ;
5852
59- solution . AddFile ( name , text ) ;
60- }
53+ if ( solution . Files . Count > 0 )
54+ {
55+ await _state . LoadSolutionAsync ( solution ) ;
56+ }
57+ else
58+ {
59+ _snackbar . AddApolloNotification ( "No files found in the selected folder" , Severity . Warning ) ;
60+ }
61+ }
62+ catch ( Exception ex )
63+ {
64+ _snackbar . AddApolloNotification ( $ "Error reading folder: { ex . Message } ", Severity . Error ) ;
65+ }
66+ }
67+
68+ private async Task ProcessDirectoryAsync (
69+ FileSystemDirectoryHandle directoryHandle ,
70+ SolutionModel solution ,
71+ string relativePath )
72+ {
73+ var handles = await directoryHandle . ValuesAsync ( ) ;
74+ foreach ( var entry in handles )
75+ {
76+ if ( entry is FileSystemFileHandle fileHandle )
77+ {
78+ try
79+ {
80+ var file = await fileHandle . GetFileAsync ( ) ;
81+ var fileName = await fileHandle . GetNameAsync ( ) ;
82+ var text = await file . TextAsync ( ) ;
83+
84+ var folderPath = string . IsNullOrEmpty ( relativePath )
85+ ? solution . Name
86+ : $ "{ solution . Name } /{ relativePath } ";
87+
88+ solution . AddFile ( fileName , folderPath , text ) ;
89+ }
90+ catch
91+ {
92+ // Skip files that can't be read as text
6193 }
6294 }
95+ else if ( entry is FileSystemDirectoryHandle subDirectoryHandle )
96+ {
97+ var subDirectoryName = await subDirectoryHandle . GetNameAsync ( ) ;
98+ var subDirectoryRelativePath = string . IsNullOrEmpty ( relativePath )
99+ ? subDirectoryName
100+ : $ "{ relativePath } /{ subDirectoryName } ";
101+
102+ await ProcessDirectoryAsync ( subDirectoryHandle , solution , subDirectoryRelativePath ) ;
103+ }
63104 }
64105 }
65106}
0 commit comments