╭ 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 pub const page_size = 20; // how many repos one window of the repos tab shows 19 20 header: Header, 21 user: evt.User.Safe, 22 repos: []const evt.Repo, 23 repos_after: usize, // the repos window this page was built with, mirrored into the url 24 repos_next_after: ?usize, // the `after` for the "next" row, or null on the last window 25 settings: Settings, 26 auth: Auth, 27 quit: Quit, 28 route_name: ui.RoutablePage.Array(evt.User.name_max_len), 29 30 const Self = @This(); 31 32 pub fn init( 33 arena: *std.heap.ArenaAllocator, 34 haxy_moment: evt.AdminDB.HashMap(.read_only), 35 name: ui.RoutablePage.Array(evt.User.name_max_len), 36 after: usize, 37 ) !Self { 38 const DB = evt.AdminDB; 39 const hash_kind = evt.admin_repo_opts.hash; 40 41 // a route identifies a user by name; resolve it to the user's event id via 42 // the name->user-id index, which everything below keys off of. 43 const name_to_user_id_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "name->user-id")) orelse return error.NotFound; 44 const name_to_user_id = try DB.HashMap(.read_only).init(name_to_user_id_cursor); 45 const user_id_cursor = try name_to_user_id.getCursor(hash.hashInt(hash_kind, name.slice())) orelse return error.NotFound; 46 var user_id_buf: [evt.event_id_size]u8 = undefined; 47 _ = try user_id_cursor.readBytes(&user_id_buf); 48 const user_id: []const u8 = &user_id_buf; 49 50 const user = (try evt.User.readById(DB, hash_kind, haxy_moment, arena, user_id)) orelse return error.NotFound; 51 52 var repos: std.ArrayList(evt.Repo) = .empty; 53 var repos_next_after: ?usize = null; 54 55 // the user-id->repo-id-set index maps each user to a set of their repo event 56 // ids ordered by creation time (oldest first); it only exists once a repo has 57 // been consumed, so a user with no repos simply yields an empty list. 58 if (try haxy_moment.getCursor(hash.hashInt(hash_kind, "user-id->repo-id-set"))) |user_id_to_repo_id_set_cursor| { 59 const user_id_to_repo_id_set = try DB.HashMap(.read_only).init(user_id_to_repo_id_set_cursor); 60 if (try user_id_to_repo_id_set.getCursor(hash.hashInt(hash_kind, user_id))) |user_repos_cursor| { 61 const user_repos = try DB.SortedSet(.read_only).init(user_repos_cursor); 62 const count = try user_repos.count(); 63 64 const event_id_to_repo_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "event-id->repo")) orelse return error.NotFound; 65 const event_id_to_repo = try DB.HashMap(.read_only).init(event_id_to_repo_cursor); 66 67 // read the window [after, after+page_size) with one seek to the start 68 // rank, then a sequential walk. 69 const end = @min(after + page_size, count); 70 var repos_iter = try user_repos.iteratorFromIndex(after); 71 var i = after; 72 while (i < end) : (i += 1) { 73 var kv_cursor = (try repos_iter.next()) orelse break; 74 const kv = try kv_cursor.readKeyValuePair(); 75 // the set key is orderKey = [timestamp][event-id]; its trailing 76 // bytes are the repo event id. 77 var order_key: [@sizeOf(u64) + evt.event_id_size]u8 = undefined; 78 _ = try kv.key_cursor.readBytes(&order_key); 79 const event_id = order_key[@sizeOf(u64)..]; 80 const repo_cursor = try event_id_to_repo.getCursor(hash.hashInt(hash_kind, event_id)) orelse continue; 81 const repo_map = try DB.HashMap(.read_only).init(repo_cursor); 82 const repo_event = try evt.read(evt.Repo, DB, hash_kind, arena, repo_map); 83 try repos.append(arena.allocator(), repo_event); 84 } 85 repos_next_after = if (end < count) end else null; 86 } 87 } 88 89 return .{ 90 .header = try Header.init(arena, user.name), 91 .user = evt.User.Safe.init(user), 92 .repos = repos.items, 93 .repos_after = after, 94 .repos_next_after = repos_next_after, 95 .settings = Settings.init(), 96 .auth = Auth.init(), 97 .quit = Quit.init(), 98 .route_name = name, 99 }; 100 } 101 102 pub const View = struct { 103 box: wgt.Box(ui.Widget), 104 data: *const Self, 105 session: *ui.Session, 106 107 const header_index: usize = 0; 108 const stack_index: usize = 1; 109 110 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 111 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .rounded_corners = true, .direction = .vert }); 112 errdefer box.deinit(allocator); 113 114 // build the header first so we can grab the repos-tab id for the auth 115 // view (it focuses there after login). 116 var repos_tab_id: usize = undefined; 117 { 118 var header_view = try Header.View.init(allocator, &data.header, session); 119 errdefer header_view.deinit(allocator); 120 repos_tab_id = header_view.tab_ids.keys()[0]; 121 try box.children.put(allocator, header_view.getFocus().id, .{ .widget = .{ .user_header = header_view }, .rect = null, .min_size = null }); 122 } 123 124 { 125 var stack = try wgt.Stack(ui.Widget).init(allocator); 126 errdefer stack.deinit(allocator); 127 128 // repos list — the default tab 129 { 130 var list = try ui.FlowBox.Scroll.init(allocator, .{}, !session.is_terminal); 131 errdefer list.deinit(allocator); 132 133 var arena = std.heap.ArenaAllocator.init(allocator); 134 defer arena.deinit(); 135 const aa = arena.allocator(); 136 137 // a leading "previous" row off the first window, one row per repo, 138 // then a trailing "next" row when more remain. each window row 139 // navigates to the adjacent window of this user's repos. 140 var items: std.ArrayList(ui.FlowBox.Item) = .empty; 141 if (data.repos_after > 0) 142 try items.append(aa, .{ .text = "← previous", .link = try std.fmt.allocPrint(aa, "a:/user/{s}?after={d}", .{ data.user.name, data.repos_after -| page_size }) }); 143 for (data.repos) |repo| 144 // clicking a repo opens its page; the "a:" prefix makes the web 145 // renderer emit an <a href="/repo/alice/foo"> anchor. 146 try items.append(aa, .{ 147 .text = try std.fmt.allocPrint(aa, "{s} - {s}", .{ repo.name, repo.description }), 148 .link = try std.fmt.allocPrint(aa, "a:/repo/{s}/{s}", .{ data.user.name, repo.name }), 149 }); 150 if (data.repos_next_after) |next_after| 151 try items.append(aa, .{ .text = "next →", .link = try std.fmt.allocPrint(aa, "a:/user/{s}?after={d}", .{ data.user.name, next_after }) }); 152 try list.setItems(allocator, items.items); 153 154 try stack.children.put(allocator, list.getFocus().id, .{ .flow_box_scroll = list }); 155 } 156 157 { 158 var settings_view = try Settings.View.init(allocator, &data.settings, session); 159 errdefer settings_view.deinit(allocator); 160 try stack.children.put(allocator, settings_view.getFocus().id, .{ .home_settings = settings_view }); 161 } 162 163 { 164 var auth_view = try Auth.View.init(allocator, &data.auth, session, repos_tab_id); 165 errdefer auth_view.deinit(allocator); 166 try stack.children.put(allocator, auth_view.getFocus().id, .{ .home_auth = auth_view }); 167 } 168 169 if (session.is_terminal) { 170 var quit_view = try Quit.View.init(allocator, &data.quit, session); 171 errdefer quit_view.deinit(allocator); 172 try stack.children.put(allocator, quit_view.getFocus().id, .{ .quit = quit_view }); 173 } 174 175 try box.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 176 } 177 178 var self = View{ .box = box, .data = data, .session = session }; 179 self.getFocus().child_id = box.children.keys()[header_index]; 180 return self; 181 } 182 183 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 184 self.box.deinit(allocator); 185 } 186 187 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 188 self.clearGrid(); 189 const header = &self.box.children.values()[header_index].widget.user_header; 190 const stack = &self.box.children.values()[stack_index].widget.stack; 191 192 // each header tab maps 1:1 to a stack child by position. mirror the 193 // selection into current_page so the host can push the matching url; 194 // all user tabs share the .user parent, so this stays on the page 195 // rather than navigating. 196 if (header.getSelectedIndex()) |index| { 197 stack.getFocus().child_id = stack.children.keys()[index]; 198 const name = self.data.route_name; 199 switch (std.meta.activeTag(stack.children.values()[index])) { 200 .home_settings => self.session.data.current_page = .{ .user_settings = name }, 201 .home_auth => self.session.data.current_page = .{ .user_auth = name }, 202 // the quit tab is tty-only and not a route, so leave current_page 203 // alone (nothing to mirror into the url). 204 .quit => {}, 205 // the repos list (the default tab) 206 else => self.session.data.current_page = .{ .user = .{ .name = name, .after = self.data.repos_after } }, 207 } 208 } 209 try self.box.build(allocator, constraint, root_focus); 210 } 211 212 pub fn input(self: *View, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 213 if (self.getFocus().child_id) |child_id| { 214 if (self.box.children.getIndex(child_id)) |current_index| { 215 const child = &self.box.children.values()[current_index].widget; 216 var index = current_index; 217 218 const Direction = enum { up, down, none }; 219 const direction: Direction = switch (key) { 220 .arrow_up => .up, 221 .arrow_down => .down, 222 .mouse => |mouse| if (mouse.action == .scroll) 223 (if (mouse.action.scroll == .up) .up else .down) 224 else 225 .none, 226 else => .none, 227 }; 228 229 switch (direction) { 230 .up => { 231 switch (child.*) { 232 .user_header => { 233 try child.input(allocator, key, root_focus); 234 }, 235 .stack => { 236 if (child.stack.getSelected()) |selected_widget| { 237 const at_top = switch (selected_widget.*) { 238 .flow_box_scroll => |*v| v.getSelectedIndex() == 0, 239 .home_settings => |*v| v.getSelectedIndex() == 0, 240 .home_auth => |*v| v.getSelectedIndex() == 0, 241 .quit => |*v| v.getSelectedIndex() == 0, 242 else => false, 243 }; 244 if (at_top) { 245 index = header_index; 246 } else { 247 try child.input(allocator, key, root_focus); 248 } 249 } 250 }, 251 else => {}, 252 } 253 }, 254 .down => { 255 switch (child.*) { 256 .user_header => { 257 index = stack_index; 258 }, 259 .stack => { 260 try child.input(allocator, key, root_focus); 261 }, 262 else => {}, 263 } 264 }, 265 .none => { 266 try child.input(allocator, key, root_focus); 267 }, 268 } 269 270 if (index != current_index) { 271 root_focus.setFocus(self.box.children.keys()[index]); 272 } 273 } 274 } 275 } 276 277 pub fn clearGrid(self: *View) void { 278 self.box.clearGrid(); 279 } 280 281 pub fn getGrid(self: View) ?Grid { 282 return self.box.getGrid(); 283 } 284 285 pub fn getFocus(self: *View) *Focus { 286 return self.box.getFocus(); 287 } 288 }; 289