╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ thread.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2985 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2986 defer any_repo.deinit(io, allocator); 2987 switch (any_repo) { 2988 inline else => |*repo| { 2989 var resolution: Event.Resolve = .{ 2990 .title = title, 2991 .labels = labels, 2992 .hunks = hunks.items, 2993 }; 2994 if (comptime @hasField(Event.Resolve, "theirs")) resolution.theirs = self.data.theirs_picks; 2995 const change = Event.Update{ .resolve = resolution }; 2996 Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, change, author) catch |err| switch (err) { 2997 // leave the form up for correction 2998 error.InvalidFields => return, 2999 else => |e| return e, 3000 }; 3001 }, 3002 } 3003 }, 3004 } 3005 3006 const route = listRoute(self.data.identity, entryStatus(entry.*), entry.id) orelse return; 3007 try self.session.navigate(route); 3008 } 3009 3010 // commit a new or edited comment and navigate to its permalink. this is the 3011 // terminal path; the web posts the form to its comment route. 3012 fn submitComment(self: *This, allocator: std.mem.Allocator) !void { 3013 if (comptime wasm) return; 3014 const io = self.session.io orelse return; 3015 const src = self.data.repo_source orelse return; 3016 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 3017 const author = actor.author; 3018 const form = self.formBox() orelse return; 3019 const body_input = try formField(form, "body"); 3020 const body = try body_input.text(allocator); 3021 defer allocator.free(body); 3022 if (!evt.Comment.fieldsValid(body)) return; 3023 3024 var thread_id: [evt.event_id_size * 2]u8 = undefined; 3025 @memcpy(&thread_id, self.data.selected_id); 3026 const parent_id = if (self.data.comment_id.len == 0) thread_id else blk: { 3027 var parent: [evt.event_id_size * 2]u8 = undefined; 3028 @memcpy(&parent, self.data.comment_id); 3029 break :blk parent; 3030 }; 3031 var event_id_hex: [evt.event_id_size * 2]u8 = undefined; 3032 const editing = self.data.view == .edit_comment; 3033 if (editing) { 3034 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 3035 if (!try src.canModify(io, allocator, actor, .comment, &comment_id)) return; 3036 } 3037 switch (src.repo_kind) { 3038 inline else => |repo_kind| { 3039 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 3040 defer any_repo.deinit(io, allocator); 3041 switch (any_repo) { 3042 inline else => |*repo| if (editing) { 3043 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 3044 try evt.Comment.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, kind, &thread_id, &comment_id, body, author); 3045 event_id_hex = std.fmt.bytesToHex(comment_id, .lower); 3046 } else { 3047 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); 3048 }, 3049 } 3050 }, 3051 } 3052 3053 body_input.clear(allocator); 3054 const route = ui.RoutablePage.repoThreadCommentRoute(kind, self.data.identity, &thread_id, &event_id_hex, 0) orelse return; 3055 try self.session.navigate(route); 3056 } 3057 3058 // terminal form submissions; the web posts to the forms' routes instead 3059 fn submitForm(self: *This, allocator: std.mem.Allocator) !void { 3060 switch (self.data.view) { 3061 .new_comment, .edit_comment => try self.submitComment(allocator), 3062 .edit => try self.submitEditedThread(allocator), 3063 else => try self.submitNewThread(allocator), 3064 } 3065 } 3066 3067 // commit the new thread to the repo's events branch and navigate to it. 3068 // this is the terminal path; the web posts the form to the thread route, 3069 // so the wasm side never runs (or compiles) the repo access below. 3070 fn submitNewThread(self: *This, allocator: std.mem.Allocator) !void { 3071 if (comptime wasm) return; 3072 const io = self.session.io orelse return; 3073 const src = self.data.repo_source orelse return; 3074 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 3075 3076 const form = self.threadForm() orelse return; 3077 const title_input = try formField(form, "title"); 3078 const labels_input = try formField(form, "labels"); 3079 const description_input = try formField(form, "description"); 3080 3081 const title = try title_input.text(allocator); 3082 defer allocator.free(title); 3083 const labels = try labels_input.text(allocator); 3084 defer allocator.free(labels); 3085 const description = try description_input.text(allocator); 3086 defer allocator.free(description); 3087 const target_branch = if (comptime supports_drafts) try (try formField(form, "target_branch")).text(allocator) else ""; 3088 defer if (comptime supports_drafts) allocator.free(target_branch); 3089 3090 if (!Event.fieldsValid(title, labels)) { 3091 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, labels, description, target_branch); 3092 return; 3093 } 3094 3095 if (comptime supports_drafts) { 3096 const source = formWidget(form, .patch_source) orelse return error.MissingFormField; 3097 const source_branch = switch (source.selectedKind()) { 3098 .fork => null, 3099 .branch => try source.field().text(allocator), 3100 }; 3101 defer if (source_branch) |branch| allocator.free(branch); 3102 const event_id_hex = Data.create(self.data, self.session, allocator, actor, title, labels, description, target_branch, source_branch) catch |err| { 3103 const failure = FeedbackFailure.fromError(err) orelse return err; 3104 try self.rememberThreadFeedback(failure, title, labels, description, target_branch); 3105 return; 3106 }; 3107 title_input.clear(allocator); 3108 labels_input.clear(allocator); 3109 description_input.clear(allocator); 3110 (try formField(form, "target_branch")).clear(allocator); 3111 const route = ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, &event_id_hex, 0) orelse return; 3112 try self.session.navigate(route); 3113 return; 3114 } 3115 3116 var id_bytes: [evt.event_id_size]u8 = undefined; 3117 io.random(&id_bytes); 3118 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 3119 3120 const event = evt.EventWithId{ 3121 .id = event_id_hex, 3122 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 3123 .author = actor.author, 3124 .event = @unionInit(evt.Event, @tagName(kind), Event{ 3125 .title = title, 3126 .description = description, 3127 .labels = labels, 3128 }), 3129 }; 3130 3131 switch (src.repo_kind) { 3132 inline else => |repo_kind| { 3133 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 3134 defer any_repo.deinit(io, allocator); 3135 switch (any_repo) { 3136 inline else => |*repo| try evt.consume(self.session.eventHost(), .repo, repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 3137 } 3138 }, 3139 } 3140 3141 // wipe the form so a return visit starts fresh 3142 title_input.clear(allocator); 3143 labels_input.clear(allocator); 3144 description_input.clear(allocator); 3145 3146 const route = listRoute(self.data.identity, @fromBackingInt(@intCast(0)), &event_id_hex) orelse return; 3147 try self.session.navigate(route); 3148 } 3149 3150 // re-emit the selected thread's event with the form's content (status 3151 // preserved), then reload the thread's page. this is the terminal path; 3152 // the web posts the form to the edit route. 3153 fn submitEditedThread(self: *This, allocator: std.mem.Allocator) !void { 3154 if (comptime wasm) return; 3155 const io = self.session.io orelse return; 3156 const entry = self.data.selectedThread() orelse return; 3157 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 3158 const author = actor.author; 3159 3160 const form = self.threadForm() orelse return; 3161 const title_input = try formField(form, "title"); 3162 const labels_input = try formField(form, "labels"); 3163 const description_input = try formField(form, "description"); 3164 3165 const title = try title_input.text(allocator); 3166 defer allocator.free(title); 3167 const labels = try labels_input.text(allocator); 3168 defer allocator.free(labels); 3169 const description = try description_input.text(allocator); 3170 defer allocator.free(description); 3171 const target_branch = if (comptime supports_drafts) try (try formField(form, "target_branch")).text(allocator) else ""; 3172 defer if (comptime supports_drafts) allocator.free(target_branch); 3173 3174 if (!Event.fieldsValid(title, labels)) { 3175 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, labels, description, target_branch); 3176 return; 3177 } 3178 const src = self.data.repo_source orelse return; 3179 3180 if (comptime supports_drafts) { 3181 if (entryDraft(entry.*)) { 3182 self.data.editDraft(self.session, allocator, author, entry.id, title, labels, description, target_branch) catch |err| { 3183 const failure = FeedbackFailure.fromError(err) orelse return err; 3184 try self.rememberThreadFeedback(failure, title, labels, description, target_branch); 3185 return; 3186 }; 3187 const route = listRoute(self.data.identity, entryStatus(entry.*), entry.id) orelse return; 3188 try self.session.navigate(route); 3189 return; 3190 } 3191 } 3192 3193 const id_bytes = try evt.parseEventId(entry.id); 3194 if (!try src.canModify(io, allocator, actor, kind, &id_bytes)) return; 3195 3196 switch (src.repo_kind) { 3197 inline else => |repo_kind| { 3198 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 3199 defer any_repo.deinit(io, allocator); 3200 switch (any_repo) { 3201 inline else => |*repo| if (supports_drafts) { 3202 Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 3203 .title = title, 3204 .labels = labels, 3205 .description = description, 3206 .target_branch = target_branch, 3207 } }, author) catch |err| { 3208 const failure = FeedbackFailure.fromError(err) orelse return err; 3209 try self.rememberThreadFeedback(failure, title, labels, description, target_branch); 3210 return; 3211 }; 3212 } else if (has_status) 3213 try Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 3214 .title = title, 3215 .labels = labels, 3216 .description = description, 3217 } }, author) 3218 else 3219 try Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, title, labels, description, author), 3220 } 3221 }, 3222 } 3223 3224 const route = listRoute(self.data.identity, entryStatus(entry.*), entry.id) orelse return; 3225 try self.session.navigate(route); 3226 } 3227 3228 fn rememberThreadFeedback(self: *This, failure: FeedbackFailure, title: []const u8, labels: []const u8, description: []const u8, target_branch: []const u8) !void { 3229 const aa = self.session.arena.allocator(); 3230 const source = if (supports_drafts) blk: { 3231 const form = self.threadForm() orelse break :blk null; 3232 break :blk formWidget(form, .patch_source); 3233 } else null; 3234 self.session.data.form_feedback = @unionInit(ui.Session.FormFeedback, @tagName(feedback_tag), .{ 3235 .failure = failure, 3236 .fields = if (comptime supports_drafts) .{ 3237 .title = try aa.dupe(u8, title), 3238 .labels = try aa.dupe(u8, labels), 3239 .description = try aa.dupe(u8, description), 3240 .target_branch = try aa.dupe(u8, target_branch), 3241 .source_branch = if (source) |value| try value.field().text(aa) else "", 3242 .source_kind = if (source) |value| value.selectedKind() else .fork, 3243 } else .{ 3244 .title = try aa.dupe(u8, title), 3245 .labels = try aa.dupe(u8, labels), 3246 .description = try aa.dupe(u8, description), 3247 }, 3248 }); 3249 } 3250 3251 fn submitConfirmation(self: *This, allocator: std.mem.Allocator, button_index: usize) !void { 3252 switch (confirmation(self.data.view) orelse return) { 3253 .publish => if (comptime supports_drafts) { 3254 if (comptime wasm) return; 3255 const entry = self.data.selectedThread() orelse return; 3256 if (!entryDraft(entry.*)) return; 3257 const author = ((try self.session.authorize(self.data.identity, .read, kind)) orelse return).author; 3258 try self.data.publishDraft(self.session, allocator, author, entry.id); 3259 const route = listRoute(self.data.identity, @fromBackingInt(@intCast(0)), entry.id) orelse return; 3260 try self.session.navigate(route); 3261 } else unreachable, 3262 .merge => if (comptime supports_merge) { 3263 if (comptime wasm) return; 3264 const entry = self.data.selectedThread() orelse return; 3265 const revision: evt.Patch.MergeRevision = if (button_index > 1) .squash else .source; 3266 if (!Data.canMerge(entry.*, self.session) or entry.mergeability.get(revision) != .clean) return; 3267 const author = ((try self.session.authorize(self.data.identity, .write, kind)) orelse return).author; 3268 self.data.mergePatch(self.session, allocator, author, entry.id, revision) catch |err| switch (err) { 3269 error.MergeConflict, error.MergeCheckUnavailable, error.PatchOutOfDate, error.PatchDataUnavailable => { 3270 const route = listRoute(self.data.identity, entryStatus(entry.*), entry.id) orelse return; 3271 try self.session.navigate(route); 3272 return; 3273 }, 3274 else => return err, 3275 }; 3276 const merged = std.meta.stringToEnum(Status, "merged") orelse return; 3277 const route = listRoute(self.data.identity, merged, entry.id) orelse return; 3278 try self.session.navigate(route); 3279 } else unreachable, 3280 .remove => try self.submitRemove(allocator), 3281 } 3282 } 3283 3284 fn submitRemove(self: *This, allocator: std.mem.Allocator) !void { 3285 const comment = self.data.comment_id.len != 0; 3286 const event_id = if (comment) self.data.comment_id else self.data.selected_id; 3287 const id = evt.parseEventId(event_id) catch return; 3288 try self.removeEvent(allocator, if (comment) .comment else kind, id); 3289 } 3290 3291 fn removeEvent(self: *This, allocator: std.mem.Allocator, event_kind: evt.EventKind, id: [evt.event_id_size]u8) !void { 3292 if (comptime wasm) return; 3293 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 3294 const author = actor.author; 3295 3296 if (comptime supports_drafts) { 3297 const selected = self.data.selectedThread() orelse return; 3298 if (event_kind == kind and entryDraft(selected.*)) { 3299 try Data.removeDraft(self.session, allocator, author, &id); 3300 const route = draftsRoute(self.data.identity) orelse return; 3301 try self.session.navigate(route); 3302 return; 3303 } 3304 } 3305 3306 const io = self.session.io orelse return; 3307 const src = self.data.repo_source orelse return; 3308 if (!try src.canModify(io, allocator, actor, event_kind, &id)) return; 3309 const thread_id = evt.parseEventId(self.data.selected_id) catch return; 3310 3311 switch (src.repo_kind) { 3312 inline else => |repo_kind| { 3313 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 3314 defer any_repo.deinit(io, allocator); 3315 switch (any_repo) { 3316 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), 3317 } 3318 }, 3319 } 3320 3321 const id_hex = std.fmt.bytesToHex(id, .lower); 3322 const route = ui.RoutablePage.repoEventsRoute(self.data.identity, .removed, event_kind, &id_hex, null) orelse return; 3323 try self.session.navigate(route); 3324 } 3325 3326 // enter the detail pane, landing on the way back to the list when it shows 3327 fn focusDetail(self: *This, index: usize, root_focus: *Focus) void { 3328 if (widget.listBackId(self.detailFrame(index)) != null) return widget.focusListBack(self.detailFrame(index), root_focus); 3329 _ = self.detail(index).focusFirst(root_focus); 3330 } 3331 3332 // return to the list. 3333 fn focusList(self: *This, index: usize, root_focus: *Focus) void { 3334 root_focus.setFocus(self.listScroll(index).getFocus().id); 3335 } 3336 3337 // cross to the header tabs above the stack. 3338 fn focusHeader(self: *This, root_focus: *Focus) void { 3339 root_focus.setFocus(self.box.children.keys()[header_index]); 3340 } 3341 3342 pub fn clearGrid(self: *This) void { 3343 self.box.clearGrid(); 3344 } 3345 3346 pub fn getGrid(self: This) ?Grid { 3347 return self.box.getGrid(); 3348 } 3349 3350 pub fn getFocus(self: *This) *Focus { 3351 return self.box.getFocus(); 3352 } 3353 3354 // for the parent's "scroll up at the top jumps to the header" check: at the 3355 // top only while the header tabs hold focus, so up from the split 3356 // crosses into the tabs first. 3357 pub fn atTop(self: *This) bool { 3358 return self.headerActive(); 3359 } 3360 }; 3361 } 3362