|
| 1 | +import SwiftUI |
| 2 | +import VPNLib |
| 3 | + |
| 4 | +struct FileSyncConfig<VPN: VPNService, FS: FileSyncDaemon>: View { |
| 5 | + @EnvironmentObject var vpn: VPN |
| 6 | + @EnvironmentObject var fileSync: FS |
| 7 | + |
| 8 | + @State private var selection: FileSyncSession.ID? |
| 9 | + @State private var addingNewSession: Bool = false |
| 10 | + @State private var editingSession: FileSyncSession? |
| 11 | + |
| 12 | + @State private var loading: Bool = false |
| 13 | + @State private var deleteError: DaemonError? |
| 14 | + |
| 15 | + var body: some View { |
| 16 | + Group { |
| 17 | + Table(fileSync.sessionState, selection: $selection) { |
| 18 | + TableColumn("Local Path") { |
| 19 | + Text($0.alphaPath).help($0.alphaPath) |
| 20 | + }.width(min: 200, ideal: 240) |
| 21 | + TableColumn("Workspace", value: \.agentHost) |
| 22 | + .width(min: 100, ideal: 120) |
| 23 | + TableColumn("Remote Path", value: \.betaPath) |
| 24 | + .width(min: 100, ideal: 120) |
| 25 | + TableColumn("Status") { $0.status.body } |
| 26 | + .width(min: 80, ideal: 100) |
| 27 | + TableColumn("Size") { item in |
| 28 | + Text(item.size) |
| 29 | + } |
| 30 | + .width(min: 60, ideal: 80) |
| 31 | + } |
| 32 | + .contextMenu(forSelectionType: FileSyncSession.ID.self, menu: { _ in }, |
| 33 | + primaryAction: { selectedSessions in |
| 34 | + if let session = selectedSessions.first { |
| 35 | + editingSession = fileSync.sessionState.first(where: { $0.id == session }) |
| 36 | + } |
| 37 | + }) |
| 38 | + .frame(minWidth: 400, minHeight: 200) |
| 39 | + .padding(.bottom, 25) |
| 40 | + .overlay(alignment: .bottom) { |
| 41 | + VStack(alignment: .leading, spacing: 0) { |
| 42 | + Divider() |
| 43 | + HStack(spacing: 0) { |
| 44 | + Button { |
| 45 | + addingNewSession = true |
| 46 | + } label: { |
| 47 | + Image(systemName: "plus") |
| 48 | + .frame(width: 24, height: 24) |
| 49 | + }.disabled(vpn.menuState.agents.isEmpty) |
| 50 | + Divider() |
| 51 | + Button { |
| 52 | + Task { |
| 53 | + loading = true |
| 54 | + defer { loading = false } |
| 55 | + do throws(DaemonError) { |
| 56 | + try await fileSync.deleteSessions(ids: [selection!]) |
| 57 | + } catch { |
| 58 | + deleteError = error |
| 59 | + } |
| 60 | + await fileSync.refreshSessions() |
| 61 | + selection = nil |
| 62 | + } |
| 63 | + } label: { |
| 64 | + Image(systemName: "minus").frame(width: 24, height: 24) |
| 65 | + }.disabled(selection == nil) |
| 66 | + if let selection { |
| 67 | + if let selectedSession = fileSync.sessionState.first(where: { $0.id == selection }) { |
| 68 | + Divider() |
| 69 | + Button { |
| 70 | + // TODO: Pause & Unpause |
| 71 | + } label: { |
| 72 | + switch selectedSession.status { |
| 73 | + case .paused: |
| 74 | + Image(systemName: "play").frame(width: 24, height: 24) |
| 75 | + default: |
| 76 | + Image(systemName: "pause").frame(width: 24, height: 24) |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + .buttonStyle(.borderless) |
| 83 | + } |
| 84 | + .background(.primary.opacity(0.04)) |
| 85 | + .fixedSize(horizontal: false, vertical: true) |
| 86 | + } |
| 87 | + }.sheet(isPresented: $addingNewSession) { |
| 88 | + FileSyncSessionModal<VPN, FS>() |
| 89 | + .frame(width: 700) |
| 90 | + }.sheet(item: $editingSession) { session in |
| 91 | + FileSyncSessionModal<VPN, FS>(existingSession: session) |
| 92 | + .frame(width: 700) |
| 93 | + }.alert("Error", isPresented: Binding( |
| 94 | + get: { deleteError != nil }, |
| 95 | + set: { isPresented in |
| 96 | + if !isPresented { |
| 97 | + deleteError = nil |
| 98 | + } |
| 99 | + } |
| 100 | + )) {} message: { |
| 101 | + Text(deleteError?.description ?? "An unknown error occurred.") |
| 102 | + }.task { |
| 103 | + while !Task.isCancelled { |
| 104 | + await fileSync.refreshSessions() |
| 105 | + try? await Task.sleep(for: .seconds(2)) |
| 106 | + } |
| 107 | + }.disabled(loading) |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +#if DEBUG |
| 112 | + #Preview { |
| 113 | + FileSyncConfig<PreviewVPN, PreviewFileSync>() |
| 114 | + .environmentObject(AppState(persistent: false)) |
| 115 | + .environmentObject(PreviewVPN()) |
| 116 | + .environmentObject(PreviewFileSync()) |
| 117 | + } |
| 118 | +#endif |
0 commit comments