╭ 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 // unreadable repos leave their window short rather than shifting the others 97 if (evt.Repo.roleOf(repo_event, session.userId()) == .none) continue; 98 try repos.append(arena.allocator(), repo_event); 99 } 100 repos_next_start = if (end < count) end else null; 101 } 102 } 103 104 var forks: std.ArrayList(ForkItem) = .empty; 105 var forks_next_start: ?usize = null; 106 if (try haxy_moment.getCursor(hash.hashInt(hash_kind, evt.Fork.user_id_to_fork_id_set_key))) |by_user_cursor| { 107 const by_user = try DB.HashMap(.read_only).init(by_user_cursor); 108 if (try by_user.getCursor(hash.hashInt(hash_kind, user_id))) |user_forks_cursor| { 109 const user_forks = try DB.SortedSet(.read_only).init(user_forks_cursor); 110 const repos_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, evt.Repo.record_map_key)) orelse return error.NotFound; 111 const repo_records = try DB.HashMap(.read_only).init(repos_cursor); 112 const count = try user_forks.count(); 113 const end = @min(forks_start +| page_size, count); 114 var iter = try user_forks.iteratorFromIndex(forks_start); 115 var i = forks_start; 116 while (i < end) : (i += 1) { 117 const cursor = (try iter.next()) orelse break; 118 var kv_cursor = cursor; 119 const kv = try kv_cursor.readKeyValuePair(); 120 var order_key: [@sizeOf(u64) + evt.event_id_size]u8 = undefined; 121 _ = try kv.key_cursor.readBytes(&order_key); 122 const fork_id = order_key[@sizeOf(u64)..]; 123 const fork_id_hex = std.fmt.bytesToHex(fork_id.*, .lower); 124 const record = (try evt.Fork.readById(DB, hash_kind, haxy_moment, arena, fork_id)) orelse continue; 125 126 const repo_cursor = try repo_records.getCursor(hash.hashInt(hash_kind, record.event.repo_id)) orelse continue; 127 const target_repo = try evt.read(evt.Repo.Record, DB, hash_kind, arena, try DB.HashMap(.read_only).init(repo_cursor)); 128 if (evt.Repo.roleOf(target_repo, session.userId()) == .none) continue; 129 const owner = (try evt.User.readById(DB, hash_kind, haxy_moment, arena, target_repo.event.user_id)) orelse continue; 130 const target = try std.fmt.allocPrint(arena.allocator(), "{s}/{s}", .{ owner.event.name, target_repo.event.name }); 131 132 var title: []const u8 = "(unavailable)"; 133 if (session.io) |io| if (session.repos_dir) |repos_dir| { 134 const path = try fork.forkPath(arena.allocator(), repos_dir, &fork_id_hex); 135 if (rp.Repo(.xit, .{}).open(io, arena.child_allocator, .{ .path = path, .require_repo_root = true })) |opened| { 136 var fork_repo = opened; 137 defer fork_repo.deinit(io, arena.child_allocator); 138 if (evt.currentMoment(.{}, &fork_repo)) |moment| { 139 if (try evt.Patch.readById(evt.EventDB(.sha1), .sha1, moment, arena, fork_id)) |patch| title = patch.event.title; 140 } else |_| {} 141 } else |_| {} 142 }; 143 try forks.append(arena.allocator(), .{ 144 .id = try arena.allocator().dupe(u8, &fork_id_hex), 145 .target = target, 146 .title = title, 147 }); 148 } 149 forks_next_start = if (end < count) end else null; 150 } 151 } 152 153 return .{ 154 .header = try Header.init(arena, user.event.name), 155 .user = evt.project(evt.User.Public, user.event), 156 .repos = repos.items, 157 .repos_start = repos_start, 158 .repos_next_start = repos_next_start, 159 .forks = forks.items, 160 .forks_start = forks_start, 161 .forks_next_start = forks_next_start, 162 .settings = Settings.init(), 163 .auth = Auth.init(), 164 .quit = Quit.init(), 165 }; 166 } 167 168 pub const View = struct { 169 box: wgt.Box(ui.Widget), 170 171 const header_index: usize = 0; 172 const stack_index: usize = 1; 173 174 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 175 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .rounded_corners = true, .direction = .vert }); 176 errdefer box.deinit(allocator); 177 178 // build the header first so we can grab the repos-tab id for the auth 179 // view (it focuses there after login). 180 { 181 var header_view = try Header.View.init(allocator, &data.header, session); 182 errdefer header_view.deinit(allocator); 183 try box.children.put(allocator, header_view.getFocus().id, .{ .widget = .{ .user_header = header_view }, .rect = null, .min_size = null }); 184 } 185 186 { 187 var stack = try wgt.Stack(ui.Widget).init(allocator); 188 errdefer stack.deinit(allocator); 189 190 // repos list — the default tab 191 { 192 var list = try ui.widget.FlowBox.Scroll.init(allocator, .{}, !session.is_terminal); 193 errdefer list.deinit(allocator); 194 195 var arena = std.heap.ArenaAllocator.init(allocator); 196 defer arena.deinit(); 197 const aa = arena.allocator(); 198 199 // a leading "previous" row off the first window, one row per repo, 200 // then a trailing "next" row when more remain. each window row 201 // navigates to the adjacent window of this user's repos. 202 var items: std.ArrayList(ui.widget.FlowBox.Item) = .empty; 203 if (data.repos_start > 0) 204 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 }) }); 205 for (data.repos) |repo| 206 // clicking a repo opens its page; the "a:" prefix makes the web 207 // renderer emit an <a href="/repo/alice/foo"> anchor. 208 try items.append(aa, .{ 209 .text = try std.fmt.allocPrint(aa, "{s} - {s}", .{ repo.event.name, repo.event.description }), 210 .link = try std.fmt.allocPrint(aa, "a:/repo/{s}/{s}", .{ data.user.name, repo.event.name }), 211 }); 212 if (data.repos_next_start) |next_start| 213 try items.append(aa, .{ .text = "next →", .link = try std.fmt.allocPrint(aa, "a:/user/{s}/repos/start:{d}", .{ data.user.name, next_start }) }); 214 try list.setItems(allocator, items.items); 215 216 try stack.children.put(allocator, list.getFocus().id, .{ .flow_box_scroll = list }); 217 } 218 219 // forks list 220 { 221 var list = try ui.widget.FlowBox.Scroll.init(allocator, .{}, !session.is_terminal); 222 errdefer list.deinit(allocator); 223 224 var item_arena = std.heap.ArenaAllocator.init(allocator); 225 defer item_arena.deinit(); 226 const aa = item_arena.allocator(); 227 var items: std.ArrayList(ui.widget.FlowBox.Item) = .empty; 228 if (data.forks_start > 0) 229 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 }) }); 230 for (data.forks) |fork_item| { 231 const route = ui.RoutablePage.forkPatchRoute(fork_item.target, fork_item.id) orelse return error.RouteTooLong; 232 try items.append(aa, .{ 233 .text = try std.fmt.allocPrint(aa, "{s}\n{s}", .{ fork_item.target, fork_item.title }), 234 .link = try std.fmt.allocPrint(aa, "a:{s}", .{try route.toUrl(session.page_arena)}), 235 }); 236 } 237 if (data.forks_next_start) |next_start| 238 try items.append(aa, .{ .text = "next →", .link = try std.fmt.allocPrint(aa, "a:/user/{s}/forks/start:{d}", .{ data.user.name, next_start }) }); 239 try list.setItems(allocator, items.items); 240 try stack.children.put(allocator, list.getFocus().id, .{ .flow_box_scroll = list }); 241 } 242 243 // the header has no settings tab without a login, so keep the 244 // stack's children 1:1 with the tabs by skipping the view too 245 if (session.data.user_id != null) { 246 var settings_view = try Settings.View.init(allocator, session); 247 errdefer settings_view.deinit(allocator); 248 try stack.children.put(allocator, settings_view.getFocus().id, .{ .home_settings = settings_view }); 249 } 250 251 { 252 var auth_view = try Auth.View.init(allocator, &data.auth, session); 253 errdefer auth_view.deinit(allocator); 254 try stack.children.put(allocator, auth_view.getFocus().id, .{ .home_auth = auth_view }); 255 } 256 257 if (session.is_terminal) { 258 var quit_view = try Quit.View.init(allocator, session); 259 errdefer quit_view.deinit(allocator); 260 try stack.children.put(allocator, quit_view.getFocus().id, .{ .quit = quit_view }); 261 } 262 263 try box.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 264 } 265 266 var self = View{ .box = box }; 267 self.getFocus().child_id = box.children.keys()[header_index]; 268 return self; 269 } 270 271 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 272 self.box.deinit(allocator); 273 } 274 275 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 276 self.clearGrid(); 277 const header = &self.box.children.values()[header_index].widget.user_header; 278 const stack = &self.box.children.values()[stack_index].widget.stack; 279 280 // each header tab maps 1:1 to a stack child by position 281 if (header.getSelectedIndex()) |index| 282 stack.getFocus().child_id = stack.children.keys()[index]; 283 try self.box.build(allocator, constraint, root_focus); 284 } 285 286 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 287 if (self.getFocus().child_id) |child_id| { 288 if (self.box.children.getIndex(child_id)) |current_index| { 289 const child = &self.box.children.values()[current_index].widget; 290 var index = current_index; 291 292 const direction = inp.vertDirection(key); 293 294 switch (direction) { 295 .up => { 296 switch (child.*) { 297 .user_header => { 298 try child.input(allocator, key, root_focus); 299 }, 300 .stack => { 301 if (child.stack.getSelected()) |selected_widget| { 302 if (selected_widget.atTop(root_focus)) { 303 index = header_index; 304 } else { 305 try child.input(allocator, key, root_focus); 306 } 307 } 308 }, 309 else => {}, 310 } 311 }, 312 .down => { 313 switch (child.*) { 314 .user_header => { 315 index = stack_index; 316 }, 317 .stack => { 318 try child.input(allocator, key, root_focus); 319 }, 320 else => {}, 321 } 322 }, 323 .none => { 324 try child.input(allocator, key, root_focus); 325 }, 326 } 327 328 if (index != current_index) { 329 root_focus.setFocus(self.box.children.keys()[index]); 330 } 331 } 332 } 333 } 334 335 pub fn clearGrid(self: *View) void { 336 self.box.clearGrid(); 337 } 338 339 pub fn getGrid(self: View) ?Grid { 340 return self.box.getGrid(); 341 } 342 343 pub fn getFocus(self: *View) *Focus { 344 return self.box.getFocus(); 345 } 346 }; 347