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