-
Notifications
You must be signed in to change notification settings - Fork 512
Expand file tree
/
Copy pathAPIKeyModalView.swift
More file actions
189 lines (171 loc) · 6.2 KB
/
Copy pathAPIKeyModalView.swift
File metadata and controls
189 lines (171 loc) · 6.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
//
// APIKeyModalView.swift
// Demo
//
// Created by Sihao Lu on 4/7/23.
//
import SwiftUI
struct APIKeyModalView: View {
@Environment(\.dismiss) var dismiss
let isMandatory: Bool
@Binding private var apiKey: String
@Binding private var providerRaw: String
@Binding private var host: String
@Binding private var basePath: String
@State private var internalAPIKey: String
@State private var internalProvider: APIProvider
@State private var internalHost: String
@State private var internalBasePath: String
public init(
apiKey: Binding<String>,
providerRaw: Binding<String>,
host: Binding<String>,
basePath: Binding<String>,
isMandatory: Bool = true
) {
self._apiKey = apiKey
self._providerRaw = providerRaw
self._host = host
self._basePath = basePath
self._internalAPIKey = State(initialValue: apiKey.wrappedValue)
let resolvedProvider = APIProvider(rawValue: providerRaw.wrappedValue) ?? .openAI
self._internalProvider = State(initialValue: resolvedProvider)
self._internalHost = State(initialValue: host.wrappedValue.isEmpty
? (resolvedProvider.defaultHost ?? "")
: host.wrappedValue)
self._internalBasePath = State(initialValue: basePath.wrappedValue.isEmpty
? (resolvedProvider.defaultBasePath ?? "")
: basePath.wrappedValue)
self.isMandatory = isMandatory
}
private var strokeColor: Color {
#if os(iOS)
return Color(uiColor: UIColor.systemGray5)
#elseif os(macOS)
return Color(nsColor: NSColor.lightGray)
#endif
}
var body: some View {
NavigationView {
VStack(alignment: .leading, spacing: 16) {
providerPicker
hostFields
apiKeyField
if isMandatory {
HStack {
Spacer()
Button {
commitAndDismiss()
} label: {
Text("Continue").padding(8)
}
.buttonStyle(.borderedProminent)
.disabled(internalAPIKey.isEmpty || internalHost.isEmpty)
Spacer()
}
}
Spacer()
}
.padding()
.navigationTitle("Provider & API Key")
.toolbar {
ToolbarItem(placement: .primaryAction) {
if isMandatory {
EmptyView()
} else {
Button("Close") {
commitAndDismiss()
}
}
}
}
}
}
private var providerPicker: some View {
VStack(alignment: .leading, spacing: 6) {
Text("Provider").font(.caption).foregroundColor(.secondary)
Picker("Provider", selection: $internalProvider) {
ForEach(APIProvider.allCases) { provider in
Text(provider.displayName).tag(provider)
}
}
.pickerStyle(.menu)
.onChange(of: internalProvider) { _, newValue in
if let host = newValue.defaultHost { internalHost = host }
if let basePath = newValue.defaultBasePath { internalBasePath = basePath }
}
}
}
private var hostFields: some View {
VStack(alignment: .leading, spacing: 6) {
Text("Host").font(.caption).foregroundColor(.secondary)
TextField("api.openai.com", text: $internalHost)
.textFieldStyle(.roundedBorder)
.disabled(internalProvider != .custom)
.autocorrectionDisabled(true)
#if os(iOS)
.textInputAutocapitalization(.never)
#endif
Text("Base path").font(.caption).foregroundColor(.secondary)
TextField("/v1", text: $internalBasePath)
.textFieldStyle(.roundedBorder)
.disabled(internalProvider != .custom)
.autocorrectionDisabled(true)
#if os(iOS)
.textInputAutocapitalization(.never)
#endif
}
}
private var apiKeyField: some View {
VStack(alignment: .leading, spacing: 8) {
Text("API Key").font(.caption).foregroundColor(.secondary)
if internalProvider == .openAI {
Link(
"Get a key at platform.openai.com/account/api-keys",
destination: URL(string: "https://platform.openai.com/account/api-keys")!
)
.font(.caption)
}
TextEditor(text: $internalAPIKey)
.frame(height: 100)
.font(.caption)
.padding(8)
.background(
RoundedRectangle(cornerRadius: 8)
.stroke(strokeColor, lineWidth: 1)
)
.padding(4)
.background(Color.white)
.clipShape(RoundedRectangle(cornerRadius: 8))
}
}
private func commitAndDismiss() {
apiKey = internalAPIKey
providerRaw = internalProvider.rawValue
host = internalHost
basePath = internalBasePath
dismiss()
}
}
struct APIKeyModalView_Previews: PreviewProvider {
struct APIKeyModalView_PreviewsContainerView: View {
@State var apiKey = ""
@State var providerRaw = APIProvider.openAI.rawValue
@State var host = APIProvider.openAI.defaultHost ?? ""
@State var basePath = APIProvider.openAI.defaultBasePath ?? ""
let isMandatory: Bool
var body: some View {
APIKeyModalView(
apiKey: $apiKey,
providerRaw: $providerRaw,
host: $host,
basePath: $basePath,
isMandatory: isMandatory
)
}
}
static var previews: some View {
APIKeyModalView_PreviewsContainerView(isMandatory: true)
APIKeyModalView_PreviewsContainerView(isMandatory: false)
}
}