╭ Branches.zig ─────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const builtin = @import("builtin"); 3 const evt = @import("../../event.zig"); 4 const ui = @import("../../ui.zig"); 5 const inp = @import("../input.zig"); 6 const Comment = @import("./Comment.zig"); 7 const xit = @import("xit"); 8 const xitui = xit.xitui; 9 const wgt = xitui.widget; 10 const layout = xitui.layout; 11 const Key = xitui.input.Key; 12 const Grid = xitui.grid.Grid; 13 const Focus = xitui.focus.Focus; 14 15 const wasm = builtin.target.cpu.arch == .wasm32; 16 17 // the repo's protected branches, which only its owners see 18 identity: []const u8, 19 names: []const []const u8, 20 21 const Self = @This(); 22 23 pub const Action = enum { protect, unprotect }; 24 25 pub fn init(arena: *std.heap.ArenaAllocator, identity: []const u8, repo: evt.Repo) !Self { 26 const aa = arena.allocator(); 27 return .{ 28 .identity = try aa.dupe(u8, identity), 29 .names = try repo.protectedBranchNames(aa), 30 }; 31 } 32 33 // protect or unprotect `branch` in the repo `identity` names as `actor`, one 34 // of its owners. returns why a protect wrote nothing, or null once written 35 pub fn perform( 36 io: std.Io, 37 allocator: std.mem.Allocator, 38 users_dir: []const u8, 39 actor: ui.Actor, 40 identity: []const u8, 41 action: Action, 42 branch: []const u8, 43 ) !?ui.Session.FormFeedback.BranchesFailure { 44 const owner_id = actor.repo_user_id orelse return error.NotFound; 45 const parsed = ui.RoutablePage.RepoIdentity.parse(identity) orelse return error.NotFound; 46 var arena = std.heap.ArenaAllocator.init(allocator); 47 defer arena.deinit(); 48 const repo_id = blk: { 49 var user_repo = (try evt.openUserRepo(io, allocator, users_dir, &owner_id)) orelse return error.NotFound; 50 defer user_repo.deinit(io, allocator); 51 const moment = (try evt.userMoment(&user_repo)) orelse return error.NotFound; 52 const found = (try evt.Repo.readByName(evt.UserDB, evt.user_repo_opts.hash, moment, &arena, parsed.name)) orelse return error.NotFound; 53 break :blk found.event_id; 54 }; 55 56 // only an existing branch can be protected, and its row links to it, so 57 // its encoded name must fit in a route 58 if (action == .protect) { 59 if ((try ui.urlEncodeRef(arena.allocator(), branch)).len > ui.RoutablePage.ref_route_max_len) return .too_long; 60 const path = try evt.repoPath(allocator, users_dir, &owner_id, &repo_id); 61 defer allocator.free(path); 62 const source = ui.RepoSource{ .path = path, .repo_kind = .xit }; 63 if (!try source.hasBranch(io, allocator, branch)) return .not_found; 64 } 65 evt.protectBranch(io, allocator, users_dir, &owner_id, &repo_id, branch, action == .protect, actor.author) catch |err| { 66 if (ui.Session.FormFeedback.BranchesFailure.fromError(err)) |failure| return failure; 67 return err; 68 }; 69 return null; 70 } 71 72 pub const View = struct { 73 scroll: wgt.Scroll(ui.Widget), 74 data: *const Self, 75 session: *ui.Session, 76 77 const add_index = 0; 78 79 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 80 const aa = session.page_arena.allocator(); 81 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .vert }); 82 errdefer box.deinit(allocator); 83 const route = ui.RoutablePage.repoBranchesRoute(data.identity) orelse return error.RouteTooLong; 84 const url = try route.toUrl(session.page_arena); 85 86 { 87 // the gap leaves a blank line between the text and the input 88 var add = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .vert, .gap = 1 }); 89 errdefer add.deinit(allocator); 90 add.getFocus().kind = .{ .custom = try aa.print("form:{s}/protect", .{url}) }; 91 { 92 var about = try ui.widget.RichText.init(allocator, &.{ 93 .{ .text = "protected branches can't be deleted or force pushed." }, 94 }); 95 errdefer about.deinit(allocator); 96 try add.children.put(allocator, about.getFocus().id, .{ .widget = .{ .rich_text = about }, .rect = null, .min_size = null }); 97 } 98 var name = try wgt.TextInput.init(allocator, .{ .top_label = .{ .text = " add a protected branch " }, .name = "name", .visible_width = 30, .round_corners = true, .render_content = session.is_terminal }); 99 errdefer name.deinit(allocator); 100 name.getFocus().mode = .all; 101 if (session.formFeedback(.repo_branches)) |saved| try name.setContent(allocator, saved.name); 102 add.getFocus().child_id = name.getFocus().id; 103 try add.children.put(allocator, name.getFocus().id, .{ .widget = .{ .text_input = name }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 104 try box.children.put(allocator, add.getFocus().id, .{ .widget = .{ .box = add }, .rect = null, .min_size = .{ .width = null, .height = 5 } }); 105 } 106 107 // the branch rows follow the add row in the same box 108 for (data.names) |branch| { 109 const encoded = try ui.urlEncodeRef(aa, branch); 110 var row_route = route; 111 row_route.repo_branches.branch = ui.RoutablePage.Array(ui.RoutablePage.ref_route_max_len).from(encoded) orelse return error.RouteTooLong; 112 const row_url = try row_route.toUrl(session.page_arena); 113 114 var row_box = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .horiz }); 115 errdefer row_box.deinit(allocator); 116 row_box.getFocus().kind = .{ .custom = try aa.print("form:{s}", .{row_url}) }; 117 { 118 const files_route = ui.RoutablePage.repoFilesRoute(data.identity, .branch, encoded, "", 0) orelse return error.RouteTooLong; 119 var name = try Comment.linkBox(allocator, session, branch, files_route); 120 errdefer name.deinit(allocator); 121 try row_box.children.put(allocator, name.getFocus().id, .{ .widget = .{ .text_box = name }, .rect = null, .min_size = null }); 122 } 123 { 124 var remove = try wgt.TextBox.init(allocator, "✕", .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 125 errdefer remove.deinit(allocator); 126 remove.getFocus().mode = .all; 127 remove.getFocus().kind = .{ .custom = try aa.print("submit:{s}/unprotect", .{row_url}) }; 128 try row_box.children.put(allocator, remove.getFocus().id, .{ .widget = .{ .text_box = remove }, .rect = null, .min_size = .{ .width = 3, .height = null } }); 129 } 130 row_box.getFocus().child_id = row_box.children.keys()[0]; 131 try box.children.put(allocator, row_box.getFocus().id, .{ .widget = .{ .box = row_box }, .rect = null, .min_size = null }); 132 } 133 134 box.getFocus().child_id = box.children.keys()[add_index]; 135 var center = try ui.widget.Center.init(allocator, .{ .box = box }); 136 errdefer center.deinit(allocator); 137 return .{ 138 .scroll = try wgt.Scroll(ui.Widget).init(allocator, .{ .center = center }, .{ .direction = .vert, .web_native = !session.is_terminal, .fill = true }), 139 .data = data, 140 .session = session, 141 }; 142 } 143 144 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 145 self.scroll.deinit(allocator); 146 } 147 148 fn contentBox(self: *View) *wgt.Box(ui.Widget) { 149 return &self.scroll.child.center.child.box; 150 } 151 152 fn addBox(self: *View) *wgt.Box(ui.Widget) { 153 return &self.contentBox().children.values()[add_index].widget.box; 154 } 155 156 fn nameInput(self: *View) *wgt.TextInput { 157 return &self.addBox().children.values()[1].widget.text_input; 158 } 159 160 fn addActive(self: *View) bool { 161 return self.contentBox().getFocus().child_id == self.addBox().getFocus().id; 162 } 163 164 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 165 self.clearGrid(); 166 try self.labelAddInput(root_focus); 167 // the window's height as a minimum centers rows that fit 168 try self.scroll.build(allocator, .{ 169 .min_size = .{ .width = constraint.min_size.width, .height = constraint.max_size.height orelse constraint.min_size.height }, 170 .max_size = constraint.max_size, 171 }, root_focus); 172 } 173 174 // label the add input for this frame and register it for the web form 175 // handling 176 fn labelAddInput(self: *View, root_focus: *Focus) !void { 177 const name = self.nameInput(); 178 const failure = if (self.session.formFeedback(.repo_branches)) |saved| saved.failure else null; 179 name.options.top_label.text = if (failure) |value| switch (value) { 180 .not_found => " add a protected branch (not found) ", 181 .already_added => " add a protected branch (already added) ", 182 .too_many => " add a protected branch (too many branches) ", 183 .too_long => " add a protected branch (name too long) ", 184 } else " add a protected branch "; 185 name.options.bottom_label.text = if (root_focus.grandchild_id == name.getFocus().id) " press enter " else ""; 186 try self.session.text_inputs.put(self.session.arena.allocator(), name.getFocus().id, name); 187 } 188 189 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 190 // the add row keeps every key but the vertical ones, which move between rows 191 if (self.addActive() and inp.vertDirection(key) == .none) { 192 if (key == .enter) { 193 const name = try self.nameInput().text(allocator); 194 defer allocator.free(name); 195 try self.act(allocator, .protect, name); 196 } else try self.nameInput().input(allocator, key, root_focus); 197 return; 198 } 199 200 const rows = self.contentBox(); 201 const current = rows.children.getIndex(rows.getFocus().child_id orelse return) orelse return; 202 if (inp.rowDelta(key, @intCast(rows.children.count()))) |delta| { 203 ui.widget.moveRowFocus(rows, &self.scroll, root_focus, delta); 204 return; 205 } 206 switch (key) { 207 .arrow_left, .arrow_right => _ = ui.widget.moveInSelectedRow(rows, root_focus, key == .arrow_right), 208 else => { 209 // the add row sits above the rows, and a row's name is a link the host follows 210 if (current <= add_index) return; 211 const row_box = &rows.children.values()[current].widget.box; 212 const button_id = row_box.getFocus().child_id orelse return; 213 if (row_box.children.getIndex(button_id) != 1 or !inp.activated(root_focus, button_id, key)) return; 214 try self.act(allocator, .unprotect, self.data.names[current - add_index - 1]); 215 }, 216 } 217 } 218 219 // the terminal performs the action itself; the web posts the form instead 220 fn act(self: *View, allocator: std.mem.Allocator, action: Action, branch: []const u8) !void { 221 if (comptime wasm) return; 222 const io = self.session.io orelse return; 223 const users_dir = self.session.users_dir orelse return; 224 const actor = (try self.session.authorize(self.data.identity, .owner, null)) orelse return; 225 if (try perform(io, allocator, users_dir, actor, self.data.identity, action, branch)) |failure| { 226 self.session.data.form_feedback = .{ .repo_branches = .{ .failure = failure, .name = try self.session.arena.allocator().dupe(u8, branch) } }; 227 return; 228 } 229 try self.session.navigate(ui.RoutablePage.repoBranchesRoute(self.data.identity) orelse return error.RouteTooLong); 230 } 231 232 pub fn clearGrid(self: *View) void { 233 self.scroll.clearGrid(); 234 } 235 236 pub fn getGrid(self: View) ?Grid { 237 return self.scroll.getGrid(); 238 } 239 240 pub fn getFocus(self: *View) *Focus { 241 return self.scroll.getFocus(); 242 } 243 244 // up leaves the view from the add row 245 pub fn atTop(self: *View) bool { 246 return self.addActive(); 247 } 248 }; 249