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