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