╭ 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 rp = xit.repo; 6 const hash = xit.hash; 7 const xitui = xit.xitui; 8 const wgt = xitui.widget; 9 const layout = xitui.layout; 10 const Key = xitui.input.Key; 11 const Grid = xitui.grid.Grid; 12 const Focus = xitui.focus.Focus; 13 const inp = @import("./input.zig"); 14 const fork = @import("../fork.zig"); 15 16 pub const Header = @import("./User/Header.zig"); 17 pub const Settings = @import("./Settings.zig"); 18 pub const Auth = @import("./Auth.zig"); 19 pub const Quit = @import("./Quit.zig"); 20 21 pub const page_size = 20; // how many repos one window of the repos tab shows 22 23 pub const ForkItem = struct { 24 id: []const u8, 25 target: []const u8, 26 title: []const u8, 27 }; 28 29 header: Header, 30 user: evt.User.Public, 31 repos: []const evt.Repo.Record, 32 repos_start: usize, // the repos window this page was built with, mirrored into the url 33 repos_next_start: ?usize, // the `start` for the "next" row, or null on the last window 34 forks: []const ForkItem, 35 forks_start: usize, 36 forks_next_start: ?usize, 37 settings: Settings, 38 auth: Auth, 39 quit: Quit, 40 41 const Self = @This(); 42 43 pub fn init( 44 arena: *std.heap.ArenaAllocator, 45 session: *ui.Session, 46 haxy_moment: evt.AdminDB.HashMap(.read_only), 47 name: ui.RoutablePage.Array(evt.User.name_max_len), 48 repos_start: usize, 49 forks_start: usize, 50 ) !Self { 51 const DB = evt.AdminDB; 52 const hash_kind = evt.admin_repo_opts.hash; 53 54 // a route identifies a user by name; resolve it to the user's event id via 55 // the name->user-id index, which everything below keys off of. 56 const name_to_user_id_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "name->user-id")) orelse return error.NotFound; 57 const name_to_user_id = try DB.HashMap(.read_only).init(name_to_user_id_cursor); 58 const user_id_cursor = try name_to_user_id.getCursor(hash.hashInt(hash_kind, name.slice())) orelse return error.NotFound; 59 var user_id_buf: [evt.event_id_size]u8 = undefined; 60 _ = try user_id_cursor.readBytes(&user_id_buf); 61 const user_id: []const u8 = &user_id_buf; 62 63 const user = (try evt.User.readById(DB, hash_kind, haxy_moment, arena, user_id)) orelse return error.NotFound; 64 65 var repos: std.ArrayList(evt.Repo.Record) = .empty; 66 var repos_next_start: ?usize = null; 67 68 // the user-id->repo-id-set index maps each user to a set of their repo event 69 // ids ordered by creation (newest first); it only exists once a repo has 70 // been consumed, so a user with no repos simply yields an empty list. 71 if (try haxy_moment.getCursor(hash.hashInt(hash_kind, "user-id->repo-id-set"))) |user_id_to_repo_id_set_cursor| { 72 const user_id_to_repo_id_set = try DB.HashMap(.read_only).init(user_id_to_repo_id_set_cursor); 73 if (try user_id_to_repo_id_set.getCursor(hash.hashInt(hash_kind, user_id))) |user_repos_cursor| { 74 const user_repos = try DB.SortedSet(.read_only).init(user_repos_cursor); 75 const count = try user_repos.count(); 76 77 const event_id_to_repo_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "event-id->repo")) orelse return error.NotFound; 78 const event_id_to_repo = try DB.HashMap(.read_only).init(event_id_to_repo_cursor); 79 80 // read the window [start, start+page_size) with one seek to the start 81 // rank, then a sequential walk. 82 const end = @min(repos_start +| page_size, count); 83 var repos_iter = try user_repos.iteratorFromIndex(repos_start); 84 var i = repos_start; 85 while (i < end) : (i += 1) { 86 var kv_cursor = (try repos_iter.next()) orelse break; 87 const kv = try kv_cursor.readKeyValuePair(); 88 // the set key is orderKey = [created-order][event-id]; its trailing 89 // bytes are the repo event id. 90 var order_key: [@sizeOf(u64) + evt.event_id_size]u8 = undefined; 91 _ = try kv.key_cursor.readBytes(&order_key); 92 const event_id = order_key[@sizeOf(u64)..]; 93 const repo_cursor = try event_id_to_repo.getCursor(hash.hashInt(hash_kind, event_id)) orelse continue; 94 const repo_map = try DB.HashMap(.read_only).init(repo_cursor); 95 const repo_event = try evt.read(evt.Repo.Record, DB, hash_kind, arena, repo_map); 96 try repos.append(arena.allocator(), repo_event); 97 } 98 repos_next_start = if (end < count) end else null; 99 } 100 } 101 102 var forks: std.ArrayList(ForkItem) = .empty; 103 var forks_next_start: ?usize = null; 104 if (try haxy_moment.getCursor(hash.hashInt(hash_kind, evt.Fork.user_id_to_fork_id_set_key))) |by_user_cursor| { 105 const by_user = try DB.HashMap(.read_only).init(by_user_cursor); 106 if (try by_user.getCursor(hash.hashInt(hash_kind, user_id))) |user_forks_cursor| { 107 const user_forks = try DB.SortedSet(.read_only).init(user_forks_cursor); 108 const repos_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, evt.Repo.record_map_key)) orelse return error.NotFound; 109 const repo_records = try DB.HashMap(.read_only).init(repos_cursor); 110 const count = try user_forks.count(); 111 const end = @min(forks_start +| page_size, count); 112 var iter = try user_forks.iteratorFromIndex(forks_start); 113 var i = forks_start; 114 while (i < end) : (i += 1) { 115 const cursor = (try iter.next()) orelse break; 116 var kv_cursor = cursor; 117 const kv = try kv_cursor.readKeyValuePair(); 118 var order_key: [@sizeOf(u64) + evt.event_id_size]u8 = undefined; 119 _ = try kv.key_cursor.readBytes(&order_key); 120 const fork_id = order_key[@sizeOf(u64)..]; 121 const fork_id_hex = std.fmt.bytesToHex(fork_id.*, .lower); 122 const record = (try evt.Fork.readById(DB, hash_kind, haxy_moment, arena, fork_id)) orelse continue; 123 124 const repo_cursor = try repo_records.getCursor(hash.hashInt(hash_kind, record.event.repo_id)) orelse continue; 125 const target_repo = try evt.read(evt.Repo.Record, DB, hash_kind, arena, try DB.HashMap(.read_only).init(repo_cursor)); 126 const owner = (try evt.User.readById(DB, hash_kind, haxy_moment, arena, target_repo.event.user_id)) orelse continue; 127 const target = try std.fmt.allocPrint(arena.allocator(), "{s}/{s}", .{ owner.event.name, target_repo.event.name }); 128 129 var title: []const u8 = "(unavailable)"; 130 if (session.io) |io| if (session.repos_dir) |repos_dir| { 131 const path = try fork.forkPath(arena.allocator(), repos_dir, &fork_id_hex); 132 if (rp.Repo(.xit, .{}).open(io, arena.child_allocator, .{ .path = path, .require_repo_root = true })) |opened| { 133 var fork_repo = opened; 134 defer fork_repo.deinit(io, arena.child_allocator); 135 if (evt.currentMoment(.{}, &fork_repo)) |moment| { 136 if (try evt.Patch.readById(evt.EventDB(.sha1), .sha1, moment, arena, fork_id)) |patch| title = patch.event.title; 137 } else |_| {} 138 } else |_| {} 139 }; 140 try forks.append(arena.allocator(), .{ 141 .id = try arena.allocator().dupe(u8, &fork_id_hex), 142 .target = target, 143 .title = title, 144 }); 145 } 146 forks_next_start = if (end < count) end else null; 147 } 148 } 149 150 return .{ 151 .header = try Header.init(arena, user.event.name), 152 .user = evt.project(evt.User.Public, user.event), 153 .repos = repos.items, 154 .repos_start = repos_start, 155 .repos_next_start = repos_next_start, 156 .forks = forks.items, 157 .forks_start = forks_start, 158 .forks_next_start = forks_next_start, 159 .settings = Settings.init(), 160 .auth = Auth.init(), 161 .quit = Quit.init(), 162 }; 163 } 164 165 pub const View = struct { 166 box: wgt.Box(ui.Widget), 167 168 const header_index: usize = 0; 169 const stack_index: usize = 1; 170 171 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 172 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .rounded_corners = true, .direction = .vert }); 173 errdefer box.deinit(allocator); 174 175 // build the header first so we can grab the repos-tab id for the auth 176 // view (it focuses there after login). 177 { 178 var header_view = try Header.View.init(allocator, &data.header, session); 179 errdefer header_view.deinit(allocator); 180 try box.children.put(allocator, header_view.getFocus().id, .{ .widget = .{ .user_header = header_view }, .rect = null, .min_size = null }); 181 } 182 183 { 184 var stack = try wgt.Stack(ui.Widget).init(allocator); 185 errdefer stack.deinit(allocator); 186 187 // repos list — the default tab 188 { 189 var list = try ui.widget.FlowBox.Scroll.init(allocator, .{}, !session.is_terminal); 190 errdefer list.deinit(allocator); 191 192 var arena = std.heap.ArenaAllocator.init(allocator); 193 defer arena.deinit(); 194 const aa = arena.allocator(); 195 196 // a leading "previous" row off the first window, one row per repo, 197 // then a trailing "next" row when more remain. each window row 198 // navigates to the adjacent window of this user's repos. 199 var items: std.ArrayList(ui.widget.FlowBox.Item) = .empty; 200 if (data.repos_start > 0) 201 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 }) }); 202 for (data.repos) |repo| 203 // clicking a repo opens its page; the "a:" prefix makes the web 204 // renderer emit an <a href="/repo/alice/foo"> anchor. 205 try items.append(aa, .{ 206 .text = try std.fmt.allocPrint(aa, "{s} - {s}", .{ repo.event.name, repo.event.description }), 207 .link = try std.fmt.allocPrint(aa, "a:/repo/{s}/{s}", .{ data.user.name, repo.event.name }), 208 }); 209 if (data.repos_next_start) |next_start| 210 try items.append(aa, .{ .text = "next →", .link = try std.fmt.allocPrint(aa, "a:/user/{s}/repos/start:{d}", .{ data.user.name, next_start }) }); 211 try list.setItems(allocator, items.items); 212 213 try stack.children.put(allocator, list.getFocus().id, .{ .flow_box_scroll = list }); 214 } 215 216 // forks list 217 { 218 var list = try ui.widget.FlowBox.Scroll.init(allocator, .{}, !session.is_terminal); 219 errdefer list.deinit(allocator); 220 221 var item_arena = std.heap.ArenaAllocator.init(allocator); 222 defer item_arena.deinit(); 223 const aa = item_arena.allocator(); 224 var items: std.ArrayList(ui.widget.FlowBox.Item) = .empty; 225 if (data.forks_start > 0) 226 try items.append(aa, .{ .text = "← previous", .link = try std.fmt.allocPrint(aa, "a:/user/{s}/forks/start:{d}", .{ data.user.name, data.forks_start -| page_size }) }); 227 for (data.forks) |fork_item| { 228 const route = ui.RoutablePage.forkPatchRoute(fork_item.target, fork_item.id) orelse return error.RouteTooLong; 229 try items.append(aa, .{ 230 .text = try std.fmt.allocPrint(aa, "{s}\n{s}", .{ fork_item.target, fork_item.title }), 231 .link = try std.fmt.allocPrint(aa, "a:{s}", .{try route.toUrl(session.page_arena)}), 232 }); 233 } 234 if (data.forks_next_start) |next_start| 235 try items.append(aa, .{ .text = "next →", .link = try std.fmt.allocPrint(aa, "a:/user/{s}/forks/start:{d}", .{ data.user.name, next_start }) }); 236 try list.setItems(allocator, items.items); 237 try stack.children.put(allocator, list.getFocus().id, .{ .flow_box_scroll = list }); 238 } 239 240 // the header has no settings tab without a login, so keep the 241 // stack's children 1:1 with the tabs by skipping the view too 242 if (session.data.user_id != null) { 243 var settings_view = try Settings.View.init(allocator, session); 244 errdefer settings_view.deinit(allocator); 245 try stack.children.put(allocator, settings_view.getFocus().id, .{ .home_settings = settings_view }); 246 } 247 248 { 249 var auth_view = try Auth.View.init(allocator, &data.auth, session); 250 errdefer auth_view.deinit(allocator); 251 try stack.children.put(allocator, auth_view.getFocus().id, .{ .home_auth = auth_view }); 252 } 253 254 if (session.is_terminal) { 255 var quit_view = try Quit.View.init(allocator, session); 256 errdefer quit_view.deinit(allocator); 257 try stack.children.put(allocator, quit_view.getFocus().id, .{ .quit = quit_view }); 258 } 259 260 try box.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 261 } 262 263 var self = View{ .box = box }; 264 self.getFocus().child_id = box.children.keys()[header_index]; 265 return self; 266 } 267 268 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 269 self.box.deinit(allocator); 270 } 271 272 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 273 self.clearGrid(); 274 const header = &self.box.children.values()[header_index].widget.user_header; 275 const stack = &self.box.children.values()[stack_index].widget.stack; 276 277 // each header tab maps 1:1 to a stack child by position 278 if (header.getSelectedIndex()) |index| 279 stack.getFocus().child_id = stack.children.keys()[index]; 280 try self.box.build(allocator, constraint, root_focus); 281 } 282 283 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 284 if (self.getFocus().child_id) |child_id| { 285 if (self.box.children.getIndex(child_id)) |current_index| { 286 const child = &self.box.children.values()[current_index].widget; 287 var index = current_index; 288 289 const direction = inp.vertDirection(key); 290 291 switch (direction) { 292 .up => { 293 switch (child.*) { 294 .user_header => { 295 try child.input(allocator, key, root_focus); 296 }, 297 .stack => { 298 if (child.stack.getSelected()) |selected_widget| { 299 if (selected_widget.atTop(root_focus)) { 300 index = header_index; 301 } else { 302 try child.input(allocator, key, root_focus); 303 } 304 } 305 }, 306 else => {}, 307 } 308 }, 309 .down => { 310 switch (child.*) { 311 .user_header => { 312 index = stack_index; 313 }, 314 .stack => { 315 try child.input(allocator, key, root_focus); 316 }, 317 else => {}, 318 } 319 }, 320 .none => { 321 try child.input(allocator, key, root_focus); 322 }, 323 } 324 325 if (index != current_index) { 326 root_focus.setFocus(self.box.children.keys()[index]); 327 } 328 } 329 } 330 } 331 332 pub fn clearGrid(self: *View) void { 333 self.box.clearGrid(); 334 } 335 336 pub fn getGrid(self: View) ?Grid { 337 return self.box.getGrid(); 338 } 339 340 pub fn getFocus(self: *View) *Focus { 341 return self.box.getFocus(); 342 } 343 }; 344