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