╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Issues.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2342 switch (src.repo_kind) { 2343 inline else => |repo_kind| { 2344 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 2345 defer any_repo.deinit(io, allocator); 2346 switch (any_repo) { 2347 inline else => |*repo| try evt.Issue.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2348 .title = title, 2349 .tags = tags, 2350 .description = description, 2351 } }, author_email), 2352 } 2353 }, 2354 } 2355 2356 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, entry.issue.event.status, "", entry.id) orelse return; 2357 try self.session.navigate(route); 2358 } 2359 2360 // flip the shown issue's status by re-emitting its event, then reload the 2361 // page rooted at the issue so the view reflects the change. this is the 2362 // terminal path; the web posts the button's form to the status route. 2363 fn toggleIssueStatus(self: *View, allocator: std.mem.Allocator, index: usize) !void { 2364 if (comptime wasm) return; 2365 const io = self.session.io orelse return; 2366 const src = self.data.repo_source orelse return; 2367 const sel = self.detailed_index[index] orelse return; 2368 const entry = self.window(index).issues[sel]; 2369 const author_email = (try self.session.eventAuthorEmail()) orelse return; 2370 2371 const status: evt.Issue.Status = switch (entry.issue.event.status) { 2372 .open => .closed, 2373 .closed => .open, 2374 }; 2375 2376 var id_bytes: [evt.event_id_size]u8 = undefined; 2377 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 2378 2379 switch (src.repo_kind) { 2380 inline else => |repo_kind| { 2381 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 2382 defer any_repo.deinit(io, allocator); 2383 switch (any_repo) { 2384 inline else => |*repo| try evt.Issue.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .status = status }, author_email), 2385 } 2386 }, 2387 } 2388 2389 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, status, "", entry.id) orelse return; 2390 try self.session.navigate(route); 2391 } 2392 2393 // arrow keys move the tag selection; at the flow's edges focus crosses to 2394 // the neighboring widgets. 2395 fn tagsInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 2396 const tf = self.tagFlow(index) orelse return; 2397 const cid = tf.focus.child_id orelse return; 2398 const cur = tf.indexOfFocusId(cid) orelse return; 2399 const count = tf.text_boxes.items.len; 2400 switch (key) { 2401 .arrow_left => if (cur > 0) self.focusTag(index, tf, root_focus, cur - 1) else try self.focusList(index, root_focus), 2402 .arrow_right => if (cur + 1 < count) self.focusTag(index, tf, root_focus, cur + 1), 2403 .arrow_up => if (tf.rowStep(cur, false)) |i| { 2404 self.focusTag(index, tf, root_focus, i); 2405 } else { 2406 _ = self.moveDetailVertical(index, root_focus, false); 2407 }, 2408 .arrow_down => if (tf.rowStep(cur, true)) |i| { 2409 self.focusTag(index, tf, root_focus, i); 2410 } else { 2411 _ = self.moveDetailVertical(index, root_focus, true); 2412 }, 2413 .home => self.focusTag(index, tf, root_focus, 0), 2414 .end => self.focusTag(index, tf, root_focus, count - 1), 2415 else => {}, 2416 } 2417 } 2418 2419 const button_in_row_index: usize = 1; 2420 const title_child_index: usize = 0; 2421 const tags_child_index: usize = 2; 2422 2423 fn tagFlow(self: *View, index: usize) ?*ui.TagFlow { 2424 const inner = self.detailInner(index); 2425 if (inner.children.count() <= tags_child_index) return null; 2426 return switch (inner.children.values()[tags_child_index].widget) { 2427 .tag_flow => |*tf| tf, 2428 else => null, 2429 }; 2430 } 2431 2432 fn tagsFocused(self: *View, index: usize) bool { 2433 const inner = self.detailInner(index); 2434 const cid = inner.getFocus().child_id orelse return false; 2435 return inner.children.getIndex(cid) == tags_child_index and self.tagFlow(index) != null; 2436 } 2437 2438 // the open/close button inside the detail frame's tool row, identified 2439 // by its posting kind; a logged-out session's or conflicted issue's row 2440 // has none. 2441 fn statusButton(self: *View, index: usize) ?*wgt.TextBox(ui.Widget) { 2442 const row = self.toolRow(index); 2443 if (row.children.count() <= button_in_row_index) return null; 2444 switch (row.children.values()[button_in_row_index].widget) { 2445 .text_box => |*tb| switch (tb.box.focus.kind) { 2446 .custom => |custom| if (std.mem.eql(u8, custom, "submit")) return tb, 2447 else => {}, 2448 }, 2449 else => {}, 2450 } 2451 return null; 2452 } 2453 2454 fn toolRowFocused(self: *View, index: usize) bool { 2455 const outer = self.detailOuter(index); 2456 const cid = outer.getFocus().child_id orelse return false; 2457 return outer.children.getIndex(cid) == tool_row_index; 2458 } 2459 2460 fn focusTag(self: *View, index: usize, tf: *ui.TagFlow, root_focus: *Focus, item: usize) void { 2461 root_focus.setFocus(tf.text_boxes.items[item].getFocus().id); 2462 // keep the tag visible on the terminal: its rect offset by the flow's 2463 // position in the pane. 2464 if (self.session.is_terminal and item < tf.rects.items.len) { 2465 if (self.detailInner(index).children.values()[tags_child_index].rect) |flow_rect| { 2466 var rect = tf.rects.items[item]; 2467 rect.x += flow_rect.x; 2468 rect.y += flow_rect.y; 2469 self.detailScroll(index).scrollToRect(rect); 2470 } 2471 } 2472 } 2473 2474 fn titleFocused(self: *View, index: usize) bool { 2475 const inner = self.detailInner(index); 2476 const cid = inner.getFocus().child_id orelse return false; 2477 return inner.children.getIndex(cid) == title_child_index; 2478 } 2479 2480 fn authorFocused(self: *View, index: usize) bool { 2481 const inner = self.detailInner(index); 2482 const cid = inner.getFocus().child_id orelse return false; 2483 return cid == self.author_id[index]; 2484 } 2485 2486 fn descriptionFocused(self: *View, index: usize) bool { 2487 const cid = self.detailInner(index).getFocus().child_id orelse return false; 2488 return cid == self.description_id[index]; 2489 } 2490 2491 // return to the tool row's last-focused button (the header when the 2492 // description page shows no row). 2493 fn focusToolRow(self: *View, index: usize, root_focus: *Focus) void { 2494 const cid = self.toolRow(index).getFocus().child_id orelse return self.focusHeader(root_focus); 2495 root_focus.setFocus(cid); 2496 } 2497 2498 // enter the detail pane. an empty pane (no issues) can't be entered. 2499 fn focusDetail(self: *View, index: usize, root_focus: *Focus) !void { 2500 if (self.detailInner(index).children.count() == 0) return; 2501 root_focus.setFocus(self.detailOuter(index).getFocus().id); 2502 } 2503 2504 // return to the list. 2505 fn focusList(self: *View, index: usize, root_focus: *Focus) !void { 2506 root_focus.setFocus(self.listScroll(index).getFocus().id); 2507 } 2508 2509 // cross to the header tabs above the stack. 2510 fn focusHeader(self: *View, root_focus: *Focus) void { 2511 root_focus.setFocus(self.box.children.keys()[header_index]); 2512 } 2513 2514 pub fn clearGrid(self: *View) void { 2515 self.box.clearGrid(); 2516 } 2517 2518 pub fn getGrid(self: View) ?Grid { 2519 return self.box.getGrid(); 2520 } 2521 2522 pub fn getFocus(self: *View) *Focus { 2523 return self.box.getFocus(); 2524 } 2525 2526 // for the parent's "scroll up at the top jumps to the header" check: at the 2527 // top only while the header tabs hold focus, so up from the split 2528 // crosses into the tabs first. 2529 pub fn getSelectedIndex(self: *View) ?usize { 2530 return if (self.headerActive()) 0 else 1; 2531 } 2532 }; 2533 2534 // the "a:" navigation link for `status`'s issues page filtered to the 2535 // url-encoded `tag` and rooted at issue `id` within `identity` ("owner/name"). 2536 // a rooted url ignores `status` (it derives its view from the issue's own); 2537 // with an empty `id` this is the bare list link. 2538 fn issuesLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8, id: []const u8) ![]const u8 { 2539 const route = ui.RoutablePage.repoIssuesRoute(identity, status, tag, id) orelse return error.RouteTooLong; 2540 const url = try route.toUrl(page_arena); 2541 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2542 } 2543 2544 // the in-page "ai:" anchor for selecting issue `id` in `identity`'s list; the 2545 // href is only followed with js off. 2546 fn issueRowLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, id: []const u8) ![]const u8 { 2547 const route = ui.RoutablePage.repoIssuesRoute(identity, .open, "", id) orelse return error.RouteTooLong; 2548 const url = try route.toUrl(page_arena); 2549 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{url}); 2550 } 2551 2552 // the "a:" link to the window `status_maybe` names, keeping the tag filter, 2553 // or the conflicts window when null, rooted at issue `id`. 2554 fn windowLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, status_maybe: ?evt.Issue.Status, id: []const u8) ![]const u8 { 2555 if (status_maybe) |status| return issuesLink(page_arena, data.identity, status, data.tag, id); 2556 return conflictsLink(page_arena, data.identity, id); 2557 } 2558 2559 // the "a:" link to the conflicts list rooted at issue `start` ("" = the 2560 // first window). 2561 fn conflictsLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, start: []const u8) ![]const u8 { 2562 const route = ui.RoutablePage.repoIssuesConflictsRoute(identity, start) orelse return error.RouteTooLong; 2563 const url = try route.toUrl(page_arena); 2564 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2565 } 2566 2567 // the "a:" link to the whole-description page of issue `id` within `identity`. 2568 fn descriptionLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, id: []const u8) ![]const u8 { 2569 const route = ui.RoutablePage.repoIssuesDescriptionRoute(identity, id) orelse return error.RouteTooLong; 2570 const url = try route.toUrl(page_arena); 2571 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2572 } 2573 2574 // the "a:" link to `status`'s issues list filtered to `tag` (raw; encoded here). 2575 fn tagLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8) ![]const u8 { 2576 const encoded = try ui.urlEncodeRef(page_arena.allocator(), tag); 2577 const route = ui.RoutablePage.repoIssuesRoute(identity, status, encoded, "") orelse return error.RouteTooLong; 2578 const url = try route.toUrl(page_arena); 2579 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2580 } 2581 2582 // tabs switching between the issues page's views. 2583 pub const Header = struct { 2584 box: wgt.Box(ui.Widget), 2585 tab_ids: std.AutoArrayHashMapUnmanaged(usize, void), 2586 2587 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !Header { 2588 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 2589 errdefer box.deinit(allocator); 2590 2591 var tab_ids: std.AutoArrayHashMapUnmanaged(usize, void) = .empty; 2592 errdefer tab_ids.deinit(allocator); 2593 2594 const aa = session.page_arena.allocator(); 2595 2596 // a list tab per status, labeled with its listing's issue count 2597 for ([_]evt.Issue.Status{ .open, .closed }) |status| { 2598 const route = ui.RoutablePage.repoIssuesRoute(data.identity, status, data.tag, "") orelse return error.RouteTooLong; 2599 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 2600 const label = try std.fmt.allocPrint(aa, "{s} ({d})", .{ @tagName(status), data.window(status).count }); 2601 try addTab(allocator, &box, &tab_ids, label, link); 2602 } 2603 2604 // tags tab, labeled with the active tag filter 2605 { 2606 const tags_route = ui.RoutablePage.repoIssuesTagsRoute(data.identity, data.tag) orelse return error.RouteTooLong; 2607 const tags_link = try std.fmt.allocPrint(aa, "ai:{s}", .{try tags_route.toUrl(session.page_arena)}); 2608 const label = if (data.tag.len == 0) "tags" else blk: { 2609 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, data.tag)); 2610 break :blk try std.fmt.allocPrint(aa, "tags ({s})", .{decoded}); 2611 }; 2612 try addTab(allocator, &box, &tab_ids, label, tags_link); 2613 } 2614 2615 // new-issue tab; an edit or resolve url shows its tab in this place 2616 { 2617 const route = switch (data.view) { 2618 .edit => ui.RoutablePage.repoIssuesEditRoute(data.identity, data.selected_id) orelse return error.RouteTooLong, 2619 .new_comment => ui.RoutablePage.repoCommentNewRoute(data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 2620 .edit_comment => ui.RoutablePage.repoCommentEditRoute(data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 2621 .resolve => ui.RoutablePage.repoIssuesResolveRoute(data.identity, data.selected_id, data.theirs_picks) orelse return error.RouteTooLong, 2622 else => ui.RoutablePage.repoIssuesNewRoute(data.identity) orelse return error.RouteTooLong, 2623 }; 2624 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 2625 const label: []const u8 = switch (data.view) { 2626 .edit => "edit", 2627 .new_comment => "reply", 2628 .edit_comment => "edit", 2629 .resolve => "resolve", 2630 else => "new", 2631 }; 2632 try addTab(allocator, &box, &tab_ids, label, link); 2633 } 2634 2635 // conflicts tab, labeled with the conflict count 2636 if (data.conflicts.count > 0) { 2637 const route = ui.RoutablePage.repoIssuesConflictsRoute(data.identity, "") orelse return error.RouteTooLong; 2638 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 2639 const label = try std.fmt.allocPrint(aa, "conflicts ({d})", .{data.conflicts.count}); 2640 try addTab(allocator, &box, &tab_ids, label, link); 2641 } 2642 2643 var self = Header{ .box = box, .tab_ids = tab_ids }; 2644 // the tab matching the page's view is selected initially. 2645 self.getFocus().child_id = self.tab_ids.keys()[View.viewIndex(data.view)]; 2646 return self; 2647 } 2648 2649 fn addTab(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), tab_ids: *std.AutoArrayHashMapUnmanaged(usize, void), label: []const u8, link: []const u8) !void { 2650 var text_box = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 2651 errdefer text_box.deinit(allocator); 2652 text_box.getFocus().focusable = true; 2653 text_box.getFocus().kind = .{ .custom = link }; 2654 try tab_ids.put(allocator, text_box.getFocus().id, {}); 2655 try box.children.put(allocator, text_box.getFocus().id, .{ 2656 .widget = .{ .text_box = text_box }, 2657 .rect = null, 2658 .min_size = .{ .width = label.len + 2, .height = null }, 2659 }); 2660 } 2661 2662 pub fn deinit(self: *Header, allocator: std.mem.Allocator) void { 2663 self.box.deinit(allocator); 2664 self.tab_ids.deinit(allocator); 2665 } 2666 2667 pub fn build(self: *Header, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2668 self.clearGrid(); 2669 // only the selected tab shows its border 2670 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 2671 switch (child.widget) { 2672 .text_box => |*tb| tb.options.border_style = if (self.getFocus().child_id == id) .single else .hidden, 2673 else => {}, 2674 } 2675 } 2676 try self.box.build(allocator, constraint, root_focus); 2677 } 2678 2679 pub fn input(self: *Header, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2680 _ = allocator; 2681 const current_tab = self.currentTabIndex() orelse return; 2682 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 2683 root_focus.setFocus(self.tab_ids.keys()[new_tab]); 2684 } 2685 } 2686 2687 pub fn clearGrid(self: *Header) void { 2688 self.box.clearGrid(); 2689 } 2690 2691 pub fn getGrid(self: Header) ?Grid { 2692 return self.box.getGrid(); 2693 } 2694 2695 pub fn getFocus(self: *Header) *Focus { 2696 return self.box.getFocus(); 2697 } 2698 2699 pub fn getSelectedIndex(self: Header) ?usize { 2700 return self.currentTabIndex(); 2701 } 2702 2703 fn currentTabIndex(self: Header) ?usize { 2704 const child_id = self.box.focus.child_id orelse return null; 2705 return self.tab_ids.getIndex(child_id); 2706 } 2707 }; 2708