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