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