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