Skip to content

Commit cf56490

Browse files
committed
fix: cache selected profile lookup, fix sshInlineFields indentation
1 parent d372156 commit cf56490

1 file changed

Lines changed: 33 additions & 71 deletions

File tree

TablePro/Views/Connection/ConnectionFormView.swift

Lines changed: 33 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -454,8 +454,7 @@ struct ConnectionFormView: View { // swiftlint:disable:this type_body_length
454454
if sshEnabled {
455455
sshProfileSection
456456

457-
if let profileId = sshProfileId,
458-
let profile = SSHProfileStorage.shared.profile(for: profileId) {
457+
if let profile = selectedSSHProfile {
459458
sshProfileSummarySection(profile)
460459
} else if sshProfileId != nil {
461460
Section {
@@ -533,6 +532,11 @@ struct ConnectionFormView: View { // swiftlint:disable:this type_body_length
533532
}
534533
}
535534

535+
private var selectedSSHProfile: SSHProfile? {
536+
guard let id = sshProfileId else { return nil }
537+
return sshProfiles.first { $0.id == id }
538+
}
539+
536540
private func reloadProfiles() {
537541
sshProfiles = SSHProfileStorage.shared.loadProfiles()
538542
// If the edited/deleted profile no longer exists, clear the selection
@@ -578,8 +582,7 @@ struct ConnectionFormView: View { // swiftlint:disable:this type_body_length
578582
Group {
579583
Section(String(localized: "Server")) {
580584
if !sshConfigEntries.isEmpty {
581-
Picker(String(localized: "Config Host"), selection: $selectedSSHConfigHost)
582-
{
585+
Picker(String(localized: "Config Host"), selection: $selectedSSHConfigHost) {
583586
Text(String(localized: "Manual")).tag("")
584587
ForEach(sshConfigEntries) { entry in
585588
Text(entry.displayName).tag(entry.host)
@@ -590,30 +593,19 @@ struct ConnectionFormView: View { // swiftlint:disable:this type_body_length
590593
}
591594
}
592595
if selectedSSHConfigHost.isEmpty || sshConfigEntries.isEmpty {
593-
TextField(
594-
String(localized: "SSH Host"),
595-
text: $sshHost,
596-
prompt: Text("ssh.example.com")
597-
)
598-
}
599-
TextField(
600-
String(localized: "SSH Port"),
601-
text: $sshPort,
602-
prompt: Text("22")
603-
)
604-
TextField(
605-
String(localized: "SSH User"),
606-
text: $sshUsername,
607-
prompt: Text("username")
608-
)
596+
TextField(String(localized: "SSH Host"), text: $sshHost, prompt: Text("ssh.example.com"))
609597
}
598+
TextField(String(localized: "SSH Port"), text: $sshPort, prompt: Text("22"))
599+
TextField(String(localized: "SSH User"), text: $sshUsername, prompt: Text("username"))
600+
}
601+
610602
Section(String(localized: "Authentication")) {
611603
Picker(String(localized: "Method"), selection: $sshAuthMethod) {
612604
ForEach(SSHAuthMethod.allCases) { method in
613605
Text(method.rawValue).tag(method)
614606
}
615607
}
616-
if sshAuthMethod == .password {
608+
if sshAuthMethod == .password {
617609
SecureField(String(localized: "Password"), text: $sshPassword)
618610
} else if sshAuthMethod == .sshAgent {
619611
Picker("Agent Socket", selection: $sshAgentSocketOption) {
@@ -622,37 +614,30 @@ struct ConnectionFormView: View { // swiftlint:disable:this type_body_length
622614
}
623615
}
624616
if sshAgentSocketOption == .custom {
625-
TextField(
626-
"Custom Path",
627-
text: $customSSHAgentSocketPath,
628-
prompt: Text("/path/to/agent.sock")
629-
)
617+
TextField("Custom Path", text: $customSSHAgentSocketPath, prompt: Text("/path/to/agent.sock"))
630618
}
631619
Text("Keys are provided by the SSH agent (e.g. 1Password, ssh-agent).")
632620
.font(.caption)
633621
.foregroundStyle(.secondary)
634622
} else if sshAuthMethod == .keyboardInteractive {
635623
SecureField(String(localized: "Password"), text: $sshPassword)
636-
Text(
637-
String(localized: "Password is sent via keyboard-interactive challenge-response.")
638-
)
639-
.font(.caption)
640-
.foregroundStyle(.secondary)
624+
Text(String(localized: "Password is sent via keyboard-interactive challenge-response."))
625+
.font(.caption)
626+
.foregroundStyle(.secondary)
641627
} else {
642628
LabeledContent(String(localized: "Key File")) {
643629
HStack {
644-
TextField(
645-
"", text: $sshPrivateKeyPath, prompt: Text("~/.ssh/id_rsa"))
630+
TextField("", text: $sshPrivateKeyPath, prompt: Text("~/.ssh/id_rsa"))
646631
Button(String(localized: "Browse")) { browseForPrivateKey() }
647632
.controlSize(.small)
648633
}
649634
}
650635
SecureField(String(localized: "Passphrase"), text: $keyPassphrase)
651636
}
652-
}
637+
}
653638

654639
if sshAuthMethod == .keyboardInteractive || sshAuthMethod == .password {
655-
Section(String(localized: "Two-Factor Authentication")) {
640+
Section(String(localized: "Two-Factor Authentication")) {
656641
Picker(String(localized: "TOTP"), selection: $totpMode) {
657642
ForEach(TOTPMode.allCases) { mode in
658643
Text(mode.displayName).tag(mode)
@@ -662,44 +647,32 @@ struct ConnectionFormView: View { // swiftlint:disable:this type_body_length
662647
if totpMode == .autoGenerate {
663648
SecureField(String(localized: "TOTP Secret"), text: $totpSecret)
664649
.help(String(localized: "Base32-encoded secret from your authenticator setup"))
665-
666650
Picker(String(localized: "Algorithm"), selection: $totpAlgorithm) {
667651
ForEach(TOTPAlgorithm.allCases) { algo in
668652
Text(algo.rawValue).tag(algo)
669653
}
670654
}
671-
672655
Picker(String(localized: "Digits"), selection: $totpDigits) {
673656
Text("6").tag(6)
674657
Text("8").tag(8)
675658
}
676-
677659
Picker(String(localized: "Period"), selection: $totpPeriod) {
678660
Text("30s").tag(30)
679661
Text("60s").tag(60)
680662
}
681663
} else if totpMode == .promptAtConnect {
682-
Text(
683-
String(
684-
localized:
685-
"You will be prompted for a verification code each time you connect."
686-
)
687-
)
688-
.font(.caption)
689-
.foregroundStyle(.secondary)
664+
Text(String(localized: "You will be prompted for a verification code each time you connect."))
665+
.font(.caption)
666+
.foregroundStyle(.secondary)
690667
}
691668
}
692-
}
669+
}
693670

694671
Section {
695672
DisclosureGroup(String(localized: "Jump Hosts")) {
696673
ForEach($jumpHosts) { $jumpHost in
697674
DisclosureGroup {
698-
TextField(
699-
String(localized: "Host"),
700-
text: $jumpHost.host,
701-
prompt: Text("bastion.example.com")
702-
)
675+
TextField(String(localized: "Host"), text: $jumpHost.host, prompt: Text("bastion.example.com"))
703676
HStack {
704677
TextField(
705678
String(localized: "Port"),
@@ -710,11 +683,7 @@ struct ConnectionFormView: View { // swiftlint:disable:this type_body_length
710683
prompt: Text("22")
711684
)
712685
.frame(width: 80)
713-
TextField(
714-
String(localized: "Username"),
715-
text: $jumpHost.username,
716-
prompt: Text("admin")
717-
)
686+
TextField(String(localized: "Username"), text: $jumpHost.username, prompt: Text("admin"))
718687
}
719688
Picker(String(localized: "Auth"), selection: $jumpHost.authMethod) {
720689
ForEach(SSHJumpAuthMethod.allCases) { method in
@@ -724,9 +693,7 @@ struct ConnectionFormView: View { // swiftlint:disable:this type_body_length
724693
if jumpHost.authMethod == .privateKey {
725694
LabeledContent(String(localized: "Key File")) {
726695
HStack {
727-
TextField(
728-
"", text: $jumpHost.privateKeyPath,
729-
prompt: Text("~/.ssh/id_rsa"))
696+
TextField("", text: $jumpHost.privateKeyPath, prompt: Text("~/.ssh/id_rsa"))
730697
Button(String(localized: "Browse")) {
731698
browseForJumpHostKey(jumpHost: $jumpHost)
732699
}
@@ -745,12 +712,9 @@ struct ConnectionFormView: View { // swiftlint:disable:this type_body_length
745712
Spacer()
746713
Button {
747714
let idToRemove = jumpHost.id
748-
withAnimation {
749-
jumpHosts.removeAll { $0.id == idToRemove }
750-
}
715+
withAnimation { jumpHosts.removeAll { $0.id == idToRemove } }
751716
} label: {
752-
Image(systemName: "minus.circle.fill")
753-
.foregroundStyle(.red)
717+
Image(systemName: "minus.circle.fill").foregroundStyle(.red)
754718
}
755719
.buttonStyle(.plain)
756720
}
@@ -766,13 +730,11 @@ struct ConnectionFormView: View { // swiftlint:disable:this type_body_length
766730
Label(String(localized: "Add Jump Host"), systemImage: "plus")
767731
}
768732

769-
Text(
770-
"Jump hosts are connected in order before reaching the SSH server above. Only key and agent auth are supported for jumps."
771-
)
772-
.font(.caption)
773-
.foregroundStyle(.secondary)
774-
}
733+
Text("Jump hosts are connected in order before reaching the SSH server above. Only key and agent auth are supported for jumps.")
734+
.font(.caption)
735+
.foregroundStyle(.secondary)
775736
}
737+
}
776738
}
777739
}
778740

0 commit comments

Comments
 (0)