╭ 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 = try wgt.Box(ui.Widget).init(allocator, .{ .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 = try wgt.Stack(ui.Widget).init(allocator); 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 const links = try aa.alloc([]const u8, data.repos.len); 119 for (data.repos, 0..) |repo, i| { 120 lines[i] = try std.fmt.allocPrint(aa, "{s} - {s}", .{ repo.name, repo.description }); 121 // clicking a repo opens its page; the "a:" prefix makes the web 122 // renderer emit an <a href="/repo/alice/foo"> anchor. 123 links[i] = try std.fmt.allocPrint(aa, "a:/repo/{s}/{s}", .{ data.user.name, repo.name }); 124 } 125 try list.setItems(allocator, lines, links); 126 127 try stack.children.put(allocator, list.getFocus().id, .{ .flow_box_scroll = list }); 128 } 129 130 { 131 var settings_view = try Settings.View.init(allocator, &data.settings, session); 132 errdefer settings_view.deinit(allocator); 133 try stack.children.put(allocator, settings_view.getFocus().id, .{ .home_settings = settings_view }); 134 } 135 136 { 137 var auth_view = try Auth.View.init(allocator, &data.auth, session, repos_tab_id); 138 errdefer auth_view.deinit(allocator); 139 try stack.children.put(allocator, auth_view.getFocus().id, .{ .home_auth = auth_view }); 140 } 141 142 if (session.is_terminal) { 143 var quit_view = try Quit.View.init(allocator, &data.quit, session); 144 errdefer quit_view.deinit(allocator); 145 try stack.children.put(allocator, quit_view.getFocus().id, .{ .quit = quit_view }); 146 } 147 148 try box.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 149 } 150 151 var self = View{ .box = box, .data = data, .session = session }; 152 self.getFocus().child_id = box.children.keys()[header_index]; 153 return self; 154 } 155 156 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 157 self.box.deinit(allocator); 158 } 159 160 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 161 self.clearGrid(); 162 const header = &self.box.children.values()[header_index].widget.user_header; 163 const stack = &self.box.children.values()[stack_index].widget.stack; 164 165 // each header tab maps 1:1 to a stack child by position. mirror the 166 // selection into current_page so the host can push the matching url; 167 // all user tabs share the .user parent, so this stays on the page 168 // rather than navigating. 169 if (header.getSelectedIndex()) |index| { 170 stack.getFocus().child_id = stack.children.keys()[index]; 171 const name = self.data.route_name; 172 switch (index) { 173 1 => self.session.data.current_page = .{ .user_settings = name }, 174 2 => self.session.data.current_page = .{ .user_auth = name }, 175 // the quit tab is tty-only and not a route, so leave current_page 176 // alone (nothing to mirror into the url). 177 3 => {}, 178 else => self.session.data.current_page = .{ .user = name }, 179 } 180 } 181 try self.box.build(allocator, constraint, root_focus); 182 } 183 184 pub fn input(self: *View, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 185 if (self.getFocus().child_id) |child_id| { 186 if (self.box.children.getIndex(child_id)) |current_index| { 187 const child = &self.box.children.values()[current_index].widget; 188 var index = current_index; 189 190 const Direction = enum { up, down, none }; 191 const direction: Direction = switch (key) { 192 .arrow_up => .up, 193 .arrow_down => .down, 194 .mouse => |mouse| if (mouse.action == .scroll) 195 (if (mouse.action.scroll == .up) .up else .down) 196 else 197 .none, 198 else => .none, 199 }; 200 201 switch (direction) { 202 .up => { 203 switch (child.*) { 204 .user_header => { 205 try child.input(allocator, key, root_focus); 206 }, 207 .stack => { 208 if (child.stack.getSelected()) |selected_widget| { 209 const at_top = switch (selected_widget.*) { 210 .flow_box_scroll => |*v| v.getSelectedIndex() == 0, 211 .home_settings => |*v| v.getSelectedIndex() == 0, 212 .home_auth => |*v| v.getSelectedIndex() == 0, 213 .quit => |*v| v.getSelectedIndex() == 0, 214 else => false, 215 }; 216 if (at_top) { 217 index = header_index; 218 } else { 219 try child.input(allocator, key, root_focus); 220 } 221 } 222 }, 223 else => {}, 224 } 225 }, 226 .down => { 227 switch (child.*) { 228 .user_header => { 229 index = stack_index; 230 }, 231 .stack => { 232 try child.input(allocator, key, root_focus); 233 }, 234 else => {}, 235 } 236 }, 237 .none => { 238 try child.input(allocator, key, root_focus); 239 }, 240 } 241 242 if (index != current_index) { 243 try root_focus.setFocus(self.box.children.keys()[index]); 244 } 245 } 246 } 247 } 248 249 pub fn clearGrid(self: *View) void { 250 self.box.clearGrid(); 251 } 252 253 pub fn getGrid(self: View) ?Grid { 254 return self.box.getGrid(); 255 } 256 257 pub fn getFocus(self: *View) *Focus { 258 return self.box.getFocus(); 259 } 260 }; 261