╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ User.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
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