forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAPIKeyManagementView.swift
More file actions
139 lines (126 loc) · 4.4 KB
/
APIKeyManagementView.swift
File metadata and controls
139 lines (126 loc) · 4.4 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
import ComposableArchitecture
import SwiftUI
struct APIKeyManagementView: View {
let store: StoreOf<APIKeyManagement>
var body: some View {
VStack(spacing: 0) {
HStack {
Button(action: {
store.send(.closeButtonClicked)
}) {
Image(systemName: "xmark.circle.fill")
.foregroundStyle(.secondary)
.padding()
}
.buttonStyle(.plain)
Text("API Keys")
Spacer()
Button(action: {
store.send(.addButtonClicked)
}) {
Image(systemName: "plus.circle.fill")
.foregroundStyle(.secondary)
.padding()
}
.buttonStyle(.plain)
}
.background(Color(nsColor: .separatorColor))
List {
WithViewStore(store, observe: { $0.availableAPIKeyNames }) { viewStore in
ForEach(viewStore.state, id: \.self) { name in
HStack {
Text(name)
.contextMenu {
Button("Remove") {
viewStore.send(.deleteButtonClicked(name: name))
}
}
Spacer()
Button(action: {
viewStore.send(.deleteButtonClicked(name: name))
}) {
Image(systemName: "trash.fill")
.foregroundStyle(.secondary)
}
.buttonStyle(.plain)
}
}
}
}
.removeBackground()
.overlay {
WithViewStore(store, observe: { $0.availableAPIKeyNames }) { viewStore in
if viewStore.state.isEmpty {
Text("""
Empty
Add a new key by clicking the add button
""")
.multilineTextAlignment(.center)
.padding()
}
}
}
}
.focusable(false)
.frame(width: 300, height: 400)
.background(.thickMaterial)
.onAppear {
store.send(.appear)
}
.sheet(store: store.scope(
state: \.$apiKeySubmission,
action: APIKeyManagement.Action.apiKeySubmission
)) { store in
APIKeySubmissionView(store: store)
.frame(minWidth: 400)
}
}
}
struct APIKeySubmissionView: View {
let store: StoreOf<APIKeySubmission>
var body: some View {
ScrollView {
VStack(spacing: 0) {
Form {
WithViewStore(store, removeDuplicates: { $0.name == $1.name }) { viewStore in
TextField("Name", text: viewStore.$name)
}
WithViewStore(store, removeDuplicates: { $0.key == $1.key }) { viewStore in
SecureField("Key", text: viewStore.$key)
}
}.padding()
Divider()
HStack {
Spacer()
Button("Cancel") { store.send(.cancelButtonClicked) }
.keyboardShortcut(.cancelAction)
Button("Save", action: { store.send(.saveButtonClicked) })
.keyboardShortcut(.defaultAction)
}.padding()
}
}
.textFieldStyle(.roundedBorder)
}
}
class APIKeyManagementView_Preview: PreviewProvider {
static var previews: some View {
APIKeyManagementView(
store: .init(
initialState: .init(
availableAPIKeyNames: ["test1", "test2"]
),
reducer: APIKeyManagement()
)
)
}
}
class APIKeySubmissionView_Preview: PreviewProvider {
static var previews: some View {
APIKeySubmissionView(
store: .init(
initialState: .init(),
reducer: APIKeySubmission()
)
)
}
}