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