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