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
146 lines (133 loc) · 4.76 KB
/
APIKeyManagementView.swift
File metadata and controls
146 lines (133 loc) · 4.76 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
import ComposableArchitecture
import SharedUIComponents
import SwiftUI
struct APIKeyManagementView: View {
@Perception.Bindable var store: StoreOf<APIKeyManagement>
var body: some View {
WithPerceptionTracking {
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 {
ForEach(store.availableAPIKeyNames, id: \.self) { name in
WithPerceptionTracking {
HStack {
Text(name)
.contextMenu {
Button("Remove") {
store.send(.deleteButtonClicked(name: name))
}
}
Spacer()
Button(action: {
store.send(.deleteButtonClicked(name: name))
}) {
Image(systemName: "trash.fill")
.foregroundStyle(.secondary)
}
.buttonStyle(.plain)
}
}
}
.modify { view in
if #available(macOS 13.0, *) {
view.listRowSeparator(.hidden).listSectionSeparator(.hidden)
} else {
view
}
}
}
.removeBackground()
.overlay {
if store.availableAPIKeyNames.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(item: $store.scope(
state: \.apiKeySubmission,
action: \.apiKeySubmission
)) { store in
APIKeySubmissionView(store: store)
.frame(minWidth: 400)
}
}
}
}
struct APIKeySubmissionView: View {
@Perception.Bindable var store: StoreOf<APIKeySubmission>
var body: some View {
WithPerceptionTracking {
ScrollView {
VStack(spacing: 0) {
Form {
TextField("Name", text: $store.name)
SecureField("Key", text: $store.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() }
)
)
}
}