Cotton code
75 lines · 2.7 KB · SwiftRaw
import CottonKitimport CottonValuesimport SwiftUI/// A table whose records are made from a file: its list, whose act picks one —/// and, when a share staged a file for it, that file handed to the table's/// `create` as the path in `column`, on arrival.////// What `create` answers decides what stands here: a record naming a chat/// (`thread_id`) is replaced by that chat, as the owner ruled for a shared/// export ("take me into the chat"); any other is the record's own page,/// which asks what the import could not decide or says why it stopped.struct TableFileImportRoute: View {  let table: String  let column: String  let take: @MainActor () -> StagedShare?  @Environment(\.tableDefinitions) private var definitions  @Environment(\.records) private var store  @Environment(Navigator.self) private var navigator: Navigator?  @State private var arrival: Arrival?  private enum Arrival {    case list    case reading(String)    case record(TableRecord)    case refused(String)  }  var body: some View {    Group {      switch arrival {      case .list?:        TableListRoute(table: table)      case .record(let record)?:        if let definition = definitions[table] {          RecordOpening(definition: definition, opens: .record(record.id, record))        }      case .reading(let name)?:        PaperScreen { QuietState(title: ShellLines.line("record", "import.reading", ["name": name]), message: ShellLines.line("record", "import.reading_message")) }      case .refused(let sentence)?:        PaperScreen { QuietState(title: ShellLines.line("record", "import.not_added"), message: sentence) }      case nil:        PaperScreen { EmptyView() }  // no loader: the rows are local and land next frame      }    }    .task {      guard arrival == nil else { return }      guard let file = take() else {        arrival = .list        return      }      guard let store else {        arrival = .refused(ShellLines.line("record", "import.kept_here"))        return      }      arrival = .reading(file.name.isEmpty ? ShellLines.line("record", "import.the_export") : file.name)      do {        let made = try await store.create(table: table, data: [column: .text(file.path), "name": .text(file.name)])        guard let record = made.after else {          arrival = .list          return        }        if case .text(let thread)? = record.values["thread_id"], let navigator {          navigator.replace(with: .messagesThread(thread: thread))        } else {          arrival = .record(record)        }      } catch let refused as ActionRefused {        arrival = .refused(refused.sentence)      } catch {        arrival = .refused(ShellLines.line("record", "import.kept_here"))      }    }  }}