╭ User.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const evt = @import("../event.zig"); 3 const ui = @import("../ui.zig"); 4 const xit = @import("xit"); 5 const hash = xit.hash; 6 const xitui = xit.xitui; 7 const wgt = xitui.widget; 8 const layout = xitui.layout; 9 const inp = xitui.input; 10 const Grid = xitui.grid.Grid; 11 const Focus = xitui.focus.Focus; 12 13 pub const Header = @import("./User/Header.zig"); 14 pub const Settings = @import("./Settings.zig"); 15 pub const Auth = @import("./Auth.zig"); 16 pub const Quit = @import("./Quit.zig"); 17 18 header: Header, 19 user: evt.User.Safe, 20 repos: []const evt.Repo, 21 settings: Settings, 22 auth: Auth, 23 quit: Quit, 24 route_name: ui.RoutablePage.Array(evt.User.name_max_len), 25 26 const Self = @This(); 27 28 pub fn init( 29 arena: *std.heap.ArenaAllocator, 30 haxy_moment: evt.AdminDB.HashMap(.read_only), 31 name: ui.RoutablePage.Array(evt.User.name_max_len), 32 ) !Self { 33 const DB = evt.AdminDB; 34 const hash_kind = evt.admin_repo_opts.hash; 35 36 // a route identifies a user by name; resolve it to the user's event id via 37 // the name->user-id index, which everything below keys off of. 38 const name_to_user_id_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "name->user-id")) orelse return error.NotFound; 39 const name_to_user_id = try DB.HashMap(.read_only).init(name_to_user_id_cursor); 40 const user_id_cursor = try name_to_user_id.getCursor(hash.hashInt(hash_kind, name.slice())) orelse return error.NotFound; 41 var user_id_buf: [evt.event_id_size]u8 = undefined; 42 _ = try user_id_cursor.readBytes(&user_id_buf); 43 const user_id: []const u8 = &user_id_buf; 44 45 const user = (try evt.User.readById(DB, hash_kind, haxy_moment, arena, user_id)) orelse return error.NotFound; 46 47 var repos: std.ArrayList(evt.Repo) = .empty; 48 49 // the user-id->repos index maps each user to the set of repo event ids they 50 // own; it only exists once a repo has been consumed, so a user with no repos 51 // simply yields an empty list. 52 if (try haxy_moment.getCursor(hash.hashInt(hash_kind, "user-id->repos"))) |user_id_to_repos_cursor| { 53 const user_id_to_repos = try DB.HashMap(.read_only).init(user_id_to_repos_cursor); 54 if (try user_id_to_repos.getCursor(hash.hashInt(hash_kind, user_id))) |user_repos_cursor| { 55 const user_repos = try DB.CountedHashSet(.read_only).init(user_repos_cursor); 56 57 const event_id_to_repo_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "event-id->repo")) orelse return error.NotFound; 58 const event_id_to_repo = try DB.HashMap(.read_only).init(event_id_to_repo_cursor); 59 60 var repos_iter = try user_repos.iterator(); 61 while (try repos_iter.next()) |kv_cursor| { 62 const kv = try kv_cursor.readKeyValuePair(); 63 const repo_cursor = try event_id_to_repo.getCursor(kv.hash) orelse continue; 64 const repo_map = try DB.HashMap(.read_only).init(repo_cursor); 65 const repo_event = try evt.read(evt.Repo, DB, hash_kind, arena, repo_map); 66 try repos.append(arena.allocator(), repo_event); 67 } 68 } 69 } 70 71 return .{ 72 .header = try Header.init(arena, user.name), 73 .user = evt.User.Safe.init(user), 74 .repos = repos.items, 75 .settings = Settings.init(), 76 .auth = Auth.init(), 77 .quit = Quit.init(), 78 .route_name = name, 79 }; 80 } 81 82 pub const View = struct { 83 box: wgt.Box(ui.Widget), 84 data: *const Self, 85 session: *ui.Session, 86 87 const header_index: usize = 0; 88 const stack_index: usize = 1; 89 90 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 91 var box = wgt.Box(ui.Widget).init(.{ .border_style = null, .rounded_corners = true, .direction = .vert }); 92 errdefer box.deinit(allocator); 93 94 // build the header first so we can grab the repos-tab id for the auth 95 // view (it focuses there after login). 96 var repos_tab_id: usize = undefined; 97 { 98 var header_view = try Header.View.init(allocator, &data.header, session); 99 errdefer header_view.deinit(allocator); 100 repos_tab_id = header_view.tab_ids.keys()[0]; 101 try box.children.put(allocator, header_view.getFocus().id, .{ .widget = .{ .user_header = header_view }, .rect = null, .min_size = null }); 102 } 103 104 { 105 var stack = wgt.Stack(ui.Widget).init(); 106 errdefer stack.deinit(allocator); 107 108 // repos list — the default tab 109 { 110 var list = try ui.FlowBox.Scroll.init(allocator, .{}); 111 errdefer list.deinit(allocator); 112 113 var arena = std.heap.ArenaAllocator.init(allocator); 114 defer arena.deinit(); 115 const aa = arena.allocator(); 116 117 const lines = try aa.alloc([]const u8, data.repos.len); 118 for (data.repos, 0..) |repo, i| { 119 lines[i] = try std.fmt.allocPrint(aa, "{s} - {s}", .{ repo.name, repo.description }); 120 } 121 try list.setItems(allocator, lines, null); 122 123 try stack.children.put(allocator, list.getFocus().id, .{ .flow_box_scroll = list }); 124 } 125 126 { 127 var settings_view = try Settings.View.init(allocator, &data.settings, session); 128 errdefer settings_view.deinit(allocator); 129 try stack.children.put(allocator, settings_view.getFocus().id, .{ .home_settings = settings_view }); 130 } 131 132 { 133 var auth_view = try Auth.View.init(allocator, &data.auth, session, repos_tab_id); 134 errdefer auth_view.deinit(allocator); 135 try stack.children.put(allocator, auth_view.getFocus().id, .{ .home_auth = auth_view }); 136 } 137 138 if (session.is_terminal) { 139 var quit_view = try Quit.View.init(allocator, &data.quit, session); 140 errdefer quit_view.deinit(allocator); 141 try stack.children.put(allocator, quit_view.getFocus().id, .{ .quit = quit_view }); 142 } 143 144 try box.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 145 } 146 147 var self = View{ .box = box, .data = data, .session = session }; 148 self.getFocus().child_id = box.children.keys()[header_index]; 149 return self; 150 } 151 152 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 153 self.box.deinit(allocator); 154 } 155 156 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 157 self.clearGrid(); 158 const header = &self.box.children.values()[header_index].widget.user_header; 159 const stack = &self.box.children.values()[stack_index].widget.stack; 160 161 // each header tab maps 1:1 to a stack child by position. mirror the 162 // selection into current_page so the host can push the matching url; 163 // all user tabs share the .user parent, so this stays on the page 164 // rather than navigating. 165 if (header.getSelectedIndex()) |index| { 166 stack.getFocus().child_id = stack.children.keys()[index]; 167 const name = self.data.route_name; 168 switch (index) { 169 1 => self.session.data.current_page = .{ .user_settings = name }, 170 2 => self.session.data.current_page = .{ .user_auth = name }, 171 // the quit tab is tty-only and not a route, so leave current_page 172 // alone (nothing to mirror into the url). 173 3 => {}, 174 else => self.session.data.current_page = .{ .user = name }, 175 } 176 } 177 try self.box.build(allocator, constraint, root_focus); 178 } 179 180 pub fn input(self: *View, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 181 if (self.getFocus().child_id) |child_id| { 182 if (self.box.children.getIndex(child_id)) |current_index| { 183 const child = &self.box.children.values()[current_index].widget; 184 var index = current_index; 185 186 const Direction = enum { up, down, none }; 187 const direction: Direction = switch (key) { 188 .arrow_up => .up, 189 .arrow_down => .down, 190 .mouse => |mouse| if (mouse.action == .scroll) 191 (if (mouse.action.scroll == .up) .up else .down) 192 else 193 .none, 194 else => .none, 195 }; 196 197 switch (direction) { 198 .up => { 199 switch (child.*) { 200 .user_header => { 201 try child.input(allocator, key, root_focus); 202 }, 203 .stack => { 204 if (child.stack.getSelected()) |selected_widget| { 205 const at_top = switch (selected_widget.*) { 206 .flow_box_scroll => |*v| v.getSelectedIndex() == 0, 207 .home_settings => |*v| v.getSelectedIndex() == 0, 208 .home_auth => |*v| v.getSelectedIndex() == 0, 209 .quit => |*v| v.getSelectedIndex() == 0, 210 else => false, 211 }; 212 if (at_top) { 213 index = header_index; 214 } else { 215 try child.input(allocator, key, root_focus); 216 } 217 } 218 }, 219 else => {}, 220 } 221 }, 222 .down => { 223 switch (child.*) { 224 .user_header => { 225 index = stack_index; 226 }, 227 .stack => { 228 try child.input(allocator, key, root_focus); 229 }, 230 else => {}, 231 } 232 }, 233 .none => { 234 try child.input(allocator, key, root_focus); 235 }, 236 } 237 238 if (index != current_index) { 239 try root_focus.setFocus(self.box.children.keys()[index]); 240 } 241 } 242 } 243 } 244 245 pub fn clearGrid(self: *View) void { 246 self.box.clearGrid(); 247 } 248 249 pub fn getGrid(self: View) ?Grid { 250 return self.box.getGrid(); 251 } 252 253 pub fn getFocus(self: *View) *Focus { 254 return self.box.getFocus(); 255 } 256 }; 257