╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Roles.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
103 try evt.grantRole(io, allocator, users_dir, &owner_id, &repo_id, &user_id, .write, actor.author); 104 }, 105 .revoke => try evt.revokeRole(io, allocator, users_dir, &owner_id, &repo_id, &user_id, actor.author), 106 // only a listed grant changes role 107 .promote, .demote => { 108 if (role == null) return error.EventNotFound; 109 try evt.grantRole(io, allocator, users_dir, &owner_id, &repo_id, &user_id, if (action == .promote) .owner else .write, actor.author); 110 }, 111 } 112 return null; 113 } 114 115 // where an action lands: the same window of the list, or the files tab once 116 // the actor gave up their own owner role 117 pub fn landingRoute(identity: []const u8, start: usize, actor: ui.Actor, action: Action, name: []const u8) ?ui.RoutablePage { 118 const lost_ownership = (action == .revoke or action == .demote) and std.mem.eql(u8, name, actor.author.name); 119 if (lost_ownership) return ui.RoutablePage.repoFilesRoute(identity, null, "", "", 0); 120 return ui.RoutablePage.repoRolesRoute(identity, start); 121 } 122 123 pub const View = struct { 124 scroll: wgt.Scroll(ui.Widget), 125 data: *const Self, 126 session: *ui.Session, 127 128 const add_index = 0; 129 130 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 131 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .vert }); 132 errdefer box.deinit(allocator); 133 const aa = session.page_arena.allocator(); 134 const route = ui.RoutablePage.repoRolesRoute(data.identity, data.start) orelse return error.RouteTooLong; 135 const url = try route.toUrl(session.page_arena); 136 137 { 138 var add = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .horiz }); 139 errdefer add.deinit(allocator); 140 add.getFocus().kind = .{ .custom = try aa.print("form:{s}/grant", .{url}) }; 141 var name = try wgt.TextInput.init(allocator, .{ .top_label = .{ .text = " add a collaborator " }, .name = "name", .visible_width = 30, .round_corners = true, .render_content = session.is_terminal }); 142 errdefer name.deinit(allocator); 143 name.getFocus().mode = .all; 144 if (session.formFeedback(.repo_roles)) |saved| try name.setContent(allocator, saved.name); 145 add.getFocus().child_id = name.getFocus().id; 146 try add.children.put(allocator, name.getFocus().id, .{ .widget = .{ .text_input = name }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 147 try box.children.put(allocator, add.getFocus().id, .{ .widget = .{ .box = add }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 148 } 149 150 // the user rows follow the add row in the same box 151 if (data.start > 0) { 152 var previous = try Comment.linkBox(allocator, session, "← previous", ui.RoutablePage.repoRolesRoute(data.identity, data.start -| page_size) orelse return error.RouteTooLong); 153 errdefer previous.deinit(allocator); 154 try box.children.put(allocator, previous.getFocus().id, .{ .widget = .{ .text_box = previous }, .rect = null, .min_size = null }); 155 } 156 for (data.rows) |row| { 157 var row_route = route; 158 row_route.repo_roles.user = ui.RoutablePage.Array(evt.User.name_max_len).from(row.name) orelse return error.RouteTooLong; 159 const row_url = try row_route.toUrl(session.page_arena); 160 161 var row_box = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .horiz }); 162 errdefer row_box.deinit(allocator); 163 row_box.getFocus().kind = .{ .custom = try aa.print("form:{s}", .{row_url}) }; 164 { 165 const user_route = ui.RoutablePage{ .user_repos = .{ .name = ui.RoutablePage.Array(evt.User.name_max_len).from(row.name) orelse return error.RouteTooLong } }; 166 var name = try Comment.linkBox(allocator, session, row.name, user_route); 167 errdefer name.deinit(allocator); 168 name.options.bottom_label.text = if (row.role == .owner) " owner " else " collaborator "; 169 try row_box.children.put(allocator, name.getFocus().id, .{ .widget = .{ .text_box = name }, .rect = null, .min_size = null }); 170 } 171 try addButton(allocator, &row_box, "remove", try aa.print("submit:{s}/revoke", .{row_url})); 172 if (row.role == .owner) 173 try addButton(allocator, &row_box, "demote to collaborator", try aa.print("submit:{s}/demote", .{row_url})) 174 else 175 try addButton(allocator, &row_box, "promote to owner", try aa.print("submit:{s}/promote", .{row_url})); 176 row_box.getFocus().child_id = row_box.children.keys()[0]; 177 try box.children.put(allocator, row_box.getFocus().id, .{ .widget = .{ .box = row_box }, .rect = null, .min_size = null }); 178 } 179 if (data.next_start) |next_start| { 180 var next = try Comment.linkBox(allocator, session, "next →", ui.RoutablePage.repoRolesRoute(data.identity, next_start) orelse return error.RouteTooLong); 181 errdefer next.deinit(allocator); 182 try box.children.put(allocator, next.getFocus().id, .{ .widget = .{ .text_box = next }, .rect = null, .min_size = null }); 183 } 184 185 box.getFocus().child_id = box.children.keys()[add_index]; 186 var center = try ui.widget.Center.init(allocator, .{ .box = box }); 187 errdefer center.deinit(allocator); 188 return .{ 189 .scroll = try wgt.Scroll(ui.Widget).init(allocator, .{ .center = center }, .{ .direction = .vert, .web_native = !session.is_terminal, .fill = true }), 190 .data = data, 191 .session = session, 192 }; 193 } 194 195 fn addButton(allocator: std.mem.Allocator, row: *wgt.Box(ui.Widget), text: []const u8, kind: []const u8) !void { 196 var button = try wgt.TextBox.init(allocator, text, .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 197 errdefer button.deinit(allocator); 198 button.getFocus().mode = .all; 199 button.getFocus().kind = .{ .custom = kind }; 200 try row.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = null }); 201 } 202 203 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 204 self.scroll.deinit(allocator); 205 } 206 207 fn contentBox(self: *View) *wgt.Box(ui.Widget) { 208 return &self.scroll.child.center.child.box; 209 } 210 211 fn addBox(self: *View) *wgt.Box(ui.Widget) { 212 return &self.contentBox().children.values()[add_index].widget.box; 213 } 214 215 fn nameInput(self: *View) *wgt.TextInput { 216 return &self.addBox().children.values()[0].widget.text_input; 217 } 218 219 fn addActive(self: *View) bool { 220 return self.contentBox().getFocus().child_id == self.addBox().getFocus().id; 221 } 222 223 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 224 self.clearGrid(); 225 const name = self.nameInput(); 226 const failure = if (self.session.formFeedback(.repo_roles)) |saved| saved.failure else null; 227 name.options.top_label.text = if (failure) |value| switch (value) { 228 .not_found => " add a collaborator (not found) ", 229 .already_added => " add a collaborator (already added) ", 230 } else " add a collaborator "; 231 name.options.bottom_label.text = if (root_focus.grandchild_id == name.getFocus().id) " press enter " else ""; 232 // the web form handling finds the input by focus id 233 try self.session.text_inputs.put(self.session.arena.allocator(), name.getFocus().id, name); 234 // the window's height as a minimum centers rows that fit 235 try self.scroll.build(allocator, .{ 236 .min_size = .{ .width = constraint.min_size.width, .height = constraint.max_size.height orelse constraint.min_size.height }, 237 .max_size = constraint.max_size, 238 }, root_focus); 239 } 240 241 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 242 // the add row keeps every key but the vertical ones, which move between rows 243 if (self.addActive() and inp.vertDirection(key) == .none) { 244 if (key == .enter) { 245 const name = try self.nameInput().text(allocator); 246 defer allocator.free(name); 247 try self.act(allocator, .grant, name); 248 } else try self.nameInput().input(allocator, key, root_focus); 249 return; 250 } 251 252 const rows = self.contentBox(); 253 const current = rows.children.getIndex(rows.getFocus().child_id orelse return) orelse return; 254 if (inp.rowDelta(key, @intCast(rows.children.count()))) |delta| { 255 ui.widget.moveRowFocus(rows, &self.scroll, root_focus, delta); 256 return; 257 } 258 switch (key) { 259 .arrow_left, .arrow_right => _ = ui.widget.moveInSelectedRow(rows, root_focus, key == .arrow_right), 260 else => { 261 // the add row and the previous link, when there is one, sit above the rows 262 const offset: usize = add_index + 1 + @intFromBool(self.data.start > 0); 263 if (current < offset or current - offset >= self.data.rows.len) return; 264 const row = self.data.rows[current - offset]; 265 const row_box = &rows.children.values()[current].widget.box; 266 const button_id = row_box.getFocus().child_id orelse return; 267 if (!inp.activated(root_focus, button_id, key)) return; 268 const action: Action = switch (row_box.children.getIndex(button_id) orelse return) { 269 1 => .revoke, 270 2 => if (row.role == .owner) .demote else .promote, 271 else => return, 272 }; 273 try self.act(allocator, action, row.name); 274 }, 275 } 276 } 277 278 // the terminal performs the action itself; the web posts the form instead 279 fn act(self: *View, allocator: std.mem.Allocator, action: Action, name: []const u8) !void { 280 if (comptime wasm) return; 281 const io = self.session.io orelse return; 282 const users_dir = self.session.users_dir orelse return; 283 const admin_repo = self.session.admin_repo orelse return; 284 const actor = (try self.session.authorize(self.data.identity, .owner, null)) orelse return; 285 const moment = try evt.currentMoment(evt.admin_repo_opts, admin_repo); 286 const failure = perform(io, allocator, moment, users_dir, actor, self.data.identity, action, name) catch |err| switch (err) { 287 // a stale row; the refresh below shows the list as it is 288 error.EventNotFound => null, 289 else => return err, 290 }; 291 if (failure) |value| { 292 self.session.data.form_feedback = .{ .repo_roles = .{ .failure = value, .name = try self.session.arena.allocator().dupe(u8, name) } }; 293 return; 294 } 295 try self.session.navigate(landingRoute(self.data.identity, self.data.start, actor, action, name) orelse return error.RouteTooLong); 296 } 297 298 pub fn clearGrid(self: *View) void { 299 self.scroll.clearGrid(); 300 } 301 302 pub fn getGrid(self: View) ?Grid { 303 return self.scroll.getGrid(); 304 } 305 306 pub fn getFocus(self: *View) *Focus { 307 return self.scroll.getFocus(); 308 } 309 310 // up leaves the view from the add row 311 pub fn atTop(self: *View) bool { 312 return self.addActive(); 313 } 314 }; 315