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
|
import SwiftUI
struct TokenListView: View {
@ObservedObject var viewModel: TokenViewModel
let client: NjallaClient?
let onTokenRemoved: (String) -> Void
@State private var showingAddToken = false
@State private var tokenPendingDeletion: APIToken?
var body: some View {
NavigationStack {
Group {
if let client {
content(client: client)
} else {
ContentUnavailableView("Sign in required", systemImage: "key.fill", description: Text("Add a valid Njalla API token to load account tokens."))
}
}
.navigationTitle("Tokens")
.toolbar {
if client != nil {
Button {
showingAddToken = true
} label: {
Label("Add Token", systemImage: "plus")
}
}
}
}
.fullScreenCover(isPresented: $showingAddToken) {
if let client {
NavigationStack {
TokenAddView(viewModel: viewModel, client: client)
}
}
}
.alert(deletionTitle, isPresented: deleteBinding) {
Button("Delete Token", role: .destructive) {
guard let tokenPendingDeletion, let client else { return }
Task {
guard !viewModel.isSaving else { return }
let removed = await viewModel.removeToken(tokenPendingDeletion, client: client)
if removed {
onTokenRemoved(tokenPendingDeletion.key)
}
self.tokenPendingDeletion = nil
}
}
Button("Cancel", role: .cancel) {
tokenPendingDeletion = nil
}
} message: {
Text("This action cannot be undone.")
}
.alert("Request Failed", isPresented: mutationErrorBinding) {
Button("OK", role: .cancel) {}
} message: {
Text(viewModel.mutationErrorMessage ?? "")
}
}
@ViewBuilder
private func content(client: NjallaClient) -> some View {
List {
if let errorMessage = viewModel.listErrorMessage {
Section {
InlineErrorView(message: errorMessage, retryTitle: "Retry Tokens") {
Task {
await viewModel.loadTokens(client: client)
}
}
.listRowInsets(EdgeInsets(top: 8, leading: 16, bottom: 8, trailing: 16))
}
}
if viewModel.isLoading && viewModel.tokens.isEmpty {
Section {
HStack {
Spacer()
ProgressView("Loading Tokens")
Spacer()
}
}
} else if viewModel.tokens.isEmpty {
Section {
ContentUnavailableView(
"No Tokens",
systemImage: "key.horizontal",
description: Text("No API tokens found. Create a restricted token for specific access.")
)
}
} else {
ForEach(viewModel.tokens) { token in
TokenRow(token: token, label: viewModel.tokenLabel(for: token))
.contentShape(Rectangle())
.contextMenu {
Button("Delete Token", role: .destructive) {
tokenPendingDeletion = token
}
}
.disabled(viewModel.isSaving)
.opacity(viewModel.isSaving ? 0.6 : 1)
.listRowInsets(EdgeInsets(top: 8, leading: 16, bottom: 8, trailing: 16))
}
}
}
.listStyle(.insetGrouped)
.refreshable {
await viewModel.loadTokens(client: client)
}
.overlay(alignment: .top) {
if viewModel.isLoading && !viewModel.tokens.isEmpty {
ProgressView()
.padding(.top, 8)
}
}
}
private var deletionTitle: String {
guard let tokenPendingDeletion else {
return ""
}
return "Delete token \(viewModel.tokenLabel(for: tokenPendingDeletion))?"
}
private var deleteBinding: Binding<Bool> {
Binding(
get: { tokenPendingDeletion != nil },
set: { newValue in
if !newValue {
tokenPendingDeletion = nil
}
}
)
}
private var mutationErrorBinding: Binding<Bool> {
Binding(
get: { viewModel.mutationErrorMessage != nil },
set: { newValue in
if !newValue {
viewModel.dismissMutationError()
}
}
)
}
}
private struct TokenRow: View {
let token: APIToken
let label: String
var body: some View {
VStack(alignment: .leading, spacing: 6) {
Text(label)
.font(.headline)
Text(methodsText)
.font(.subheadline)
.foregroundStyle(.secondary)
if let from = token.from, !from.isEmpty {
Text("From: \(from.joined(separator: ", "))")
.font(.subheadline)
.foregroundStyle(.secondary)
}
if let domains = token.allowedDomains, !domains.isEmpty {
Text("Domains: \(domains.joined(separator: ", "))")
.font(.subheadline)
.foregroundStyle(.secondary)
}
}
.padding(.vertical, 4)
}
private var methodsText: String {
guard let methods = token.allowedMethods, !methods.isEmpty else {
return "Methods: Unrestricted"
}
return "Methods: \(methods.joined(separator: ", "))"
}
}
|