╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ thread.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2811 .arrow_up => if (tf.rowStep(cur, false)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id) else self.focusHeader(root_focus), 2812 .arrow_down => if (tf.rowStep(cur, true)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id), 2813 .home => root_focus.setFocus(tf.text_boxes.items[0].getFocus().id), 2814 .end => root_focus.setFocus(tf.text_boxes.items[count - 1].getFocus().id), 2815 else => {}, 2816 } 2817 } 2818 2819 fn listInput(self: *This, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 2820 // up/down (and the scroll wheel) move the selection a row; page up/down 2821 // jump a fixed amount. right/Enter cross into the detail pane. up from 2822 // the top row crosses into the header tabs. 2823 if (inp.rowDelta(key, @intCast(self.listBox(index).children.count()))) |delta| { 2824 const lb = self.listBox(index); 2825 const at_top = if (lb.getFocus().child_id) |cid| lb.children.getIndex(cid) == 0 else true; 2826 if (delta < 0 and at_top) return self.focusHeader(root_focus); 2827 moveRowFocus(lb, self.listScroll(index), root_focus, delta); 2828 return; 2829 } 2830 switch (key) { 2831 .enter, .arrow_right => self.focusDetail(index, root_focus), 2832 // when the window is too narrow to lay out the detail pane beside 2833 // the list, a click on a row opens it like enter. the row was just 2834 // selected, so the detail is swapped to it ahead of the build. 2835 .mouse => |mouse| if (self.resultsBox(index).children.values()[detail_index].rect == null and widget.clickOnSelectedRow(self.listBox(index), root_focus, mouse)) { 2836 try self.refreshDetail(allocator, index); 2837 self.focusDetail(index, root_focus); 2838 }, 2839 else => {}, 2840 } 2841 } 2842 2843 fn formInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2844 const form = self.formBox() orelse return; 2845 const cid = form.getFocus().child_id orelse return; 2846 const cur = form.children.getIndex(cid) orelse return; 2847 const child = &form.children.values()[cur]; 2848 2849 if (child.widget == .text_input) { 2850 const text_input = &child.widget.text_input; 2851 if (text_input.options.multiline and try multilineKeepsKey(text_input, allocator, key)) 2852 return child.widget.input(allocator, key, root_focus); 2853 } 2854 2855 const on_submit = child.widget == .submit_button; 2856 switch (key) { 2857 .arrow_up, .back_tab => return if (formStep(form, cur, false)) |i| 2858 root_focus.setFocus(form.children.keys()[i]) 2859 else 2860 self.focusHeader(root_focus), 2861 .arrow_down, .tab => return if (formStep(form, cur, true)) |i| root_focus.setFocus(form.children.keys()[i]), 2862 .enter => if (on_submit) return self.submitForm(allocator), 2863 .mouse => |mouse| if (on_submit) { 2864 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) return self.submitForm(allocator); 2865 }, 2866 else => {}, 2867 } 2868 try child.widget.input(allocator, key, root_focus); 2869 } 2870 2871 fn confirmationFormInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2872 const form = self.confirmationForm() orelse return; 2873 const child_id = form.getFocus().child_id orelse return; 2874 const index = form.children.getIndex(child_id) orelse return; 2875 switch (key) { 2876 .arrow_up, .back_tab => if (index > 1) 2877 root_focus.setFocus(form.children.keys()[index - 1]) 2878 else 2879 self.focusHeader(root_focus), 2880 .arrow_down, .tab => if (index + 1 < form.children.count()) root_focus.setFocus(form.children.keys()[index + 1]), 2881 .enter => try self.submitConfirmation(allocator, index), 2882 .mouse => |mouse| for (form.children.keys()[1..], 1..) |button_id, button_index| { 2883 if (inp.leftClickOn(root_focus, button_id, mouse)) { 2884 try self.submitConfirmation(allocator, button_index); 2885 break; 2886 } 2887 }, 2888 else => {}, 2889 } 2890 } 2891 2892 // the multiline hunk inputs keep enter and any up/down that has a row to 2893 // move to; enter on a "use this" link is a navigation the host follows 2894 // before this runs. 2895 fn resolveInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2896 const form = self.formBox() orelse return; 2897 const cid = form.getFocus().child_id orelse return; 2898 const cur = form.children.getIndex(cid) orelse return; 2899 const child = &form.children.values()[cur]; 2900 2901 if (child.widget == .text_input) { 2902 const ti = &child.widget.text_input; 2903 if (ti.options.multiline and try multilineKeepsKey(ti, allocator, key)) 2904 return child.widget.input(allocator, key, root_focus); 2905 } 2906 2907 const on_submit = child.widget == .submit_button; 2908 2909 switch (key) { 2910 .arrow_up, .back_tab => if (formStep(form, cur, false)) |i| self.focusResolveChild(form, i, root_focus) else self.focusHeader(root_focus), 2911 .arrow_down, .tab => if (formStep(form, cur, true)) |i| self.focusResolveChild(form, i, root_focus), 2912 .arrow_left, .arrow_right => switch (child.widget) { 2913 .box => |*row| _ = widget.moveInRow(row, root_focus, key == .arrow_right), 2914 else => try child.widget.input(allocator, key, root_focus), 2915 }, 2916 .enter => if (on_submit) try self.submitResolution(allocator), 2917 .mouse => |mouse| if (on_submit) { 2918 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) try self.submitResolution(allocator); 2919 }, 2920 else => try child.widget.input(allocator, key, root_focus), 2921 } 2922 } 2923 2924 // a multiline input keeps enter and any up/down that has a row to move to 2925 fn multilineKeepsKey(text_input: *wgt.TextInput, allocator: std.mem.Allocator, key: Key) !bool { 2926 return switch (key) { 2927 .enter => true, 2928 .arrow_up => !try text_input.cursorOnFirstRow(allocator), 2929 .arrow_down => !try text_input.cursorOnLastRow(allocator), 2930 else => false, 2931 }; 2932 } 2933 2934 // the neighboring form control, skipping spacers and blank rows 2935 fn formStep(form: *wgt.Box(Widget), cur: usize, down: bool) ?usize { 2936 var i = cur; 2937 while (true) { 2938 if (down) { 2939 i += 1; 2940 if (i >= form.children.count()) return null; 2941 } else { 2942 if (i == 0) return null; 2943 i -= 1; 2944 } 2945 const child = &form.children.values()[i]; 2946 if (child.hidden) continue; 2947 switch (child.widget) { 2948 .spacer, .text => continue, 2949 else => return i, 2950 } 2951 } 2952 } 2953 2954 // focus the resolve-form child at `i` (a version row focuses its selected 2955 // child) and keep it visible on the terminal 2956 fn focusResolveChild(self: *This, form: *wgt.Box(Widget), i: usize, root_focus: *Focus) void { 2957 const child = &form.children.values()[i]; 2958 switch (child.widget) { 2959 .box => |*row| root_focus.setFocus(row.getFocus().child_id orelse form.children.keys()[i]), 2960 else => root_focus.setFocus(form.children.keys()[i]), 2961 } 2962 if (self.session.is_terminal) { 2963 if (self.resolveScroll()) |sc| { 2964 if (child.rect) |rect| sc.scrollToRect(rect); 2965 } 2966 } 2967 } 2968 2969 // re-emit the conflicted thread's event with the resolved content; any 2970 // event on the thread settles its conflict entry. this is the terminal 2971 // path; the web posts the form to the resolve route. 2972 fn submitResolution(self: *This, allocator: std.mem.Allocator) !void { 2973 if (comptime wasm) return; 2974 const io = self.session.io orelse return; 2975 const src = self.data.repo_source orelse return; 2976 const entry = self.data.selectedThread() orelse return; 2977 const author = ((try self.session.authorize(self.data.identity, .write, kind)) orelse return).author; 2978 const form = self.formBox() orelse return; 2979 2980 // gather the inputs by name; the d<n> hunk inputs appear in chunk order 2981 var title: ?[]u8 = null; 2982 var labels: ?[]u8 = null; 2983 var hunks: std.ArrayList([]const u8) = .empty; 2984 defer { 2985 if (title) |t| allocator.free(t); 2986 if (labels) |t| allocator.free(t); 2987 for (hunks.items) |h| allocator.free(h); 2988 hunks.deinit(allocator); 2989 } 2990 for (form.children.values()) |*child| switch (child.widget) { 2991 .text_input => |*ti| { 2992 const text = try ti.text(allocator); 2993 errdefer allocator.free(text); 2994 if (std.mem.eql(u8, ti.options.name, "title")) { 2995 title = text; 2996 } else if (std.mem.eql(u8, ti.options.name, "labels")) { 2997 labels = text; 2998 } else { 2999 try hunks.append(allocator, text); 3000 } 3001 }, 3002 else => {}, 3003 }; 3004 3005 const id_bytes = try evt.parseEventId(entry.id); 3006 3007 switch (src.repo_kind) { 3008 inline else => |repo_kind| { 3009 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 3010 defer any_repo.deinit(io, allocator); 3011 switch (any_repo) { 3012 inline else => |*repo| { 3013 var resolution: Event.Resolve = .{ 3014 .title = title, 3015 .labels = labels, 3016 .hunks = hunks.items, 3017 }; 3018 if (comptime @hasField(Event.Resolve, "theirs")) resolution.theirs = self.data.theirs_picks; 3019 const change = Event.Update{ .resolve = resolution }; 3020 Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, change, author) catch |err| switch (err) { 3021 // leave the form up for correction 3022 error.InvalidFields => return, 3023 else => |e| return e, 3024 }; 3025 }, 3026 } 3027 }, 3028 } 3029 3030 const route = listRoute(self.data.identity, entryStatus(entry.*), entry.id) orelse return; 3031 try self.session.navigate(route); 3032 } 3033 3034 // commit a new or edited comment and navigate to its permalink. this is the 3035 // terminal path; the web posts the form to its comment route. 3036 fn submitComment(self: *This, allocator: std.mem.Allocator) !void { 3037 if (comptime wasm) return; 3038 const io = self.session.io orelse return; 3039 const src = self.data.repo_source orelse return; 3040 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 3041 const author = actor.author; 3042 const form = self.formBox() orelse return; 3043 const body_input = try formField(form, "body"); 3044 const body = try body_input.text(allocator); 3045 defer allocator.free(body); 3046 if (!evt.Comment.fieldsValid(body)) return; 3047 3048 var thread_id: [evt.event_id_size * 2]u8 = undefined; 3049 @memcpy(&thread_id, self.data.selected_id); 3050 const parent_id = if (self.data.comment_id.len == 0) thread_id else blk: { 3051 var parent: [evt.event_id_size * 2]u8 = undefined; 3052 @memcpy(&parent, self.data.comment_id); 3053 break :blk parent; 3054 }; 3055 var event_id_hex: [evt.event_id_size * 2]u8 = undefined; 3056 const editing = self.data.view == .edit_comment; 3057 if (editing) { 3058 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 3059 if (!try src.canModify(io, allocator, actor, .comment, &comment_id)) return; 3060 } 3061 switch (src.repo_kind) { 3062 inline else => |repo_kind| { 3063 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 3064 defer any_repo.deinit(io, allocator); 3065 switch (any_repo) { 3066 inline else => |*repo| if (editing) { 3067 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 3068 try evt.Comment.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, kind, &thread_id, &comment_id, body, author); 3069 event_id_hex = std.fmt.bytesToHex(comment_id, .lower); 3070 } else { 3071 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); 3072 }, 3073 } 3074 }, 3075 } 3076 3077 body_input.clear(allocator); 3078 const route = ui.RoutablePage.repoThreadCommentRoute(kind, self.data.identity, &thread_id, &event_id_hex, 0) orelse return; 3079 try self.session.navigate(route); 3080 } 3081 3082 // terminal form submissions; the web posts to the forms' routes instead 3083 fn submitForm(self: *This, allocator: std.mem.Allocator) !void { 3084 switch (self.data.view) { 3085 .new_comment, .edit_comment => try self.submitComment(allocator), 3086 .edit => try self.submitEditedThread(allocator), 3087 else => try self.submitNewThread(allocator), 3088 } 3089 } 3090 3091 // commit the new thread to the repo's events branch and navigate to it. 3092 // this is the terminal path; the web posts the form to the thread route, 3093 // so the wasm side never runs (or compiles) the repo access below. 3094 fn submitNewThread(self: *This, allocator: std.mem.Allocator) !void { 3095 if (comptime wasm) return; 3096 const io = self.session.io orelse return; 3097 const src = self.data.repo_source orelse return; 3098 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 3099 3100 const form = self.threadForm() orelse return; 3101 const title_input = try formField(form, "title"); 3102 const labels_input = try formField(form, "labels"); 3103 const description_input = try formField(form, "description"); 3104 3105 const title = try title_input.text(allocator); 3106 defer allocator.free(title); 3107 const labels = try labels_input.text(allocator); 3108 defer allocator.free(labels); 3109 const description = try description_input.text(allocator); 3110 defer allocator.free(description); 3111 const target_branch = if (comptime supports_drafts) try (try formField(form, "target_branch")).text(allocator) else ""; 3112 defer if (comptime supports_drafts) allocator.free(target_branch); 3113 3114 if (!Event.fieldsValid(title, labels)) { 3115 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, labels, description, target_branch); 3116 return; 3117 } 3118 3119 if (comptime supports_drafts) { 3120 const source = formWidget(form, .patch_source) orelse return error.MissingFormField; 3121 const source_branch = switch (source.selectedKind()) { 3122 .fork => null, 3123 .branch => try source.field().text(allocator), 3124 }; 3125 defer if (source_branch) |branch| allocator.free(branch); 3126 const event_id_hex = Data.create(self.data, self.session, allocator, actor, title, labels, description, target_branch, source_branch) catch |err| { 3127 const failure = FeedbackFailure.fromError(err) orelse return err; 3128 try self.rememberThreadFeedback(failure, title, labels, description, target_branch); 3129 return; 3130 }; 3131 title_input.clear(allocator); 3132 labels_input.clear(allocator); 3133 description_input.clear(allocator); 3134 (try formField(form, "target_branch")).clear(allocator); 3135 const route = ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, &event_id_hex, 0) orelse return; 3136 try self.session.navigate(route); 3137 return; 3138 } 3139 3140 var id_bytes: [evt.event_id_size]u8 = undefined; 3141 io.random(&id_bytes); 3142 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 3143 3144 const event = evt.EventWithId{ 3145 .id = event_id_hex, 3146 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 3147 .author = actor.author, 3148 .event = @unionInit(evt.Event, @tagName(kind), Event{ 3149 .title = title, 3150 .description = description, 3151 .labels = labels, 3152 }), 3153 }; 3154 3155 switch (src.repo_kind) { 3156 inline else => |repo_kind| { 3157 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 3158 defer any_repo.deinit(io, allocator); 3159 switch (any_repo) { 3160 inline else => |*repo| try evt.consume(self.session.eventHost(), .repo, repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 3161 } 3162 }, 3163 } 3164 3165 // wipe the form so a return visit starts fresh 3166 title_input.clear(allocator); 3167 labels_input.clear(allocator); 3168 description_input.clear(allocator); 3169 3170 const route = listRoute(self.data.identity, @fromBackingInt(@intCast(0)), &event_id_hex) orelse return; 3171 try self.session.navigate(route); 3172 } 3173 3174 // re-emit the selected thread's event with the form's content (status 3175 // preserved), then reload the thread's page. this is the terminal path; 3176 // the web posts the form to the edit route. 3177 fn submitEditedThread(self: *This, allocator: std.mem.Allocator) !void { 3178 if (comptime wasm) return; 3179 const io = self.session.io orelse return; 3180 const entry = self.data.selectedThread() orelse return; 3181 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 3182 const author = actor.author; 3183 3184 const form = self.threadForm() orelse return; 3185 const title_input = try formField(form, "title"); 3186 const labels_input = try formField(form, "labels"); 3187 const description_input = try formField(form, "description"); 3188 3189 const title = try title_input.text(allocator); 3190 defer allocator.free(title); 3191 const labels = try labels_input.text(allocator); 3192 defer allocator.free(labels); 3193 const description = try description_input.text(allocator); 3194 defer allocator.free(description); 3195 const target_branch = if (comptime supports_drafts) try (try formField(form, "target_branch")).text(allocator) else ""; 3196 defer if (comptime supports_drafts) allocator.free(target_branch); 3197 3198 if (!Event.fieldsValid(title, labels)) { 3199 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, labels, description, target_branch); 3200 return; 3201 } 3202 const src = self.data.repo_source orelse return; 3203 3204 if (comptime supports_drafts) { 3205 if (entryDraft(entry.*)) { 3206 self.data.editDraft(self.session, allocator, author, entry.id, title, labels, description, target_branch) catch |err| { 3207 const failure = FeedbackFailure.fromError(err) orelse return err; 3208 try self.rememberThreadFeedback(failure, title, labels, description, target_branch); 3209 return; 3210 }; 3211 const route = listRoute(self.data.identity, entryStatus(entry.*), entry.id) orelse return; 3212 try self.session.navigate(route); 3213 return; 3214 } 3215 } 3216 3217 const id_bytes = try evt.parseEventId(entry.id); 3218 if (!try src.canModify(io, allocator, actor, kind, &id_bytes)) return; 3219 3220 switch (src.repo_kind) { 3221 inline else => |repo_kind| { 3222 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 3223 defer any_repo.deinit(io, allocator); 3224 switch (any_repo) { 3225 inline else => |*repo| if (supports_drafts) { 3226 Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 3227 .title = title, 3228 .labels = labels, 3229 .description = description, 3230 .target_branch = target_branch, 3231 } }, author) catch |err| { 3232 const failure = FeedbackFailure.fromError(err) orelse return err; 3233 try self.rememberThreadFeedback(failure, title, labels, description, target_branch); 3234 return; 3235 }; 3236 } else if (has_status) 3237 try Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 3238 .title = title, 3239 .labels = labels, 3240 .description = description, 3241 } }, author) 3242 else 3243 try Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, title, labels, description, author), 3244 } 3245 }, 3246 } 3247 3248 const route = listRoute(self.data.identity, entryStatus(entry.*), entry.id) orelse return; 3249 try self.session.navigate(route); 3250 } 3251 3252 fn rememberThreadFeedback(self: *This, failure: FeedbackFailure, title: []const u8, labels: []const u8, description: []const u8, target_branch: []const u8) !void { 3253 const aa = self.session.arena.allocator(); 3254 const source = if (supports_drafts) blk: { 3255 const form = self.threadForm() orelse break :blk null; 3256 break :blk formWidget(form, .patch_source); 3257 } else null; 3258 self.session.data.form_feedback = @unionInit(ui.Session.FormFeedback, @tagName(feedback_tag), .{ 3259 .failure = failure, 3260 .fields = if (comptime supports_drafts) .{ 3261 .title = try aa.dupe(u8, title), 3262 .labels = try aa.dupe(u8, labels), 3263 .description = try aa.dupe(u8, description), 3264 .target_branch = try aa.dupe(u8, target_branch), 3265 .source_branch = if (source) |value| try value.field().text(aa) else "", 3266 .source_kind = if (source) |value| value.selectedKind() else .fork, 3267 } else .{ 3268 .title = try aa.dupe(u8, title), 3269 .labels = try aa.dupe(u8, labels), 3270 .description = try aa.dupe(u8, description), 3271 }, 3272 }); 3273 } 3274 3275 fn submitConfirmation(self: *This, allocator: std.mem.Allocator, button_index: usize) !void { 3276 switch (confirmation(self.data.view) orelse return) { 3277 .publish => if (comptime supports_drafts) { 3278 if (comptime wasm) return; 3279 const entry = self.data.selectedThread() orelse return; 3280 if (!entryDraft(entry.*)) return; 3281 const author = ((try self.session.authorize(self.data.identity, .read, kind)) orelse return).author; 3282 try self.data.publishDraft(self.session, allocator, author, entry.id); 3283 const route = listRoute(self.data.identity, @fromBackingInt(@intCast(0)), entry.id) orelse return; 3284 try self.session.navigate(route); 3285 } else unreachable, 3286 .merge => if (comptime supports_merge) { 3287 if (comptime wasm) return; 3288 const entry = self.data.selectedThread() orelse return; 3289 const revision: evt.Patch.MergeRevision = if (button_index > 1) .squash else .source; 3290 if (!Data.canMerge(entry.*, self.session) or entry.mergeability.get(revision) != .clean) return; 3291 const author = ((try self.session.authorize(self.data.identity, .write, kind)) orelse return).author; 3292 self.data.mergePatch(self.session, allocator, author, entry.id, revision) catch |err| switch (err) { 3293 error.MergeConflict, error.MergeCheckUnavailable, error.PatchOutOfDate, error.PatchDataUnavailable => { 3294 const route = listRoute(self.data.identity, entryStatus(entry.*), entry.id) orelse return; 3295 try self.session.navigate(route); 3296 return; 3297 }, 3298 else => return err, 3299 }; 3300 const merged = std.meta.stringToEnum(Status, "merged") orelse return; 3301 const route = listRoute(self.data.identity, merged, entry.id) orelse return; 3302 try self.session.navigate(route); 3303 } else unreachable, 3304 .remove => try self.submitRemove(allocator), 3305 } 3306 } 3307 3308 fn submitRemove(self: *This, allocator: std.mem.Allocator) !void { 3309 const comment = self.data.comment_id.len != 0; 3310 const event_id = if (comment) self.data.comment_id else self.data.selected_id; 3311 const id = evt.parseEventId(event_id) catch return; 3312 try self.removeEvent(allocator, if (comment) .comment else kind, id); 3313 } 3314 3315 fn removeEvent(self: *This, allocator: std.mem.Allocator, event_kind: evt.EventKind, id: [evt.event_id_size]u8) !void { 3316 if (comptime wasm) return; 3317 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 3318 const author = actor.author; 3319 3320 if (comptime supports_drafts) { 3321 const selected = self.data.selectedThread() orelse return; 3322 if (event_kind == kind and entryDraft(selected.*)) { 3323 try Data.removeDraft(self.session, allocator, author, &id); 3324 const route = draftsRoute(self.data.identity) orelse return; 3325 try self.session.navigate(route); 3326 return; 3327 } 3328 } 3329 3330 const io = self.session.io orelse return; 3331 const src = self.data.repo_source orelse return; 3332 if (!try src.canModify(io, allocator, actor, event_kind, &id)) return; 3333 const thread_id = evt.parseEventId(self.data.selected_id) catch return; 3334 3335 switch (src.repo_kind) { 3336 inline else => |repo_kind| { 3337 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 3338 defer any_repo.deinit(io, allocator); 3339 switch (any_repo) { 3340 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), 3341 } 3342 }, 3343 } 3344 3345 const id_hex = std.fmt.bytesToHex(id, .lower); 3346 const route = ui.RoutablePage.repoEventsRoute(self.data.identity, .removed, event_kind, &id_hex, null) orelse return; 3347 try self.session.navigate(route); 3348 } 3349 3350 // enter the detail pane, landing on the way back to the list when it shows 3351 fn focusDetail(self: *This, index: usize, root_focus: *Focus) void { 3352 if (widget.listBackId(self.detailFrame(index)) != null) return widget.focusListBack(self.detailFrame(index), root_focus); 3353 _ = self.detail(index).focusFirst(root_focus); 3354 } 3355 3356 // return to the list. 3357 fn focusList(self: *This, index: usize, root_focus: *Focus) void { 3358 root_focus.setFocus(self.listScroll(index).getFocus().id); 3359 } 3360 3361 // cross to the header tabs above the stack. 3362 fn focusHeader(self: *This, root_focus: *Focus) void { 3363 root_focus.setFocus(self.box.children.keys()[header_index]); 3364 } 3365 3366 pub fn clearGrid(self: *This) void { 3367 self.box.clearGrid(); 3368 } 3369 3370 pub fn getGrid(self: This) ?Grid { 3371 return self.box.getGrid(); 3372 } 3373 3374 pub fn getFocus(self: *This) *Focus { 3375 return self.box.getFocus(); 3376 } 3377 3378 // for the parent's "scroll up at the top jumps to the header" check: at the 3379 // top only while the header tabs hold focus, so up from the split 3380 // crosses into the tabs first. 3381 pub fn atTop(self: *This) bool { 3382 return self.headerActive(); 3383 } 3384 }; 3385 } 3386