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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
|
import SwiftUI
struct RepositoryACLView: View {
let repository: RepositorySummary
let client: SRHTClient
let showsDoneButton: Bool
@Environment(\.dismiss) private var dismiss
@State private var viewModel: RepositoryACLViewModel?
@State private var pendingDeletion: RepositoryACLEntry?
@State private var showAddSheet = false
var body: some View {
Group {
if let viewModel {
content(viewModel)
} else {
SRHTLoadingStateView(message: "Loading access…")
}
}
.navigationTitle("Access")
.navigationBarTitleDisplayMode(.inline)
.toolbar {
if showsDoneButton {
ToolbarItem(placement: .cancellationAction) {
Button("Done") { dismiss() }
}
}
if viewModel != nil {
ToolbarItem(placement: .primaryAction) {
Button {
showAddSheet = true
} label: {
Image(systemName: "plus")
}
.accessibilityLabel("Add User")
}
}
}
.task {
if viewModel == nil {
let service = RepositoryACLService(client: client, service: repository.service)
let vm = RepositoryACLViewModel(repository: repository, service: service)
viewModel = vm
await vm.load()
}
}
}
@ViewBuilder
private func content(_ viewModel: RepositoryACLViewModel) -> some View {
@Bindable var vm = viewModel
Group {
if viewModel.isLoading && !viewModel.hasEntries && viewModel.loadError == nil {
SRHTLoadingStateView(message: "Loading access…")
} else if let loadError = viewModel.loadError, !viewModel.hasEntries {
SRHTErrorStateView(
title: "Couldn't Load Access",
message: loadError,
retryAction: { await viewModel.load() }
)
} else {
List {
if viewModel.visibleEntries.isEmpty {
ContentUnavailableView {
Label("No Additional Access", systemImage: "person.2.slash")
} description: {
Text("Only the repository owner currently has access.")
}
.frame(maxWidth: .infinity)
.listRowBackground(Color.clear)
} else {
Section {
ForEach(viewModel.visibleEntries) { entry in
RepositoryACLEntryRow(
entry: entry,
isUpdating: viewModel.isUpdating(entry),
isDeleting: viewModel.isDeleting(entry),
onSelectMode: { mode in
Task { await viewModel.updatePermission(for: entry, to: mode) }
},
onDelete: {
pendingDeletion = entry
}
)
}
}
}
}
.listStyle(.insetGrouped)
.refreshable {
await viewModel.load()
}
}
}
.srhtErrorBanner(error: $vm.error)
.alert("Remove Access?", isPresented: Binding(
get: { pendingDeletion != nil },
set: { isPresented in
if !isPresented {
pendingDeletion = nil
}
}
)) {
Button("Cancel", role: .cancel) {}
Button("Remove Access", role: .destructive) {
guard let entry = pendingDeletion else { return }
Task {
await viewModel.removeEntry(entry)
pendingDeletion = nil
}
}
} message: {
if let entry = pendingDeletion {
Text("\(entry.entity.canonicalName) will lose \(entry.mode.displayName.lowercased()) access to this repository.")
}
}
.sheet(isPresented: $showAddSheet) {
NavigationStack {
RepositoryACLAddUserView(viewModel: viewModel) {
showAddSheet = false
}
}
}
}
}
private struct RepositoryACLEntryRow: View {
let entry: RepositoryACLEntry
let isUpdating: Bool
let isDeleting: Bool
let onSelectMode: (AccessMode) -> Void
let onDelete: () -> Void
var body: some View {
HStack(alignment: .center, spacing: 12) {
Text(entry.entity.canonicalName)
.font(.body.monospaced())
.lineLimit(2)
.truncationMode(.middle)
.frame(maxWidth: .infinity, alignment: .leading)
if isUpdating || isDeleting {
ProgressView()
.controlSize(.small)
}
Menu {
ForEach(AccessMode.allCases, id: \.self) { mode in
Button {
onSelectMode(mode)
} label: {
if mode == entry.mode {
Label(mode.displayName, systemImage: "checkmark")
} else {
Text(mode.displayName)
}
}
}
} label: {
Text(entry.mode.shortLabel)
.font(.caption.monospaced())
.foregroundStyle(.secondary)
.padding(.horizontal, 10)
.padding(.vertical, 6)
.background(.quaternary, in: Capsule())
}
.disabled(isUpdating || isDeleting)
}
.swipeActions(edge: .trailing, allowsFullSwipe: false) {
Button(role: .destructive) {
onDelete()
} label: {
Label("Remove", systemImage: "trash")
}
.disabled(isUpdating || isDeleting)
}
}
}
private struct RepositoryACLAddUserView: View {
@Environment(\.dismiss) private var dismiss
@Bindable var viewModel: RepositoryACLViewModel
let onAdded: () -> Void
var body: some View {
Form {
Section("User") {
TextField("Username or ~username", text: $viewModel.addUsername)
.autocorrectionDisabled()
.textInputAutocapitalization(.never)
if let validation = inlineValidationMessage {
Text(validation)
.font(.caption)
.foregroundStyle(.secondary)
}
}
Section("Permission") {
Picker("Permission", selection: $viewModel.addMode) {
ForEach(AccessMode.allCases, id: \.self) { mode in
Text(mode.shortLabel).tag(mode)
}
}
.pickerStyle(.segmented)
}
}
.navigationTitle("Add User")
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .cancellationAction) {
Button("Cancel") {
dismiss()
}
}
ToolbarItem(placement: .confirmationAction) {
Button("Add") {
Task {
if await viewModel.addEntry() {
onAdded()
}
}
}
.disabled(!viewModel.canSubmitNewEntry)
}
}
}
private var inlineValidationMessage: String? {
let trimmed = viewModel.addUsername.trimmingCharacters(in: .whitespacesAndNewlines)
guard !trimmed.isEmpty else { return nil }
return viewModel.addValidationMessage
}
}
|