╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ thread.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2647 .text_input => |*ti| try self.session.text_inputs.put(inputs_arena, ti.getFocus().id, ti), 2648 else => {}, 2649 }; 2650 } 2651 2652 try self.box.build(allocator, constraint, root_focus); 2653 } 2654 2655 pub fn input(self: *This, allocator: std.mem.Allocator, raw_key: Key, root_focus: *Focus) !void { 2656 // wheel input moves focus like the arrows 2657 const key: Key = switch (raw_key) { 2658 .scroll_up => .arrow_up, 2659 .scroll_down => .arrow_down, 2660 else => raw_key, 2661 }; 2662 2663 if (self.headerActive()) { 2664 // down from the sub-header re-enters the stack if the selected 2665 // view has something to focus; enter in the search box submits 2666 // the query; other keys move within the sub-header. 2667 if (inp.vertDirection(key) == .down) { 2668 const enterable = if (self.viewStack().getSelected()) |selected| switch (selected.*) { 2669 .word_flow => |*tf| tf.text_boxes.items.len > 0, 2670 // nothing focusable in the unauthorized view 2671 .unauthorized => false, 2672 else => true, 2673 } else false; 2674 if (enterable) root_focus.setFocus(self.box.children.keys()[stack_index]); 2675 } else if (key == .enter) { 2676 if (try self.header().submittedText(allocator)) |submission| { 2677 defer allocator.free(submission.text); 2678 switch (submission.kind) { 2679 .search => try self.submitSearch(submission.text), 2680 .assignee => try self.submitAssignee(submission.text), 2681 } 2682 } 2683 } else { 2684 try self.header().input(allocator, key, root_focus); 2685 } 2686 return; 2687 } 2688 if (self.labelsViewActive()) { 2689 self.labelsViewInput(key, root_focus); 2690 return; 2691 } 2692 if (self.formViewActive()) { 2693 if (confirmation(self.data.view) != null) { 2694 try self.confirmationFormInput(allocator, key, root_focus); 2695 } else if (supports_conflicts and self.data.view == .resolve) { 2696 try self.resolveInput(allocator, key, root_focus); 2697 } else { 2698 try self.formInput(allocator, key, root_focus); 2699 } 2700 return; 2701 } 2702 const i = self.selectedSplitIndex() orelse return; 2703 if (self.detailActive(i)) { 2704 // the way back to the list, by enter or a click 2705 if (widget.listBackId(self.detailFrame(i))) |id| { 2706 if (inp.activated(root_focus, id, key)) return self.focusList(i, root_focus); 2707 if (root_focus.grandchild_id == id) return switch (key) { 2708 .arrow_left => self.focusList(i, root_focus), 2709 .arrow_up => self.focusHeader(root_focus), 2710 .arrow_down => _ = self.detail(i).focusFirst(root_focus), 2711 else => {}, 2712 }; 2713 } 2714 switch (try self.detail(i).inputWithExit(allocator, key, root_focus)) { 2715 .none => {}, 2716 .list => self.focusList(i, root_focus), 2717 .header => if (widget.listBackId(self.detailFrame(i))) |id| root_focus.setFocus(id) else self.focusHeader(root_focus), 2718 } 2719 } else { 2720 try self.listInput(allocator, i, key, root_focus); 2721 } 2722 } 2723 2724 // navigate to the typed query's results, keeping the label filter and the 2725 // status list the page shows. an empty box clears a filtered page and 2726 // does nothing on any other one. 2727 fn submitSearch(self: *This, text: []const u8) !void { 2728 if (text.len == 0 and self.data.search == null) return; 2729 const selected = self.header().getSelectedIndex() orelse 0; 2730 const status: Status = if (selected < status_count) splitStatus(selected) else @fromBackingInt(@intCast(0)); 2731 var route = listRoute(self.data.identity, status, self.data.label, "") orelse return; 2732 const assignee = assigneeFilter(self.data); 2733 if (assignee.len != 0) route = route.withEncodedAssignee(assignee) orelse return; 2734 try self.session.navigate(route.withSearch(text) orelse return); 2735 } 2736 2737 // navigate to the issues assigned to the typed name or email, keeping 2738 // the label, the search and the status list. an empty box clears the 2739 // filter and does nothing on an unfiltered page. 2740 fn submitAssignee(self: *This, typed: []const u8) !void { 2741 const text = std.mem.trim(u8, typed, " \t"); 2742 if (text.len == 0 and assigneeFilter(self.data).len == 0) return; 2743 const selected = self.header().getSelectedIndex() orelse 0; 2744 const status: Status = if (selected < status_count) splitStatus(selected) else @fromBackingInt(@intCast(0)); 2745 var route = listRoute(self.data.identity, status, self.data.label, "") orelse return; 2746 if (self.data.search) |search| route = route.withSearch(search) orelse return; 2747 try self.session.navigate(route.withEncodedAssignee(try ui.urlEncodeRef(self.session.page_arena.allocator(), text)) orelse return); 2748 } 2749 2750 // arrow keys move the label selection; up from the top row crosses to the 2751 // header tabs. 2752 fn labelsViewInput(self: *This, key: Key, root_focus: *Focus) void { 2753 const tf = self.labelsView(); 2754 const cid = tf.focus.child_id orelse return; 2755 const cur = tf.indexOfFocusId(cid) orelse return; 2756 const count = tf.text_boxes.items.len; 2757 switch (key) { 2758 .arrow_left => if (cur > 0) root_focus.setFocus(tf.text_boxes.items[cur - 1].getFocus().id), 2759 .arrow_right => if (cur + 1 < count) root_focus.setFocus(tf.text_boxes.items[cur + 1].getFocus().id), 2760 .arrow_up => if (tf.rowStep(cur, false)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id) else self.focusHeader(root_focus), 2761 .arrow_down => if (tf.rowStep(cur, true)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id), 2762 .home => root_focus.setFocus(tf.text_boxes.items[0].getFocus().id), 2763 .end => root_focus.setFocus(tf.text_boxes.items[count - 1].getFocus().id), 2764 else => {}, 2765 } 2766 } 2767 2768 fn listInput(self: *This, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 2769 // up/down (and the scroll wheel) move the selection a row; page up/down 2770 // jump a fixed amount. right/Enter cross into the detail pane. up from 2771 // the top row crosses into the header tabs. 2772 if (inp.rowDelta(key, @intCast(self.listBox(index).children.count()))) |delta| { 2773 const lb = self.listBox(index); 2774 const at_top = if (lb.getFocus().child_id) |cid| lb.children.getIndex(cid) == 0 else true; 2775 if (delta < 0 and at_top) return self.focusHeader(root_focus); 2776 moveRowFocus(lb, self.listScroll(index), root_focus, delta); 2777 return; 2778 } 2779 switch (key) { 2780 .enter, .arrow_right => self.focusDetail(index, root_focus), 2781 // when the window is too narrow to lay out the detail pane beside 2782 // the list, a click on a row opens it like enter. the row was just 2783 // selected, so the detail is swapped to it ahead of the build. 2784 .mouse => |mouse| if (self.resultsBox(index).children.values()[detail_index].rect == null and widget.clickOnSelectedRow(self.listBox(index), root_focus, mouse)) { 2785 try self.refreshDetail(allocator, index); 2786 self.focusDetail(index, root_focus); 2787 }, 2788 else => {}, 2789 } 2790 } 2791 2792 fn formInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2793 const form = self.formBox() orelse return; 2794 const cid = form.getFocus().child_id orelse return; 2795 const cur = form.children.getIndex(cid) orelse return; 2796 const child = &form.children.values()[cur]; 2797 2798 if (child.widget == .text_input) { 2799 const text_input = &child.widget.text_input; 2800 if (text_input.options.multiline and try multilineKeepsKey(text_input, allocator, key)) 2801 return child.widget.input(allocator, key, root_focus); 2802 } 2803 2804 const on_submit = child.widget == .submit_button; 2805 switch (key) { 2806 .arrow_up, .back_tab => return if (formStep(form, cur, false)) |i| 2807 root_focus.setFocus(form.children.keys()[i]) 2808 else 2809 self.focusHeader(root_focus), 2810 .arrow_down, .tab => return if (formStep(form, cur, true)) |i| root_focus.setFocus(form.children.keys()[i]), 2811 .enter => if (on_submit) return self.submitForm(allocator), 2812 .mouse => |mouse| if (on_submit) { 2813 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) return self.submitForm(allocator); 2814 }, 2815 else => {}, 2816 } 2817 try child.widget.input(allocator, key, root_focus); 2818 } 2819 2820 fn confirmationFormInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2821 const form = self.confirmationForm() orelse return; 2822 const child_id = form.getFocus().child_id orelse return; 2823 const index = form.children.getIndex(child_id) orelse return; 2824 switch (key) { 2825 .arrow_up, .back_tab => if (index > 1) 2826 root_focus.setFocus(form.children.keys()[index - 1]) 2827 else 2828 self.focusHeader(root_focus), 2829 .arrow_down, .tab => if (index + 1 < form.children.count()) root_focus.setFocus(form.children.keys()[index + 1]), 2830 .enter => try self.submitConfirmation(allocator, index), 2831 .mouse => |mouse| for (form.children.keys()[1..], 1..) |button_id, button_index| { 2832 if (inp.leftClickOn(root_focus, button_id, mouse)) { 2833 try self.submitConfirmation(allocator, button_index); 2834 break; 2835 } 2836 }, 2837 else => {}, 2838 } 2839 } 2840 2841 // the multiline hunk inputs keep enter and any up/down that has a row to 2842 // move to; enter on a "use this" link is a navigation the host follows 2843 // before this runs. 2844 fn resolveInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2845 const form = self.formBox() orelse return; 2846 const cid = form.getFocus().child_id orelse return; 2847 const cur = form.children.getIndex(cid) orelse return; 2848 const child = &form.children.values()[cur]; 2849 2850 if (child.widget == .text_input) { 2851 const ti = &child.widget.text_input; 2852 if (ti.options.multiline and try multilineKeepsKey(ti, allocator, key)) 2853 return child.widget.input(allocator, key, root_focus); 2854 } 2855 2856 const on_submit = child.widget == .submit_button; 2857 2858 switch (key) { 2859 .arrow_up, .back_tab => if (formStep(form, cur, false)) |i| self.focusResolveChild(form, i, root_focus) else self.focusHeader(root_focus), 2860 .arrow_down, .tab => if (formStep(form, cur, true)) |i| self.focusResolveChild(form, i, root_focus), 2861 .arrow_left, .arrow_right => switch (child.widget) { 2862 .box => |*row| _ = widget.moveInRow(row, root_focus, key == .arrow_right), 2863 else => try child.widget.input(allocator, key, root_focus), 2864 }, 2865 .enter => if (on_submit) try self.submitResolution(allocator), 2866 .mouse => |mouse| if (on_submit) { 2867 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) try self.submitResolution(allocator); 2868 }, 2869 else => try child.widget.input(allocator, key, root_focus), 2870 } 2871 } 2872 2873 // a multiline input keeps enter and any up/down that has a row to move to 2874 fn multilineKeepsKey(text_input: *wgt.TextInput, allocator: std.mem.Allocator, key: Key) !bool { 2875 return switch (key) { 2876 .enter => true, 2877 .arrow_up => !try text_input.cursorOnFirstRow(allocator), 2878 .arrow_down => !try text_input.cursorOnLastRow(allocator), 2879 else => false, 2880 }; 2881 } 2882 2883 // the neighboring form control, skipping spacers and blank rows 2884 fn formStep(form: *wgt.Box(Widget), cur: usize, down: bool) ?usize { 2885 var i = cur; 2886 while (true) { 2887 if (down) { 2888 i += 1; 2889 if (i >= form.children.count()) return null; 2890 } else { 2891 if (i == 0) return null; 2892 i -= 1; 2893 } 2894 const child = &form.children.values()[i]; 2895 if (child.hidden) continue; 2896 switch (child.widget) { 2897 .spacer, .text => continue, 2898 else => return i, 2899 } 2900 } 2901 } 2902 2903 // focus the resolve-form child at `i` (a version row focuses its selected 2904 // child) and keep it visible on the terminal 2905 fn focusResolveChild(self: *This, form: *wgt.Box(Widget), i: usize, root_focus: *Focus) void { 2906 const child = &form.children.values()[i]; 2907 switch (child.widget) { 2908 .box => |*row| root_focus.setFocus(row.getFocus().child_id orelse form.children.keys()[i]), 2909 else => root_focus.setFocus(form.children.keys()[i]), 2910 } 2911 if (self.session.is_terminal) { 2912 if (self.resolveScroll()) |sc| { 2913 if (child.rect) |rect| sc.scrollToRect(rect); 2914 } 2915 } 2916 } 2917 2918 // re-emit the conflicted thread's event with the resolved content; any 2919 // event on the thread settles its conflict entry. this is the terminal 2920 // path; the web posts the form to the resolve route. 2921 fn submitResolution(self: *This, allocator: std.mem.Allocator) !void { 2922 if (comptime wasm) return; 2923 const io = self.session.io orelse return; 2924 const src = self.data.repo_source orelse return; 2925 const entry = self.data.selectedThread() orelse return; 2926 const author = ((try self.session.authorize(self.data.identity, .write, kind)) orelse return).author; 2927 const form = self.formBox() orelse return; 2928 2929 // gather the inputs by name; the d<n> hunk inputs appear in chunk order 2930 var title: ?[]u8 = null; 2931 var labels: ?[]u8 = null; 2932 var hunks: std.ArrayList([]const u8) = .empty; 2933 defer { 2934 if (title) |t| allocator.free(t); 2935 if (labels) |t| allocator.free(t); 2936 for (hunks.items) |h| allocator.free(h); 2937 hunks.deinit(allocator); 2938 } 2939 for (form.children.values()) |*child| switch (child.widget) { 2940 .text_input => |*ti| { 2941 const text = try ti.text(allocator); 2942 errdefer allocator.free(text); 2943 if (std.mem.eql(u8, ti.options.name, "title")) { 2944 title = text; 2945 } else if (std.mem.eql(u8, ti.options.name, "labels")) { 2946 labels = text; 2947 } else { 2948 try hunks.append(allocator, text); 2949 } 2950 }, 2951 else => {}, 2952 }; 2953 2954 const id_bytes = try evt.parseEventId(entry.id); 2955 2956 switch (src.repo_kind) { 2957 inline else => |repo_kind| { 2958 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2959 defer any_repo.deinit(io, allocator); 2960 switch (any_repo) { 2961 inline else => |*repo| { 2962 var resolution: Event.Resolve = .{ 2963 .title = title, 2964 .labels = labels, 2965 .hunks = hunks.items, 2966 }; 2967 if (comptime @hasField(Event.Resolve, "theirs")) resolution.theirs = self.data.theirs_picks; 2968 const change = Event.Update{ .resolve = resolution }; 2969 Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, change, author) catch |err| switch (err) { 2970 // leave the form up for correction 2971 error.InvalidFields => return, 2972 else => |e| return e, 2973 }; 2974 }, 2975 } 2976 }, 2977 } 2978 2979 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2980 try self.session.navigate(route); 2981 } 2982 2983 // commit a new or edited comment and navigate to its permalink. this is the 2984 // terminal path; the web posts the form to its comment route. 2985 fn submitComment(self: *This, allocator: std.mem.Allocator) !void { 2986 if (comptime wasm) return; 2987 const io = self.session.io orelse return; 2988 const src = self.data.repo_source orelse return; 2989 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 2990 const author = actor.author; 2991 const form = self.formBox() orelse return; 2992 const body_input = try formField(form, "body"); 2993 const body = try body_input.text(allocator); 2994 defer allocator.free(body); 2995 if (!evt.Comment.fieldsValid(body)) return; 2996 2997 var thread_id: [evt.event_id_size * 2]u8 = undefined; 2998 @memcpy(&thread_id, self.data.selected_id); 2999 const parent_id = if (self.data.comment_id.len == 0) thread_id else blk: { 3000 var parent: [evt.event_id_size * 2]u8 = undefined; 3001 @memcpy(&parent, self.data.comment_id); 3002 break :blk parent; 3003 }; 3004 var event_id_hex: [evt.event_id_size * 2]u8 = undefined; 3005 const editing = self.data.view == .edit_comment; 3006 if (editing) { 3007 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 3008 if (!try src.canModify(io, allocator, actor, .comment, &comment_id)) return; 3009 } 3010 switch (src.repo_kind) { 3011 inline else => |repo_kind| { 3012 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 3013 defer any_repo.deinit(io, allocator); 3014 switch (any_repo) { 3015 inline else => |*repo| if (editing) { 3016 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 3017 try evt.Comment.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, kind, &thread_id, &comment_id, body, author); 3018 event_id_hex = std.fmt.bytesToHex(comment_id, .lower); 3019 } else { 3020 event_id_hex = try evt.Comment.create(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, kind, &thread_id, &parent_id, body, author); 3021 }, 3022 } 3023 }, 3024 } 3025 3026 body_input.clear(allocator); 3027 const route = ui.RoutablePage.repoThreadCommentRoute(kind, self.data.identity, &thread_id, &event_id_hex, 0) orelse return; 3028 try self.session.navigate(route); 3029 } 3030 3031 // terminal form submissions; the web posts to the forms' routes instead 3032 fn submitForm(self: *This, allocator: std.mem.Allocator) !void { 3033 switch (self.data.view) { 3034 .new_comment, .edit_comment => try self.submitComment(allocator), 3035 .edit => try self.submitEditedThread(allocator), 3036 else => try self.submitNewThread(allocator), 3037 } 3038 } 3039 3040 // commit the new thread to the repo's events branch and navigate to it. 3041 // this is the terminal path; the web posts the form to the thread route, 3042 // so the wasm side never runs (or compiles) the repo access below. 3043 fn submitNewThread(self: *This, allocator: std.mem.Allocator) !void { 3044 if (comptime wasm) return; 3045 const io = self.session.io orelse return; 3046 const src = self.data.repo_source orelse return; 3047 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 3048 3049 const form = self.threadForm() orelse return; 3050 const title_input = try formField(form, "title"); 3051 const labels_input = try formField(form, "labels"); 3052 const description_input = try formField(form, "description"); 3053 3054 const title = try title_input.text(allocator); 3055 defer allocator.free(title); 3056 const labels = try labels_input.text(allocator); 3057 defer allocator.free(labels); 3058 const description = try description_input.text(allocator); 3059 defer allocator.free(description); 3060 const target_branch = if (comptime supports_drafts) try (try formField(form, "target_branch")).text(allocator) else ""; 3061 defer if (comptime supports_drafts) allocator.free(target_branch); 3062 3063 if (!Event.fieldsValid(title, labels)) { 3064 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, labels, description, target_branch); 3065 return; 3066 } 3067 3068 if (comptime supports_drafts) { 3069 const source = formWidget(form, .patch_source) orelse return error.MissingFormField; 3070 const source_branch = switch (source.selectedKind()) { 3071 .fork => null, 3072 .branch => try source.field().text(allocator), 3073 }; 3074 defer if (source_branch) |branch| allocator.free(branch); 3075 const event_id_hex = Data.create(self.data, self.session, allocator, actor, title, labels, description, target_branch, source_branch) catch |err| { 3076 const failure = FeedbackFailure.fromError(err) orelse return err; 3077 try self.rememberThreadFeedback(failure, title, labels, description, target_branch); 3078 return; 3079 }; 3080 title_input.clear(allocator); 3081 labels_input.clear(allocator); 3082 description_input.clear(allocator); 3083 (try formField(form, "target_branch")).clear(allocator); 3084 const route = ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, &event_id_hex, 0) orelse return; 3085 try self.session.navigate(route); 3086 return; 3087 } 3088 3089 var id_bytes: [evt.event_id_size]u8 = undefined; 3090 io.random(&id_bytes); 3091 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 3092 3093 const event = evt.EventWithId{ 3094 .id = event_id_hex, 3095 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 3096 .author = actor.author, 3097 .event = @unionInit(evt.Event, @tagName(kind), Event{ 3098 .title = title, 3099 .description = description, 3100 .labels = labels, 3101 }), 3102 }; 3103 3104 switch (src.repo_kind) { 3105 inline else => |repo_kind| { 3106 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 3107 defer any_repo.deinit(io, allocator); 3108 switch (any_repo) { 3109 inline else => |*repo| try evt.consume(self.session.eventHost(), .repo, repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 3110 } 3111 }, 3112 } 3113 3114 // wipe the form so a return visit starts fresh 3115 title_input.clear(allocator); 3116 labels_input.clear(allocator); 3117 description_input.clear(allocator); 3118 3119 const route = listRoute(self.data.identity, @fromBackingInt(@intCast(0)), "", &event_id_hex) orelse return; 3120 try self.session.navigate(route); 3121 } 3122 3123 // re-emit the selected thread's event with the form's content (status 3124 // preserved), then reload the thread's page. this is the terminal path; 3125 // the web posts the form to the edit route. 3126 fn submitEditedThread(self: *This, allocator: std.mem.Allocator) !void { 3127 if (comptime wasm) return; 3128 const io = self.session.io orelse return; 3129 const entry = self.data.selectedThread() orelse return; 3130 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 3131 const author = actor.author; 3132 3133 const form = self.threadForm() orelse return; 3134 const title_input = try formField(form, "title"); 3135 const labels_input = try formField(form, "labels"); 3136 const description_input = try formField(form, "description"); 3137 3138 const title = try title_input.text(allocator); 3139 defer allocator.free(title); 3140 const labels = try labels_input.text(allocator); 3141 defer allocator.free(labels); 3142 const description = try description_input.text(allocator); 3143 defer allocator.free(description); 3144 const target_branch = if (comptime supports_drafts) try (try formField(form, "target_branch")).text(allocator) else ""; 3145 defer if (comptime supports_drafts) allocator.free(target_branch); 3146 3147 if (!Event.fieldsValid(title, labels)) { 3148 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, labels, description, target_branch); 3149 return; 3150 } 3151 const src = self.data.repo_source orelse return; 3152 3153 if (comptime supports_drafts) { 3154 if (entryDraft(entry.*)) { 3155 self.data.editDraft(self.session, allocator, author, entry.id, title, labels, description, target_branch) catch |err| { 3156 const failure = FeedbackFailure.fromError(err) orelse return err; 3157 try self.rememberThreadFeedback(failure, title, labels, description, target_branch); 3158 return; 3159 }; 3160 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 3161 try self.session.navigate(route); 3162 return; 3163 } 3164 } 3165 3166 const id_bytes = try evt.parseEventId(entry.id); 3167 if (!try src.canModify(io, allocator, actor, kind, &id_bytes)) return; 3168 3169 switch (src.repo_kind) { 3170 inline else => |repo_kind| { 3171 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 3172 defer any_repo.deinit(io, allocator); 3173 switch (any_repo) { 3174 inline else => |*repo| if (supports_drafts) { 3175 Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 3176 .title = title, 3177 .labels = labels, 3178 .description = description, 3179 .target_branch = target_branch, 3180 } }, author) catch |err| { 3181 const failure = FeedbackFailure.fromError(err) orelse return err; 3182 try self.rememberThreadFeedback(failure, title, labels, description, target_branch); 3183 return; 3184 }; 3185 } else if (has_status) 3186 try Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 3187 .title = title, 3188 .labels = labels, 3189 .description = description, 3190 } }, author) 3191 else 3192 try Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, title, labels, description, author), 3193 } 3194 }, 3195 } 3196 3197 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 3198 try self.session.navigate(route); 3199 } 3200 3201 fn rememberThreadFeedback(self: *This, failure: FeedbackFailure, title: []const u8, labels: []const u8, description: []const u8, target_branch: []const u8) !void { 3202 const aa = self.session.arena.allocator(); 3203 const source = if (supports_drafts) blk: { 3204 const form = self.threadForm() orelse break :blk null; 3205 break :blk formWidget(form, .patch_source); 3206 } else null; 3207 self.session.data.form_feedback = @unionInit(ui.Session.FormFeedback, @tagName(feedback_tag), .{ 3208 .failure = failure, 3209 .fields = if (comptime supports_drafts) .{ 3210 .title = try aa.dupe(u8, title), 3211 .labels = try aa.dupe(u8, labels), 3212 .description = try aa.dupe(u8, description), 3213 .target_branch = try aa.dupe(u8, target_branch), 3214 .source_branch = if (source) |value| try value.field().text(aa) else "", 3215 .source_kind = if (source) |value| value.selectedKind() else .fork, 3216 } else .{ 3217 .title = try aa.dupe(u8, title), 3218 .labels = try aa.dupe(u8, labels), 3219 .description = try aa.dupe(u8, description), 3220 }, 3221 }); 3222 } 3223 3224 fn submitConfirmation(self: *This, allocator: std.mem.Allocator, button_index: usize) !void { 3225 switch (confirmation(self.data.view) orelse return) { 3226 .publish => if (comptime supports_drafts) { 3227 if (comptime wasm) return; 3228 const entry = self.data.selectedThread() orelse return; 3229 if (!entryDraft(entry.*)) return; 3230 const author = ((try self.session.authorize(self.data.identity, .read, kind)) orelse return).author; 3231 try self.data.publishDraft(self.session, allocator, author, entry.id); 3232 const route = listRoute(self.data.identity, @fromBackingInt(@intCast(0)), "", entry.id) orelse return; 3233 try self.session.navigate(route); 3234 } else unreachable, 3235 .merge => if (comptime supports_merge) { 3236 if (comptime wasm) return; 3237 const entry = self.data.selectedThread() orelse return; 3238 const revision: evt.Patch.MergeRevision = if (button_index > 1) .squash else .source; 3239 if (!Data.canMerge(entry.*, self.session) or entry.mergeability.get(revision) != .clean) return; 3240 const author = ((try self.session.authorize(self.data.identity, .write, kind)) orelse return).author; 3241 self.data.mergePatch(self.session, allocator, author, entry.id, revision) catch |err| switch (err) { 3242 error.MergeConflict, error.MergeCheckUnavailable, error.PatchOutOfDate, error.PatchDataUnavailable => { 3243 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 3244 try self.session.navigate(route); 3245 return; 3246 }, 3247 else => return err, 3248 }; 3249 const merged = std.meta.stringToEnum(Status, "merged") orelse return; 3250 const route = listRoute(self.data.identity, merged, "", entry.id) orelse return; 3251 try self.session.navigate(route); 3252 } else unreachable, 3253 .remove => try self.submitRemove(allocator), 3254 } 3255 } 3256 3257 fn submitRemove(self: *This, allocator: std.mem.Allocator) !void { 3258 const comment = self.data.comment_id.len != 0; 3259 const event_id = if (comment) self.data.comment_id else self.data.selected_id; 3260 const id = evt.parseEventId(event_id) catch return; 3261 try self.removeEvent(allocator, if (comment) .comment else kind, id); 3262 } 3263 3264 fn removeEvent(self: *This, allocator: std.mem.Allocator, event_kind: evt.EventKind, id: [evt.event_id_size]u8) !void { 3265 if (comptime wasm) return; 3266 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 3267 const author = actor.author; 3268 3269 if (comptime supports_drafts) { 3270 const selected = self.data.selectedThread() orelse return; 3271 if (event_kind == kind and entryDraft(selected.*)) { 3272 try Data.removeDraft(self.session, allocator, author, &id); 3273 const route = draftsRoute(self.data.identity) orelse return; 3274 try self.session.navigate(route); 3275 return; 3276 } 3277 } 3278 3279 const io = self.session.io orelse return; 3280 const src = self.data.repo_source orelse return; 3281 if (!try src.canModify(io, allocator, actor, event_kind, &id)) return; 3282 const thread_id = evt.parseEventId(self.data.selected_id) catch return; 3283 3284 switch (src.repo_kind) { 3285 inline else => |repo_kind| { 3286 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 3287 defer any_repo.deinit(io, allocator); 3288 switch (any_repo) { 3289 inline else => |*repo| try evt.removeInThread(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, kind, &thread_id, event_kind, &id, author), 3290 } 3291 }, 3292 } 3293 3294 const id_hex = std.fmt.bytesToHex(id, .lower); 3295 const route = ui.RoutablePage.repoEventsRoute(self.data.identity, .removed, event_kind, &id_hex, null) orelse return; 3296 try self.session.navigate(route); 3297 } 3298 3299 // enter the detail pane, landing on the way back to the list when it shows 3300 fn focusDetail(self: *This, index: usize, root_focus: *Focus) void { 3301 if (widget.listBackId(self.detailFrame(index)) != null) return widget.focusListBack(self.detailFrame(index), root_focus); 3302 _ = self.detail(index).focusFirst(root_focus); 3303 } 3304 3305 // return to the list. 3306 fn focusList(self: *This, index: usize, root_focus: *Focus) void { 3307 root_focus.setFocus(self.listScroll(index).getFocus().id); 3308 } 3309 3310 // cross to the header tabs above the stack. 3311 fn focusHeader(self: *This, root_focus: *Focus) void { 3312 root_focus.setFocus(self.box.children.keys()[header_index]); 3313 } 3314 3315 pub fn clearGrid(self: *This) void { 3316 self.box.clearGrid(); 3317 } 3318 3319 pub fn getGrid(self: This) ?Grid { 3320 return self.box.getGrid(); 3321 } 3322 3323 pub fn getFocus(self: *This) *Focus { 3324 return self.box.getFocus(); 3325 } 3326 3327 // for the parent's "scroll up at the top jumps to the header" check: at the 3328 // top only while the header tabs hold focus, so up from the split 3329 // crosses into the tabs first. 3330 pub fn atTop(self: *This) bool { 3331 return self.headerActive(); 3332 } 3333 }; 3334 } 3335