╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Issues.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
530 .theirs_author = their_author, 531 }, 532 } 533 } 534 return conflict; 535 } 536 537 // the oid `field` maps to in a field->oid map, or null 538 fn readFieldOid( 539 comptime hash_kind: hash.HashKind, 540 field_oids_maybe: ?evt.EventDB(hash_kind).SortedMap(.read_only), 541 field: []const u8, 542 ) !?[hash.byteLen(hash_kind)]u8 { 543 const field_oids = field_oids_maybe orelse return null; 544 const cursor = (try field_oids.getCursor(field)) orelse return null; 545 var oid: [hash.byteLen(hash_kind)]u8 = undefined; 546 _ = try cursor.readBytes(&oid); 547 return oid; 548 } 549 550 // the author of the event commit `oid_maybe` names 551 fn oidAuthor( 552 comptime repo_kind: rp.RepoKind, 553 comptime repo_opts: rp.RepoOpts(repo_kind), 554 arena: *std.heap.ArenaAllocator, 555 repo: *rp.Repo(repo_kind, repo_opts), 556 io: std.Io, 557 admin_moment: ?evt.AdminDB.HashMap(.read_only), 558 oid_maybe: ?[hash.byteLen(repo_opts.hash)]u8, 559 ) !ui.Author { 560 const oid = oid_maybe orelse return .unknown; 561 const gpa = arena.child_allocator; 562 var start_oids = [_][hash.hexLen(repo_opts.hash)]u8{std.fmt.bytesToHex(oid, .lower)}; 563 var commit_iter = repo.log(io, gpa, start_oids[0..1]) catch return .unknown; 564 defer commit_iter.deinit(); 565 const commit_object = (commit_iter.next(gpa) catch return .unknown) orelse return .unknown; 566 defer commit_object.deinit(); 567 return try ui.Author.init(admin_moment, arena, commit_object.content.commit.metadata.author orelse ""); 568 } 569 570 pub const View = Threads.View(.issue, Self); 571 572 // tabs switching between the issues page's views. 573 pub const Header = struct { 574 box: wgt.Box(ui.Widget), 575 tab_ids: std.AutoArrayHashMapUnmanaged(usize, void), 576 577 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !Header { 578 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 579 errdefer box.deinit(allocator); 580 581 var tab_ids: std.AutoArrayHashMapUnmanaged(usize, void) = .empty; 582 errdefer tab_ids.deinit(allocator); 583 584 const aa = session.page_arena.allocator(); 585 586 // a list tab per status, labeled with its listing's issue count 587 for ([_]evt.Issue.Status{ .open, .closed }) |status| { 588 const route = ui.RoutablePage.repoIssuesRoute(data.identity, status, data.tag, "") orelse return error.RouteTooLong; 589 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 590 const label = try std.fmt.allocPrint(aa, "{s} ({d})", .{ @tagName(status), data.window(status).count }); 591 try addTab(allocator, &box, &tab_ids, label, link); 592 } 593 594 // tags tab, labeled with the active tag filter 595 { 596 const tags_route = ui.RoutablePage.repoThreadTagsRoute(.issue, data.identity, data.tag) orelse return error.RouteTooLong; 597 const tags_link = try std.fmt.allocPrint(aa, "ai:{s}", .{try tags_route.toUrl(session.page_arena)}); 598 const label = if (data.tag.len == 0) "tags" else blk: { 599 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, data.tag)); 600 break :blk try std.fmt.allocPrint(aa, "tags ({s})", .{decoded}); 601 }; 602 try addTab(allocator, &box, &tab_ids, label, tags_link); 603 } 604 605 // new-issue tab; an edit or resolve url shows its tab in this place 606 { 607 const route = switch (data.view) { 608 .edit => ui.RoutablePage.repoThreadEditRoute(.issue, data.identity, data.selected_id) orelse return error.RouteTooLong, 609 .new_comment => ui.RoutablePage.repoThreadCommentNewRoute(.issue, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 610 .edit_comment => ui.RoutablePage.repoThreadCommentEditRoute(.issue, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 611 .remove => ui.RoutablePage.repoThreadRemoveRoute(.issue, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 612 .resolve => ui.RoutablePage.repoIssuesResolveRoute(data.identity, data.selected_id, data.theirs_picks) orelse return error.RouteTooLong, 613 else => ui.RoutablePage.repoThreadNewRoute(.issue, data.identity) orelse return error.RouteTooLong, 614 }; 615 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 616 const label: []const u8 = switch (data.view) { 617 .edit => "edit", 618 .new_comment => "reply", 619 .edit_comment => "edit", 620 .remove => "remove", 621 .resolve => "resolve", 622 else => "new", 623 }; 624 try addTab(allocator, &box, &tab_ids, label, link); 625 } 626 627 // conflicts tab, labeled with the conflict count 628 if (data.conflicts.count > 0) { 629 const route = ui.RoutablePage.repoIssuesConflictsRoute(data.identity, "") orelse return error.RouteTooLong; 630 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 631 const label = try std.fmt.allocPrint(aa, "conflicts ({d})", .{data.conflicts.count}); 632 try addTab(allocator, &box, &tab_ids, label, link); 633 } 634 635 var self = Header{ .box = box, .tab_ids = tab_ids }; 636 // the tab matching the page's view is selected initially. 637 self.getFocus().child_id = self.tab_ids.keys()[View.viewIndex(data.view)]; 638 return self; 639 } 640 641 fn addTab(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), tab_ids: *std.AutoArrayHashMapUnmanaged(usize, void), label: []const u8, link: []const u8) !void { 642 var text_box = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 643 errdefer text_box.deinit(allocator); 644 text_box.getFocus().focusable = true; 645 text_box.getFocus().kind = .{ .custom = link }; 646 try tab_ids.put(allocator, text_box.getFocus().id, {}); 647 try box.children.put(allocator, text_box.getFocus().id, .{ 648 .widget = .{ .text_box = text_box }, 649 .rect = null, 650 .min_size = .{ .width = label.len + 2, .height = null }, 651 }); 652 } 653 654 pub fn deinit(self: *Header, allocator: std.mem.Allocator) void { 655 self.box.deinit(allocator); 656 self.tab_ids.deinit(allocator); 657 } 658 659 pub fn build(self: *Header, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 660 self.clearGrid(); 661 // only the selected tab shows its border 662 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 663 switch (child.widget) { 664 .text_box => |*tb| tb.options.border_style = if (self.getFocus().child_id == id) .single else .hidden, 665 else => {}, 666 } 667 } 668 try self.box.build(allocator, constraint, root_focus); 669 } 670 671 pub fn input(self: *Header, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 672 _ = allocator; 673 const current_tab = self.currentTabIndex() orelse return; 674 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 675 root_focus.setFocus(self.tab_ids.keys()[new_tab]); 676 } 677 } 678 679 pub fn clearGrid(self: *Header) void { 680 self.box.clearGrid(); 681 } 682 683 pub fn getGrid(self: Header) ?Grid { 684 return self.box.getGrid(); 685 } 686 687 pub fn getFocus(self: *Header) *Focus { 688 return self.box.getFocus(); 689 } 690 691 pub fn getSelectedIndex(self: Header) ?usize { 692 return self.currentTabIndex(); 693 } 694 695 fn currentTabIndex(self: Header) ?usize { 696 const child_id = self.box.focus.child_id orelse return null; 697 return self.tab_ids.getIndex(child_id); 698 } 699 }; 700