-
Notifications
You must be signed in to change notification settings - Fork 3
feat: add remote folder picker to file sync GUI #127
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5b4d965
feat: add remote folder picker to file sync GUI
ethanndickson c148b67
review
ethanndickson 1dfafa8
review
ethanndickson a553fb6
rebase
ethanndickson d0133ce
replace binding with class
ethanndickson fc587b8
review
ethanndickson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
252 changes: 252 additions & 0 deletions
252
Coder-Desktop/Coder-Desktop/Views/FileSync/FilePicker.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,252 @@ | ||
import CoderSDK | ||
import Foundation | ||
import SwiftUI | ||
|
||
struct FilePicker: View { | ||
@Environment(\.dismiss) var dismiss | ||
@StateObject private var model: FilePickerModel | ||
@State private var selection: FilePickerItemModel.ID? | ||
|
||
@Binding var outputAbsPath: String | ||
|
||
let inspection = Inspection<Self>() | ||
|
||
init( | ||
host: String, | ||
outputAbsPath: Binding<String> | ||
) { | ||
_model = StateObject(wrappedValue: FilePickerModel(host: host)) | ||
_outputAbsPath = outputAbsPath | ||
} | ||
|
||
var body: some View { | ||
VStack(spacing: 0) { | ||
if model.rootIsLoading { | ||
Spacer() | ||
ProgressView() | ||
.controlSize(.large) | ||
Spacer() | ||
} else if let loadError = model.error { | ||
Text("\(loadError.description)") | ||
.font(.headline) | ||
.foregroundColor(.red) | ||
.multilineTextAlignment(.center) | ||
.frame(maxWidth: .infinity, maxHeight: .infinity) | ||
.padding() | ||
} else { | ||
List(selection: $selection) { | ||
ForEach(model.rootFiles) { rootItem in | ||
FilePickerItem(item: rootItem) | ||
} | ||
}.contextMenu( | ||
forSelectionType: FilePickerItemModel.ID.self, | ||
menu: { _ in }, | ||
primaryAction: { selections in | ||
// Per the type of `selection`, this will only ever be a set of | ||
// one item. | ||
let files = model.findFilesByIds(ids: selections) | ||
files.forEach { file in withAnimation { file.isExpanded.toggle() } } | ||
} | ||
).listStyle(.sidebar) | ||
} | ||
Divider() | ||
HStack { | ||
Spacer() | ||
Button("Cancel", action: { dismiss() }).keyboardShortcut(.cancelAction) | ||
Button("Select", action: submit).keyboardShortcut(.defaultAction).disabled(selection == nil) | ||
}.padding(20) | ||
} | ||
.onAppear { | ||
model.loadRoot() | ||
} | ||
.onReceive(inspection.notice) { inspection.visit(self, $0) } // ViewInspector | ||
} | ||
|
||
private func submit() { | ||
guard let selection else { return } | ||
let files = model.findFilesByIds(ids: [selection]) | ||
if let file = files.first { | ||
outputAbsPath = file.absolute_path | ||
} | ||
dismiss() | ||
} | ||
} | ||
|
||
@MainActor | ||
class FilePickerModel: ObservableObject { | ||
@Published var rootFiles: [FilePickerItemModel] = [] | ||
ethanndickson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@Published var rootIsLoading: Bool = false | ||
@Published var error: ClientError? | ||
|
||
let client: AgentClient | ||
|
||
init(host: String) { | ||
client = AgentClient(agentHost: host) | ||
} | ||
|
||
func loadRoot() { | ||
error = nil | ||
rootIsLoading = true | ||
Task { | ||
defer { rootIsLoading = false } | ||
do throws(ClientError) { | ||
rootFiles = try await client | ||
.listAgentDirectory(.init(path: [], relativity: .root)) | ||
.toModels(client: client, path: []) | ||
} catch { | ||
self.error = error | ||
} | ||
} | ||
} | ||
|
||
func findFilesByIds(ids: Set<FilePickerItemModel.ID>) -> [FilePickerItemModel] { | ||
var result: [FilePickerItemModel] = [] | ||
|
||
for id in ids { | ||
if let file = findFileByPath(path: id, in: rootFiles) { | ||
result.append(file) | ||
} | ||
} | ||
|
||
return result | ||
} | ||
|
||
private func findFileByPath(path: [String], in files: [FilePickerItemModel]?) -> FilePickerItemModel? { | ||
guard let files, !path.isEmpty else { return nil } | ||
|
||
if let file = files.first(where: { $0.name == path[0] }) { | ||
if path.count == 1 { | ||
return file | ||
} | ||
// Array slices are just views, so this isn't expensive | ||
return findFileByPath(path: Array(path[1...]), in: file.contents) | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
struct FilePickerItem: View { | ||
@ObservedObject var item: FilePickerItemModel | ||
|
||
var body: some View { | ||
Group { | ||
if item.dir { | ||
directory | ||
} else { | ||
Label(item.name, systemImage: "doc") | ||
.help(item.absolute_path) | ||
.selectionDisabled() | ||
.foregroundColor(.secondary) | ||
} | ||
} | ||
} | ||
|
||
private var directory: some View { | ||
DisclosureGroup(isExpanded: $item.isExpanded) { | ||
if let contents = item.contents { | ||
ForEach(contents) { item in | ||
FilePickerItem(item: item) | ||
} | ||
} | ||
} label: { | ||
Label { | ||
Text(item.name) | ||
ZStack { | ||
ProgressView().controlSize(.small).opacity(item.isLoading && item.error == nil ? 1 : 0) | ||
Image(systemName: "exclamationmark.triangle.fill") | ||
.opacity(item.error != nil ? 1 : 0) | ||
} | ||
} icon: { | ||
Image(systemName: "folder") | ||
}.help(item.error != nil ? item.error!.description : item.absolute_path) | ||
} | ||
} | ||
} | ||
|
||
@MainActor | ||
class FilePickerItemModel: Identifiable, ObservableObject { | ||
nonisolated let id: [String] | ||
let name: String | ||
// Components of the path as an array | ||
let path: [String] | ||
let absolute_path: String | ||
let dir: Bool | ||
|
||
let client: AgentClient | ||
|
||
@Published var contents: [FilePickerItemModel]? | ||
@Published var isLoading = false | ||
@Published var error: ClientError? | ||
@Published private var innerIsExpanded = false | ||
ethanndickson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
var isExpanded: Bool { | ||
get { innerIsExpanded } | ||
set { | ||
if !newValue { | ||
withAnimation { self.innerIsExpanded = false } | ||
ethanndickson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} else { | ||
Task { | ||
self.loadContents() | ||
} | ||
} | ||
} | ||
} | ||
|
||
init( | ||
name: String, | ||
client: AgentClient, | ||
absolute_path: String, | ||
path: [String], | ||
dir: Bool = false, | ||
contents: [FilePickerItemModel]? = nil | ||
) { | ||
self.name = name | ||
self.client = client | ||
self.path = path | ||
self.dir = dir | ||
self.absolute_path = absolute_path | ||
self.contents = contents | ||
|
||
// Swift Arrays are COW | ||
ethanndickson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
id = path | ||
} | ||
|
||
func loadContents() { | ||
self.error = nil | ||
withAnimation { isLoading = true } | ||
Task { | ||
defer { | ||
withAnimation { | ||
isLoading = false | ||
innerIsExpanded = true | ||
} | ||
} | ||
do throws(ClientError) { | ||
contents = try await client | ||
.listAgentDirectory(.init(path: path, relativity: .root)) | ||
.toModels(client: client, path: path) | ||
} catch { | ||
self.error = error | ||
} | ||
} | ||
} | ||
} | ||
|
||
extension LSResponse { | ||
@MainActor | ||
func toModels(client: AgentClient, path: [String]) -> [FilePickerItemModel] { | ||
ethanndickson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
contents.compactMap { file in | ||
// Filter dotfiles from the picker | ||
guard !file.name.hasPrefix(".") else { return nil } | ||
|
||
return FilePickerItemModel( | ||
name: file.name, | ||
client: client, | ||
absolute_path: file.absolute_path_string, | ||
path: path + [file.name], | ||
dir: file.is_dir, | ||
contents: nil | ||
) | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.