╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ thread.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2433 } 2434 2435 fn listInput(self: *This, index: usize, key: Key, root_focus: *Focus) void { 2436 // up/down (and the scroll wheel) move the selection a row; page up/down 2437 // jump a fixed amount. right/Enter cross into the detail pane. up from 2438 // the top row crosses into the header tabs. 2439 if (inp.rowDelta(key, @intCast(self.listBox(index).children.count()))) |delta| { 2440 const lb = self.listBox(index); 2441 const at_top = if (lb.getFocus().child_id) |cid| lb.children.getIndex(cid) == 0 else true; 2442 if (delta < 0 and at_top) return self.focusHeader(root_focus); 2443 moveRowFocus(lb, self.listScroll(index), root_focus, delta); 2444 return; 2445 } 2446 switch (key) { 2447 .enter, .arrow_right => _ = self.detail(index).focusFirst(root_focus), 2448 else => {}, 2449 } 2450 } 2451 2452 fn formInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2453 const form = self.formBox() orelse return; 2454 const cid = form.getFocus().child_id orelse return; 2455 const cur = form.children.getIndex(cid) orelse return; 2456 const child = &form.children.values()[cur]; 2457 2458 if (child.widget == .text_input) { 2459 const text_input = &child.widget.text_input; 2460 if (text_input.options.multiline and try multilineKeepsKey(text_input, allocator, key)) 2461 return child.widget.input(allocator, key, root_focus); 2462 } 2463 2464 const on_submit = child.widget == .submit_button; 2465 switch (key) { 2466 .arrow_up, .back_tab => return if (formStep(form, cur, false)) |i| 2467 root_focus.setFocus(form.children.keys()[i]) 2468 else 2469 self.focusHeader(root_focus), 2470 .arrow_down, .tab => return if (formStep(form, cur, true)) |i| root_focus.setFocus(form.children.keys()[i]), 2471 .enter => if (on_submit) return self.submitForm(allocator), 2472 .mouse => |mouse| if (on_submit) { 2473 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) return self.submitForm(allocator); 2474 }, 2475 else => {}, 2476 } 2477 try child.widget.input(allocator, key, root_focus); 2478 } 2479 2480 fn confirmationFormInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2481 const form = self.confirmationForm() orelse return; 2482 const child_id = form.getFocus().child_id orelse return; 2483 const index = form.children.getIndex(child_id) orelse return; 2484 switch (key) { 2485 .arrow_up, .back_tab => if (index > 1) 2486 root_focus.setFocus(form.children.keys()[index - 1]) 2487 else 2488 self.focusHeader(root_focus), 2489 .arrow_down, .tab => if (index + 1 < form.children.count()) root_focus.setFocus(form.children.keys()[index + 1]), 2490 .enter => try self.submitConfirmation(allocator, index), 2491 .mouse => |mouse| for (form.children.keys()[1..], 1..) |button_id, button_index| { 2492 if (inp.leftClickOn(root_focus, button_id, mouse)) { 2493 try self.submitConfirmation(allocator, button_index); 2494 break; 2495 } 2496 }, 2497 else => {}, 2498 } 2499 } 2500 2501 // the multiline hunk inputs keep enter and any up/down that has a row to 2502 // move to; enter on a "use this" link is a navigation the host follows 2503 // before this runs. 2504 fn resolveInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2505 const form = self.formBox() orelse return; 2506 const cid = form.getFocus().child_id orelse return; 2507 const cur = form.children.getIndex(cid) orelse return; 2508 const child = &form.children.values()[cur]; 2509 2510 if (child.widget == .text_input) { 2511 const ti = &child.widget.text_input; 2512 if (ti.options.multiline and try multilineKeepsKey(ti, allocator, key)) 2513 return child.widget.input(allocator, key, root_focus); 2514 } 2515 2516 const on_submit = child.widget == .submit_button; 2517 2518 switch (key) { 2519 .arrow_up, .back_tab => if (formStep(form, cur, false)) |i| self.focusResolveChild(form, i, root_focus) else self.focusHeader(root_focus), 2520 .arrow_down, .tab => if (formStep(form, cur, true)) |i| self.focusResolveChild(form, i, root_focus), 2521 .arrow_left, .arrow_right => switch (child.widget) { 2522 .box => |*row| _ = widget.moveInRow(row, root_focus, key == .arrow_right), 2523 else => try child.widget.input(allocator, key, root_focus), 2524 }, 2525 .enter => if (on_submit) try self.submitResolution(allocator), 2526 .mouse => |mouse| if (on_submit) { 2527 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) try self.submitResolution(allocator); 2528 }, 2529 else => try child.widget.input(allocator, key, root_focus), 2530 } 2531 } 2532 2533 // a multiline input keeps enter and any up/down that has a row to move to 2534 fn multilineKeepsKey(text_input: *wgt.TextInput, allocator: std.mem.Allocator, key: Key) !bool { 2535 return switch (key) { 2536 .enter => true, 2537 .arrow_up => !try text_input.cursorOnFirstRow(allocator), 2538 .arrow_down => !try text_input.cursorOnLastRow(allocator), 2539 else => false, 2540 }; 2541 } 2542 2543 // the neighboring form control, skipping spacers and blank rows 2544 fn formStep(form: *wgt.Box(Widget), cur: usize, down: bool) ?usize { 2545 var i = cur; 2546 while (true) { 2547 if (down) { 2548 i += 1; 2549 if (i >= form.children.count()) return null; 2550 } else { 2551 if (i == 0) return null; 2552 i -= 1; 2553 } 2554 const child = &form.children.values()[i]; 2555 if (child.hidden) continue; 2556 switch (child.widget) { 2557 .spacer, .text => continue, 2558 else => return i, 2559 } 2560 } 2561 } 2562 2563 // focus the resolve-form child at `i` (a version row focuses its selected 2564 // child) and keep it visible on the terminal 2565 fn focusResolveChild(self: *This, form: *wgt.Box(Widget), i: usize, root_focus: *Focus) void { 2566 const child = &form.children.values()[i]; 2567 switch (child.widget) { 2568 .box => |*row| root_focus.setFocus(row.getFocus().child_id orelse form.children.keys()[i]), 2569 else => root_focus.setFocus(form.children.keys()[i]), 2570 } 2571 if (self.session.is_terminal) { 2572 if (self.resolveScroll()) |sc| { 2573 if (child.rect) |rect| sc.scrollToRect(rect); 2574 } 2575 } 2576 } 2577 2578 // re-emit the conflicted thread's event with the resolved content; any 2579 // event on the thread settles its conflict entry. this is the terminal 2580 // path; the web posts the form to the resolve route. 2581 fn submitResolution(self: *This, allocator: std.mem.Allocator) !void { 2582 if (comptime wasm) return; 2583 const io = self.session.io orelse return; 2584 const src = self.data.repo_source orelse return; 2585 const entry = self.data.selectedThread() orelse return; 2586 const author = ((try self.session.authorize(self.data.identity, .write)) orelse return).author; 2587 const form = self.formBox() orelse return; 2588 2589 // gather the inputs by name; the d<n> hunk inputs appear in chunk order 2590 var title: ?[]u8 = null; 2591 var labels: ?[]u8 = null; 2592 var hunks: std.ArrayList([]const u8) = .empty; 2593 defer { 2594 if (title) |t| allocator.free(t); 2595 if (labels) |t| allocator.free(t); 2596 for (hunks.items) |h| allocator.free(h); 2597 hunks.deinit(allocator); 2598 } 2599 for (form.children.values()) |*child| switch (child.widget) { 2600 .text_input => |*ti| { 2601 const text = try ti.text(allocator); 2602 errdefer allocator.free(text); 2603 if (std.mem.eql(u8, ti.options.name, "title")) { 2604 title = text; 2605 } else if (std.mem.eql(u8, ti.options.name, "labels")) { 2606 labels = text; 2607 } else { 2608 try hunks.append(allocator, text); 2609 } 2610 }, 2611 else => {}, 2612 }; 2613 2614 const id_bytes = try evt.parseEventId(entry.id); 2615 2616 switch (src.repo_kind) { 2617 inline else => |repo_kind| { 2618 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2619 defer any_repo.deinit(io, allocator); 2620 switch (any_repo) { 2621 inline else => |*repo| { 2622 var resolution: Event.Resolve = .{ 2623 .title = title, 2624 .labels = labels, 2625 .hunks = hunks.items, 2626 }; 2627 if (comptime @hasField(Event.Resolve, "theirs")) resolution.theirs = self.data.theirs_picks; 2628 const change = Event.Update{ .resolve = resolution }; 2629 Event.update(self.session.data.host_kind, repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, change, author) catch |err| switch (err) { 2630 // leave the form up for correction 2631 error.InvalidFields => return, 2632 else => |e| return e, 2633 }; 2634 }, 2635 } 2636 }, 2637 } 2638 2639 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2640 try self.session.navigate(route); 2641 } 2642 2643 // commit a new or edited comment and navigate to its permalink. this is the 2644 // terminal path; the web posts the form to its comment route. 2645 fn submitComment(self: *This, allocator: std.mem.Allocator) !void { 2646 if (comptime wasm) return; 2647 const io = self.session.io orelse return; 2648 const src = self.data.repo_source orelse return; 2649 const actor = (try self.session.authorize(self.data.identity, .read)) orelse return; 2650 const author = actor.author; 2651 const form = self.formBox() orelse return; 2652 const body_input = try formField(form, "body"); 2653 const body = try body_input.text(allocator); 2654 defer allocator.free(body); 2655 if (!evt.Comment.fieldsValid(body)) return; 2656 2657 var thread_id: [evt.event_id_size * 2]u8 = undefined; 2658 @memcpy(&thread_id, self.data.selected_id); 2659 const parent_id = if (self.data.comment_id.len == 0) thread_id else blk: { 2660 var parent: [evt.event_id_size * 2]u8 = undefined; 2661 @memcpy(&parent, self.data.comment_id); 2662 break :blk parent; 2663 }; 2664 var event_id_hex: [evt.event_id_size * 2]u8 = undefined; 2665 const editing = self.data.view == .edit_comment; 2666 if (editing) { 2667 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 2668 if (!try src.canModify(io, allocator, actor, .comment, &comment_id)) return; 2669 } 2670 switch (src.repo_kind) { 2671 inline else => |repo_kind| { 2672 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2673 defer any_repo.deinit(io, allocator); 2674 switch (any_repo) { 2675 inline else => |*repo| if (editing) { 2676 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 2677 try evt.Comment.update(self.session.data.host_kind, repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &comment_id, body, author); 2678 event_id_hex = std.fmt.bytesToHex(comment_id, .lower); 2679 } else { 2680 event_id_hex = try evt.Comment.create(self.session.data.host_kind, repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &parent_id, body, author); 2681 }, 2682 } 2683 }, 2684 } 2685 2686 body_input.clear(allocator); 2687 const route = ui.RoutablePage.repoThreadCommentRoute(kind, self.data.identity, &thread_id, &event_id_hex, 0) orelse return; 2688 try self.session.navigate(route); 2689 } 2690 2691 // terminal form submissions; the web posts to the forms' routes instead 2692 fn submitForm(self: *This, allocator: std.mem.Allocator) !void { 2693 switch (self.data.view) { 2694 .new_comment, .edit_comment => try self.submitComment(allocator), 2695 .edit => try self.submitEditedThread(allocator), 2696 else => try self.submitNewThread(allocator), 2697 } 2698 } 2699 2700 // commit the new thread to the repo's events branch and navigate to it. 2701 // this is the terminal path; the web posts the form to the thread route, 2702 // so the wasm side never runs (or compiles) the repo access below. 2703 fn submitNewThread(self: *This, allocator: std.mem.Allocator) !void { 2704 if (comptime wasm) return; 2705 const io = self.session.io orelse return; 2706 const src = self.data.repo_source orelse return; 2707 const author = ((try self.session.authorize(self.data.identity, .read)) orelse return).author; 2708 2709 const form = self.threadForm() orelse return; 2710 const title_input = try formField(form, "title"); 2711 const labels_input = try formField(form, "labels"); 2712 const description_input = try formField(form, "description"); 2713 2714 const title = try title_input.text(allocator); 2715 defer allocator.free(title); 2716 const labels = try labels_input.text(allocator); 2717 defer allocator.free(labels); 2718 const description = try description_input.text(allocator); 2719 defer allocator.free(description); 2720 const target_branch = if (comptime supports_drafts) try (try formField(form, "target_branch")).text(allocator) else ""; 2721 defer if (comptime supports_drafts) allocator.free(target_branch); 2722 2723 if (!Event.fieldsValid(title, labels)) { 2724 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, labels, description, target_branch); 2725 return; 2726 } 2727 2728 if (comptime supports_drafts) { 2729 const source = formWidget(form, .patch_source) orelse return error.MissingFormField; 2730 const source_branch = switch (source.selectedKind()) { 2731 .fork => null, 2732 .branch => try source.field().text(allocator), 2733 }; 2734 defer if (source_branch) |branch| allocator.free(branch); 2735 const event_id_hex = Data.create(self.data, self.session, allocator, author, title, labels, description, target_branch, source_branch) catch |err| { 2736 const failure = FeedbackFailure.fromError(err) orelse return err; 2737 try self.rememberThreadFeedback(failure, title, labels, description, target_branch); 2738 return; 2739 }; 2740 title_input.clear(allocator); 2741 labels_input.clear(allocator); 2742 description_input.clear(allocator); 2743 (try formField(form, "target_branch")).clear(allocator); 2744 const route = ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, &event_id_hex, 0) orelse return; 2745 try self.session.navigate(route); 2746 return; 2747 } 2748 2749 var id_bytes: [evt.event_id_size]u8 = undefined; 2750 io.random(&id_bytes); 2751 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 2752 2753 const event = evt.EventWithId{ 2754 .id = event_id_hex, 2755 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 2756 .author = author, 2757 .event = @unionInit(evt.Event, @tagName(kind), Event{ 2758 .title = title, 2759 .description = description, 2760 .labels = labels, 2761 }), 2762 }; 2763 2764 switch (src.repo_kind) { 2765 inline else => |repo_kind| { 2766 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2767 defer any_repo.deinit(io, allocator); 2768 switch (any_repo) { 2769 inline else => |*repo| try evt.consume(self.session.data.host_kind, .repo, repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 2770 } 2771 }, 2772 } 2773 2774 // wipe the form so a return visit starts fresh 2775 title_input.clear(allocator); 2776 labels_input.clear(allocator); 2777 description_input.clear(allocator); 2778 2779 const route = listRoute(self.data.identity, @enumFromInt(0), "", &event_id_hex) orelse return; 2780 try self.session.navigate(route); 2781 } 2782 2783 // re-emit the selected thread's event with the form's content (status 2784 // preserved), then reload the thread's page. this is the terminal path; 2785 // the web posts the form to the edit route. 2786 fn submitEditedThread(self: *This, allocator: std.mem.Allocator) !void { 2787 if (comptime wasm) return; 2788 const io = self.session.io orelse return; 2789 const entry = self.data.selectedThread() orelse return; 2790 const actor = (try self.session.authorize(self.data.identity, .read)) orelse return; 2791 const author = actor.author; 2792 2793 const form = self.threadForm() orelse return; 2794 const title_input = try formField(form, "title"); 2795 const labels_input = try formField(form, "labels"); 2796 const description_input = try formField(form, "description"); 2797 2798 const title = try title_input.text(allocator); 2799 defer allocator.free(title); 2800 const labels = try labels_input.text(allocator); 2801 defer allocator.free(labels); 2802 const description = try description_input.text(allocator); 2803 defer allocator.free(description); 2804 const target_branch = if (comptime supports_drafts) try (try formField(form, "target_branch")).text(allocator) else ""; 2805 defer if (comptime supports_drafts) allocator.free(target_branch); 2806 2807 if (!Event.fieldsValid(title, labels)) { 2808 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, labels, description, target_branch); 2809 return; 2810 } 2811 const src = self.data.repo_source orelse return; 2812 2813 if (comptime supports_drafts) { 2814 if (entryDraft(entry.*)) { 2815 self.data.editDraft(self.session, allocator, author, entry.id, title, labels, description, target_branch) catch |err| { 2816 const failure = FeedbackFailure.fromError(err) orelse return err; 2817 try self.rememberThreadFeedback(failure, title, labels, description, target_branch); 2818 return; 2819 }; 2820 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2821 try self.session.navigate(route); 2822 return; 2823 } 2824 } 2825 2826 const id_bytes = try evt.parseEventId(entry.id); 2827 if (!try src.canModify(io, allocator, actor, kind, &id_bytes)) return; 2828 2829 switch (src.repo_kind) { 2830 inline else => |repo_kind| { 2831 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2832 defer any_repo.deinit(io, allocator); 2833 switch (any_repo) { 2834 inline else => |*repo| if (supports_drafts) { 2835 Event.update(self.session.data.host_kind, repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2836 .title = title, 2837 .labels = labels, 2838 .description = description, 2839 .target_branch = target_branch, 2840 } }, author) catch |err| { 2841 const failure = FeedbackFailure.fromError(err) orelse return err; 2842 try self.rememberThreadFeedback(failure, title, labels, description, target_branch); 2843 return; 2844 }; 2845 } else if (has_status) 2846 try Event.update(self.session.data.host_kind, repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2847 .title = title, 2848 .labels = labels, 2849 .description = description, 2850 } }, author) 2851 else 2852 try Event.update(self.session.data.host_kind, repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, title, labels, description, author), 2853 } 2854 }, 2855 } 2856 2857 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2858 try self.session.navigate(route); 2859 } 2860 2861 fn rememberThreadFeedback(self: *This, failure: FeedbackFailure, title: []const u8, labels: []const u8, description: []const u8, target_branch: []const u8) !void { 2862 const aa = self.session.arena.allocator(); 2863 const source = if (supports_drafts) blk: { 2864 const form = self.threadForm() orelse break :blk null; 2865 break :blk formWidget(form, .patch_source); 2866 } else null; 2867 self.session.data.form_feedback = @unionInit(ui.Session.FormFeedback, @tagName(feedback_tag), .{ 2868 .failure = failure, 2869 .fields = if (comptime supports_drafts) .{ 2870 .title = try aa.dupe(u8, title), 2871 .labels = try aa.dupe(u8, labels), 2872 .description = try aa.dupe(u8, description), 2873 .target_branch = try aa.dupe(u8, target_branch), 2874 .source_branch = if (source) |value| try value.field().text(aa) else "", 2875 .source_kind = if (source) |value| value.selectedKind() else .fork, 2876 } else .{ 2877 .title = try aa.dupe(u8, title), 2878 .labels = try aa.dupe(u8, labels), 2879 .description = try aa.dupe(u8, description), 2880 }, 2881 }); 2882 } 2883 2884 fn submitConfirmation(self: *This, allocator: std.mem.Allocator, button_index: usize) !void { 2885 switch (confirmation(self.data.view) orelse return) { 2886 .publish => if (comptime supports_drafts) { 2887 if (comptime wasm) return; 2888 const entry = self.data.selectedThread() orelse return; 2889 if (!entryDraft(entry.*)) return; 2890 const author = ((try self.session.authorize(self.data.identity, .read)) orelse return).author; 2891 try self.data.publishDraft(self.session, allocator, author, entry.id); 2892 const route = listRoute(self.data.identity, @enumFromInt(0), "", entry.id) orelse return; 2893 try self.session.navigate(route); 2894 } else unreachable, 2895 .merge => if (comptime supports_merge) { 2896 if (comptime wasm) return; 2897 const entry = self.data.selectedThread() orelse return; 2898 const revision: evt.Patch.MergeRevision = if (button_index > 1) .squash else .source; 2899 if (!Data.canMerge(entry.*, self.session) or entry.mergeability.get(revision) != .clean) return; 2900 const author = ((try self.session.authorize(self.data.identity, .write)) orelse return).author; 2901 self.data.mergePatch(self.session, allocator, author, entry.id, revision) catch |err| switch (err) { 2902 error.MergeConflict, error.MergeCheckUnavailable, error.PatchOutOfDate, error.PatchDataUnavailable => { 2903 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2904 try self.session.navigate(route); 2905 return; 2906 }, 2907 else => return err, 2908 }; 2909 const merged = std.meta.stringToEnum(Status, "merged") orelse return; 2910 const route = listRoute(self.data.identity, merged, "", entry.id) orelse return; 2911 try self.session.navigate(route); 2912 } else unreachable, 2913 .remove => try self.submitRemove(allocator), 2914 } 2915 } 2916 2917 fn submitRemove(self: *This, allocator: std.mem.Allocator) !void { 2918 const comment = self.data.comment_id.len != 0; 2919 const event_id = if (comment) self.data.comment_id else self.data.selected_id; 2920 const id = evt.parseEventId(event_id) catch return; 2921 try self.removeEvent(allocator, if (comment) .comment else kind, id); 2922 } 2923 2924 fn removeEvent(self: *This, allocator: std.mem.Allocator, event_kind: evt.EventKind, id: [evt.event_id_size]u8) !void { 2925 if (comptime wasm) return; 2926 const actor = (try self.session.authorize(self.data.identity, .read)) orelse return; 2927 const author = actor.author; 2928 2929 if (comptime supports_drafts) { 2930 const selected = self.data.selectedThread() orelse return; 2931 if (event_kind == kind and entryDraft(selected.*)) { 2932 try Data.removeDraft(self.session, allocator, author, &id); 2933 const route = draftsRoute(self.data.identity) orelse return; 2934 try self.session.navigate(route); 2935 return; 2936 } 2937 } 2938 2939 const io = self.session.io orelse return; 2940 const src = self.data.repo_source orelse return; 2941 if (!try src.canModify(io, allocator, actor, event_kind, &id)) return; 2942 2943 switch (src.repo_kind) { 2944 inline else => |repo_kind| { 2945 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2946 defer any_repo.deinit(io, allocator); 2947 switch (any_repo) { 2948 inline else => |*repo| try evt.remove(self.session.data.host_kind, .repo, repo_kind, repo.self_repo_opts, io, allocator, repo, &id, event_kind, author), 2949 } 2950 }, 2951 } 2952 2953 const id_hex = std.fmt.bytesToHex(id, .lower); 2954 const route = ui.RoutablePage.repoEventsRoute(self.data.identity, .removed, event_kind, &id_hex, null) orelse return; 2955 try self.session.navigate(route); 2956 } 2957 2958 // return to the list. 2959 fn focusList(self: *This, index: usize, root_focus: *Focus) void { 2960 root_focus.setFocus(self.listScroll(index).getFocus().id); 2961 } 2962 2963 // cross to the header tabs above the stack. 2964 fn focusHeader(self: *This, root_focus: *Focus) void { 2965 root_focus.setFocus(self.box.children.keys()[header_index]); 2966 } 2967 2968 pub fn clearGrid(self: *This) void { 2969 self.box.clearGrid(); 2970 } 2971 2972 pub fn getGrid(self: This) ?Grid { 2973 return self.box.getGrid(); 2974 } 2975 2976 pub fn getFocus(self: *This) *Focus { 2977 return self.box.getFocus(); 2978 } 2979 2980 // for the parent's "scroll up at the top jumps to the header" check: at the 2981 // top only while the header tabs hold focus, so up from the split 2982 // crosses into the tabs first. 2983 pub fn atTop(self: *This) bool { 2984 return self.headerActive(); 2985 } 2986 }; 2987 } 2988