╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ thread.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
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